Python

Python Sets

Every component in a set would have to be unique and unchangeable and will eliminate redundant entries from sets. Sets are changeable, which indicates we may change the elements of the sets if they’ve been created. The items of the set have no index, meaning we would not explicitly retrieve any component of the set by utilizing the index. Therefore, we could display all elements of the set. Let’s go through the sets in detail.

Creation of a Set

Python also has a set() function that generates a set from the provided elements of the sequence. The elements of the set are surrounded by square brackets and are separated by commas.

s = set(["Aima", "Mamoona", "Aleena"])

print(s)

 

s.add("Saima")

print(s)

Graphical user interface, text Description automatically generated

First, we define the elements of the set by using the set() function. This set contains the names of different girls. We have initialized a variable “s” to store the set’s elements. Then, we used the print() method to display the required set.

We will add an extra element in the specified set, so we have applied the add() method. As a function parameter, we will give an additional name that we want to insert in the set. Once again, we employ the print() command to get the set after inserting a new element.

Normal Set and Frozen Set

The frozen sets are unchangeable entities with only procedures and expressions that give outputs without modifying the sets’ elements that have been implemented. While components of a set could be adjusted at any moment, components of a frozen set would not change once they’ve been created.

nor_set = set(["Azka", "Aleyan", "Shayan"])

print("Normal Set")

print(nor_set)

frozen_set = frozenset(["Ali", "Haseeb", "Ajmal"])

print("\nFrozen Set")

print(frozen_set)

Text Description automatically generated

We have utilized the set() method to create a simple set. The elements of this set will be stored in a variable, “nor_set”. This set is called a normal set. We have used a print() statement to show the elements of the normal set. In the next step, we specified the frozen set’s elements. So, we have applied the frozenset() function for defining the elements. Then, we call the print() function to represent the frozen set.

Text Description automatically generated

After running the previous code, we get two sets: a normal set and a frozen set.

Inserting Different Elements in the Set

We can insert the new elements in the required set by applying the add() function. This function contains the element as a parameter we want to add to the set.

Country = {"Australia", "Germany", "Canada"}

print("Country:", end = " ")

print(country)

country.add("Spain")

for j in range(1, 6):

country.add(j)

print("\nSet after inserting element:", end = " ")

print(country)

Text Description automatically generated

In this instance, we will see how to insert a new element into the set. The first step is the creation of the set. We have created a set that contains the names of different countries. The print() command will display the countries’ names.

In the next step, we want to add a new name, “Spain”, to the required set of the country. To accomplish this, we call the add() function. We have provided “Spain” as its argument. We have employed the “for” loop to iterate the value up to 6. Within the “for” loop, the loop variable is being initialized. Then, we apply the add() function to the variable.

Text Description automatically generated

Union of the Sets

We can take the union of two sets with the help of the union() function and the ‘|’ operator. In the union, the elements of the specified sets are merged.

side_characters = {"Abeer", "Rahmeen", "Rubab"}

vilan = {"Kiran", "Zain"}

hero = {"Salar"}

cast = side_characters.union(vilan)

print("Union using union() function")

print(cast)

cast = side_characters|hero

print("\nUnion using '|' operator")

print(cast)

Text Description automatically generated

Here, we will create three different sets. The first set includes the side characters of the movie. The second set contains the names of the movie’s villains, and the thirst set has the name of the movie’s hero. In the next step, we have to take the union of the two sets “side_characters” and “vilan” using the union() method.

After taking the union, the resultant value is stored in the variable “cast”. print() method prints the union of these two sets. Now, we use another method of taking the union. We have utilized the ‘|’ operator. We take the union between the sets of “side_characters” and “hero”. In the end, we again employ a print() statement to show the union value.

Text Description automatically generated

The Intersection of the Sets

Common elements of the two sets have been chosen while taking the intersection. They’re identical to iterating through Hash lists and merging identical data from defined values of sets.

s_1 = set()

s_2 = set()

for j in range(6):

s_1.add(j)

for j in range(4,8):

s_2.add(j)

s_3 = s_1.intersection(s_2)

print("Intersection using intersection() function")

print(s_3)

s_3 = s_1 & s_2

print("\nIntersection using '&' operator")

print(s_3)

Text Description automatically generated

We will get the intersection of the two sets in this example. For this, we must define the sets using the set() method. We take two empty sets. To add the values in both sets, “s_1” and “s_2”, we have to utilize the “for” loop. Here, we specify the values of both sets by applying the add() method.

Now, we take the intersection of the sets “s_1” and “s_2” with the help of the intersection() function. This print() method represents the intersection value. Then, we use another method of taking intersections that use the ‘&’ operator. After getting the intersection value between “s_1” and “s_2”, we have employed the print() statement.

Conclusion

In this article, we discussed sets in Python. A set is an unsorted sequence of data points, and that data type is iterable, changeable, and has no redundant items. The set module in Python illustrates the mathematical concept of a set. We have also seen how to create a different set, add the elements in the sets, the difference between a normal set and a frozen set, and the methods of taking union and intersection of sets. We can take the intersection of the sets by using the intersection() method and the ‘|’ operator. Similarly, we can get the union of the sets by using the union() function and the ‘&’ operator.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content