Anyone Can Code.
This tutorial will be the complete discussion of Sets for this course. It's quite a long one so take your time and make sure you understand everything.
A set is a collection of data which is unordered and unindexed. Their contents are enclosed within curly brackets, but they are not dictionaries.
names = {'John', 'Henry', 'Julia', 'Paul'} print(names)
{'Julia', 'John', 'Paul', 'Henry'}
Since sets are unordered, the order in which the items are placed can change at any given
point
in time. Therefore, there are no fixed positions and so one can't access the items using an
index value. However, one can still loop through the items in the set using a for
>
loop.
names = {'John', 'Henry', 'Julia', 'Paul'} for x in names: print(x)
Julia John Paul Henry
You can check whether a specific item is present in the set by using the in
keyword.
names = {'John', 'Henry', 'Julia', 'Paul'} if 'John' in names: print('Yes')
Yes
You cannot change the items within a created set but you can add new items or remove items. However, the added item will only be added if it doesn't already exist in the set.
#the add() method is used to add one item names = {'John', 'Henry', 'Julia', 'Paul'} names.add('Candace') print(names) #the update() method is used to add multiple items names = {'John', 'Henry', 'Julia', 'Paul'} names.update(['Candace', 'Kenan', 'Joe']) print(names)
{'Julia', 'Paul', 'Henry', 'John', 'Candace'} {'Julia', 'Joe', 'Paul', 'Henry', 'Kenan', 'John', 'Candace'}
There are many ways to join two or more sets in Python. The union()
method creates
a new set
which contains items from both sets and the update()
method transfers all the
elements from one
set to the second.
#union() method set1 = {'John', 'Henry', 'Julia', 'Paul'} set2 = {3, 19, 5, 2} set3 = set1.union(set2) print(set3) #update() method set1 = {'John', 'Henry', 'Julia', 'Paul'} set2 = {3, 2, 7, 5} set1.update(set2) print(set1)
{'Julia', 2, 3, 'Paul', 5, 'Henry', 19, 'John'} {'Julia', 2, 3, 'Paul', 5, 7, 'Henry', 'John'}
Before we look at some more Set methods, let's look at some basic Set Theory. If you are already familiar with this, scroll down.
In Python we have some set methods that are similar to mathematical set operations. In this tutorial, we are looking at union, intersection, subsets, supersets and difference of sets.
Union is the combination of two or more sets, without repeating common elements. An intersection is the set of all common elements between one or more sets.
For example, if we have A = {2,3,4} and B = {3,5,6}, then A ∪ B = {2,3,4,5,6} and A ∩ B = {3}
A subset is a set which contains some of the elements of another set. A superset of another set is a set that contains the elements of the second set
If we have a set A = {2,3,4} and B = {2,3,4,5,6}, then A is a subset of B and B is a superset of A
The difference of two sets is the elements of one set minus the elements in common with another set. For example, if we have A = {2,3,4} and B = {3,5,6}, then A-B = {2,4}
len()
the len() method determines the number of items in a set
names = {'John', 'Henry', 'Julia', 'Paul'} print(len(names))
4
remove()
the remove() method removes a specified item from the set
names = {'John', 'Henry', 'Julia', 'Paul'} names.remove('John') print(names)
{'Julia', 'Paul', 'Henry'}
pop()
the pop() method removes the last item in the set so you will not know what item gets removed
names = {'John', 'Henry', 'Julia', 'Paul'} names.pop() print(names)
{'John', 'Paul', 'Henry'}
clear()
the clear() method empties the set
names = {'John', 'Henry', 'Julia', 'Paul'} names.clear() print(names)
set()
intersection()
the intersection() method returns a set that is the intersection of two other sets
set1 = {'John', 'Henry', 'Julia', 'Paul', 19} set2 = {3, 19, 5, 2, 'Henry'} set3 = set1.intersection(set2) print(set3)
{19, 'Henry'}
issubset()
the issubset() method checks whether another set contains a particular set or not. It returns a boolean.
set1 = {'John', 'Paul'} set2 = {'John', 'Henry', 'Julia', 'Paul'} print(set1.issubset(set2))
True
issuperset()
the issuperset() method checks whether another set is a superset of a particular set or not
set1 = {'John', 'Henry', 'Julia', 'Paul'} set2 = {'John', 'Paul'} print(set1.issuperset(set2))
True
difference()
the difference() method returns a set that contains the item only present in the first set.
set1 = {'John', 'Henry', 'Julia', 'Paul'} set2 = {'John', 45, 'Alex'} print(set1.difference(set2))
{'Julia', 'Paul', 'Henry'}
That's it for this tutorial! Click Next to access the next tutorial!
This site uses cookies to improve your experience as well as for site metrics. To see more, check out our Privacy Policy