0% found this document useful (0 votes)
17 views12 pages

Python Sets: Creation and Operations

Sets in python programming

Uploaded by

dulbiki09
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views12 pages

Python Sets: Creation and Operations

Sets in python programming

Uploaded by

dulbiki09
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Learn

Programming
Using Python
Dr. Probidita Roychoudhury
Associate Professor
Dept. of Computer Science
St. Anthony’s College
USING SETS
Set
• Collection of values without any duplicates.
• Mutable
• Unordered
•{ }
Creating a Set
grades = {"A+", "A-", "B+", "B", "C+", "C", "D",
"F"}
print(grades)

Output : {'B', 'B+', 'C', 'F', 'C+', 'A+', 'D', 'A-’}

No Duplicates
Other ways of creating a Set
grade_list =["A+", "A-", "B+", "B", "C+", "C",
"D", "F"]
grade_set=set(grade_list)
print("List :",grade_list)
print("Set :",grade_set)

Output:
List : ['A+', 'A-', 'B+', 'B', 'C+', 'C', 'D', 'F’]
Set : {'B', 'F', 'C+', 'B+', 'C', 'A-', 'A+', 'D'}
Point to Note
• Sets are unordered
• They are stored internally in the form of a
hash table
• When displayed, they are displayed according
to their order in the hash table
• This result in faster lookup
Other ways of creating a Set
grade_tuple =("A+", "A-", "B+", "B", "C+", "C",
"D", "F")
grade_set=set(grade_tuple)
print(”Tuple :",grade_tuple)
print("Set :",grade_set)

• Output:
Tuple : ('A+', 'A-', 'B+', 'B', 'C+', 'C', 'D', 'F')
Set : {'B', 'F', 'C+', 'B+', 'C', 'A-', 'A+', 'D'}
Point to Note
• Duplicate values will be removed when
converting from lists or tuples to sets

grade_tuple =("A+", "A-", "B+", "B", "C+", "C",


"D", "F", "F")
grade_set=set(grade_tuple)
print("List :",grade_tuple)
print("Set :",grade_set)
Elements of a set
• The elements of a set can be of different
types.
my_set={1,”Hello”, 34.6, None}
• However, all items in a set must be
immutable.
• Hence, a set cannot contain lists or other sets
• Allowed types
• Numbers (int, float, etc.)
• Strings ("hello")
• Tuples (only if they contain immutable elements)
Why can’t elements of a set be
mutable?
• Sets in Python are implemented as hash
tables.
• To place an element in a hash table, Python
computes its hash value (via the built-
in hash() function).
• If an object is mutable (can change), its hash
could change too.
• That would break the set, because the
element would no longer be in the correct
slot, and lookups would fail.
Creating empty iterables
lst=[]
OUTPUT
tup=()
class 'list'>
st={} <class 'tuple'>
another_st=set() <class 'dict'>
<class 'set'>
print(type(lst))
print(type(tup))
print(type(st))
print(type(another_st))
Operations on Sets
• issubset()
• update()
• issuperset()
• add()
• union()
• remove()
• intersection()
• pop()
• difference()
• clear()
• copy()
• len()
• enumerate()
• in
• min(), max(), sum(),
• not in
• sorted()

You might also like