Up-Skill your Career with the world's effective online learning platform

Trusted by 200,000+ Satisfied Learners & 123+ Corporates

Python is a powerful programming language that has started regaining its fame for its usage in the Data Science along with the latest technologies like R and etc. Having said that, let us take a look at the tiny winy bits of concepts to get ourselves stronger in this programming language.

In this article, we will try to understand the concept of sets in Python programming language. To provide you a better understanding on what sets can do in Python programming language, take a look at the following section.

Learn how to use Python, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Python Training Demo!

Just like dictionaries, sets have no order in the collections of items or objects. Sets require that the items or objects that are stored within are hashable. Sets also require that the elements present in them are unique and immutable – just the opposite of what we have seen in lists. However the set itself is mutable, that is, we can add or remove elements from it. To sum all the points that we have discussed over here, we need to use sets when we have an unordered set of values that are unique, immutable and which are hashable as well.

A set of elements can be created by putting the elements in curly braces or use the built-in set() function to add elements to it. Please take a look at the following example for your better understanding.

Creating a set with no elements within it is a bit of a tricky process. If we try the following, you would observe the reason why?

Creating a set with empty curly braces creates a dictionary instead of a set, but whereas creating an empty set using the built-in function set() creates a set without any troubles.

As we have discussed earlier, sets are mutable – but since they are unordered, indexes would not make any sense to it. We will not be able to access or update an element based on the index of an element in a set, as set does not support both indexing and slicing. To add an element to a set, we can use the add() method and to add more than one element to a set, we can use the update() method – in either of the cases, duplicates are not allowed to be added to the set.

The following example shows the commands in its usage, just the way it is explained earlier.

Now to remove elements from a set, we can use either the discard() method or the remove() method. The difference in usage of these two functions is – when discard() method is used to remove an element of the set which doesn’t exist in the set, it will not raise an error but the same case when remove() method is used, it will error out. The following example will show the usage of both these methods on a set of elements.

Similar to the methods that we have used on lists, we can use the pop() method and the clear() method on the set. The only difference here with set is that, there is no order of elements that will be maintained and hence the reason, we will not be able to determine which element of the set will be removed using the pop() method. In case of list, the last element of the list gets popped when the pop() method is used. Let us now take a look at an example on how this can be achieved.

Let us know concentrate on the set operations like ‘Set Union’, ‘Set Intersection’, ‘Set Difference’ and ‘Set Symmetric Difference’. Sets can be used in Python to carry out mathematical set operations like Union, Difference and Intersection. We can achieve performing these activities by operators or by using methods.

Frequently Asked Python Interview Questions & Answers

Let us now consider an example of two sets with values as like given below:

setA = {1, 2, 3, 4, 5, 6}

setB = (4, 5, 6, 7, 8, 9)

Now with these set values, let us carry out the 4 different operations as we have discussed just a little bit earlier.

A Union operation applied on two different sets, will result in all the elements from both the sets excluding the duplicates (if there are any present). Union is performed by using the | operator in Python programming language or by using the union() method on the sets that we want to perform this operation. A Union operation on the sets defined above will be giving a result as shown below.

An Intersection operation applied on two different sets will result in the elements that are common between both of these sets. An Intersection operation is performed by using the & operator in Python programming language or by using the intersection() method on the sets that we want to perform this operation. An Intersection operation on the sets defined above will be giving a result as shown below.

A set Difference operation applied on two different sets will result in the elements present in the first set but not available in the second set. A Difference operation is performed by using the – operator in Python programming language or by using the difference() method on the sets that we want to perform this operation. A difference operation on the sets defined above will be giving a result as shown below.

setA – setB would yield set(1, 2, 3) whereas setB – setA would yield set(7, 8, 9).

A set Symmetric Difference operation applied on two different sets will result in the elements present in both the sets excluding the ones that are repeating in both the sets. A Symmetric Difference operation is performed by using the ^ operator in Python programming language or by using the symmetric_difference() method on the sets that we want to perform this operation. A symmetric difference operation on the sets defined above will be giving a result as shown below.

In this article, we have seen what sets are and why are they used in Python programming language. We have also tried to take a deeper look into the concept with various examples as well.

Hope that you were clear with the concepts after going through this detailed article, please do comment if you have any suggestions to make.

Explore Python Sample Resumes! Download & Edit, Get Noticed by Top Employers!**Download Now!**

Free Demo for Corporate & Online Trainings.