Sets

Python SELF EN
Level 9 , Lesson 1
Available

1.1 Getting to Know Sets

The concept of a set comes from mathematics to programming. A set is a group of unique elements. This characteristic makes sets a powerful tool in programming with many unique capabilities.

What is a group of unique elements? Imagine a "Top 10 Most Popular Names in the Country" list. The very nature of this definition implies that each name in the list is unique and doesn't repeat. Only the carriers of the names do.

Or picture a stamp collection. You collect one stamp from each country. Your collection is a set of stamps, where each stamp is unique, and you don't want duplicate stamps. That's the idea of a set.

Main Characteristics of Sets

Uniqueness

A set is a group of unique items. Imagine you have a list of customer numbers you need to call. In this list, each number should be unique to avoid annoying anyone with repeat calls.

Unordered

Sets don't care about the order of items. It's like tossing fruits into a basket without worrying what’s at the bottom or on top. The main thing is that all the fruits in the basket are different.

Mutable

You can add and remove items from a set. It's like being able to throw a new fruit into the basket or remove one you don't need anymore, anytime.

1.2 Set Operations

You can perform various operations on sets, like big groups of elements. Here are 4 of the most common ones:

  • Union: The result of a union of two sets includes all unique elements from both sets.
  • Intersection: The result of the intersection of two sets includes only the elements that are present in both sets.
  • Difference: The result of the difference of two sets includes elements that are present in the first set but not in the second.
  • Symmetric Difference: The result of the symmetric difference of two sets includes elements that are present in one of the sets but not in both simultaneously.

Here's a nice picture to help remember the essence of these operations:

Union:

The result of the union of two sets A and B includes all unique elements from both sets.

Intersection:

The result of the intersection of two sets includes only the elements that are present in both sets.

Difference:

The result of the difference of two sets includes elements that are present in the first set but not in the second.

Symmetric Difference:

The result of the symmetric difference of two sets includes elements that are present in either one or the other set, but not in both at the same time.

1.3 Features of Sets

Sets are famous not only for their properties but also for the specific operations you can perform on them.

Unique Collections

Imagine you’re collecting celebrity autographs. You want each autograph in your collection to be unique. So if you already have your favorite actor’s autograph, you won’t collect a second one. Your collection of autographs is a set of unique autographs.

Removing Duplicates

Suppose you have a guest list for a party, but you accidentally listed some people twice. To make sure each guest is invited only once, you can create a set of guests. This set will automatically keep only unique names, and duplicates will disappear.

Membership Check

Imagine playing a game where you need to collect different types of treasures. Each chest can contain one out of a set of treasures. If you want to find out if you have already found a particular treasure, you just check if it’s in your set of treasures.

Merging Groups

Imagine you have two lists of friends: one from school, another from the sports club. If you want to know how many unique people you know, you can merge these two lists into one set. That way, you’ll have a list of all unique friends without repetitions.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION