Programming in Python
(Data Type: Set)
Dr. Faisal Anwer
Department of Computer Science
Aligarh Muslim University 1
Copyright © Dept of Computer Science, AMU, Aligarh. Permission required for reproduction or display.
Recap
• Creating Tuple
• Accessing Tuple
• Tuple Slicing
• Operations & Functions of Tuple
• Methods of Tuple
Contents
• Creating Set
• Accessing Set
• Set Slicing
• Operations & Functions of Set
• Methods of Set
Set
A set is an unordered collection of elements.
Index operator is not applicable.
Every element must be unique and immutable.
Duplicates are eliminated
Elements are Immutable like tuples, strings, etc.
However, the set itself is mutable.
It is possible to add or remove items to set.
We can change its state—its length and content.
Sets can be used to perform mathematical set operations like
union, intersection, symmetric difference etc.
Set Creation
A set is created by placing all items (elements) inside curly
braces {}, separated by comma or by using the built-in function
set().
It can have any number of items and they may be of different
(integer, float, tuple, string etc.).
The empty set can also be created using a built-in function
set() only.
The empty curly braces {} will be treated as an empty
dictionary in Python.
set1---- {11, 22}
sett2 = set() ---- { }
set3= set({1, 2, 3})
Set Creation
• The range function returns a set of numbers that consists of
elements from zero to one less than the value of argument.
print(set()) ------set().
print(set(range(4)) ------{0, 1, 2, 3}
print(set(range(1, 4)) ------{1, 2, 3}
print(set(range(2, 10, 2)) ------{2, 4, 6, 8}
print(set(range(-1, -11, -2)) ------{-1, -3, -5, -7, -9}
Sets have no duplicates
A = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}
Print(A)
{1, 2, 3, 4, 5}
Sets do not contain mutable
A = {1, 2, 3, 4, 5, „PYTHON‟, 2.3, [12, 34]}
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
A = {1, 2, 3, 4, 5, 'PYTHON', 2.3, [12, 34]}
TypeError: unhashable type: 'list'
Sets do not support indexing
>>> myset = {'Apples', 'Bananas', 'Oranges'}
>>> myset
{'Bananas', 'Oranges', 'Apples'}
>>> myset[0]
Traceback (most recent call last):
File "<pyshell#390>", line 1, in <module>
myset[0]
TypeError: 'set' object does not support indexing
So, How to access elements of set?
Sets are iterable
friends = {“Amar”, “Aman”, “Sahil”}
for friend in friends :
print('Happy New Year:', friend)
Basic Operations
Concatenation
Not Supported
Repetition
Not Supported
Membership operators
in /not in:
The in operator is used to check the membership of an element
in the set.
2 in {1, 2, 3} will return True
4 in {1, 2, 3} will return False
Sets are mutable
Sets are mutable.
add(): It takes one element and adds this element to the set.
pop(): It removes one item from the set and returns it.
remove() : It removes an element from a set. If the element is not
a member, raise a KeyError
clear(): This function removes all items from a set.
Example:
set1 = {11, 22, 33,5,10}
set1.add(2) ---- will add 2 to set1
set1.remove(5) --- It removes 5 from the set.
set1.pop() ---- It will remove any item.
set1.clear() --- It removes all item in a set
Sets Operations
Sets can be used to perform the set operations
A = {1, 2, 3, 4, 5, 6}
B = {5, 6, 7, 8, 9}
Union
The union of A and B consists of all elements from both sets.
Union is performed using | operator. It can also be
accomplished using the method union().
The | operator/union() method takes two set and returns a new
set consisting of union of two sets.
Example
A | B
A.union(B)
Sets Operations
Intersection
The intersection of A and B consists of all elements that are
common in both sets.
It is performed using & operator or method intersection().
The & operator/intersection() method takes two set and
returns a new set consisting of intersection of two sets.
Example
A & B
A.intersection(B)
Sets Operations
Set Difference
The difference of A and B (i.e. A - B) consists of elements that
are only in A but not in B.
Similarly, B - A is a set of element in B but not in A.
It is performed using minus(-) operator or difference() method.
The (-) operator/difference() method takes two sets and
returns a new set consisting of difference of two sets.
Example
A - B
A.difference(B)
Sets Operations
Symmetric Difference
The symmetric difference of sets A and B is a set of elements in
both A and B except those that are common in both
A ^ B = (A|B) – (A&B)
The symmetric difference is performed using ^ operator or
method symmetric_difference().
The ^ operator takes two set and returns a new set consisting of
symmetric difference of two sets.
Example
A ^ B
A.symmetric_difference(B)
Sets Methods
A.update(B) ---Update the set A with the union of A and B
A.difference_update(B) ---Remove all elements of set B from set A
A.symmetric_difference_update(B) ---Update a set A with the
symmetric difference of A and B
A.intersection_update(B) --- Update the set A with the intersection of
A and B
isdisjoint() ---- Return True if two sets have a null intersection
issubset() ----Return True if another set contains this set
issuperset() ----Return True if this set contains another set
Functions For SET
len(): Returns the length (the number of items) of the set.
max(): Returns the largest item in the set.
min(): Returns the smallest item in the set.
sum(): Retruns the sum of all elements in the set.
Summary
• Creating Set
• Accessing Set
• Set Slicing
• Operations & Functions of Set
• Methods of Set