Python Sets – Notes
[Link] Kiran
Contents
1 Introduction 1
2 Definition 1
n
3 Syntax 1
ira
4 Creating a Set 1
4.1 Using Curly Braces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
iK
4.2 Using set() Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
5 Accessing Set Elements 2
h
6 Adding Elements to a Set 2
nt
6.1 add() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
6.2 update() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
ra
7 Removing Elements from a Set 2
7.1 remove() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
K
7.2 discard() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
P.
7.3 pop() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
7.4 clear() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
8 Set Operations 3
8.1 Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
8.2 Intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
8.3 Difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
8.4 Symmetric Difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
9 Additional Set Methods 4
9.1 issubset() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
9.2 issuperset() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
9.3 isdisjoint() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
9.4 copy() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
i
9.5 difference update() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
9.6 intersection update() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
9.7 symmetric difference update() . . . . . . . . . . . . . . . . . . . . . . . . 5
10 Set vs List 5
11 Advantages of Sets 6
12 Applications of Sets 6
13 Exam & Viva Points 6
14 Questions 6
14.1 Very Short Answer Questions (1 Mark) . . . . . . . . . . . . . . . . . . . 6
n
14.2 Short Answer Questions (2–3 Marks) . . . . . . . . . . . . . . . . . . . . 7
14.3 Long Answer Questions (5 Marks) . . . . . . . . . . . . . . . . . . . . . . 7
ira
15 Conclusion 7
h iK
nt
ra
K
P.
ii
1 Introduction
A set in Python is a built-in data structure used to store a collection of unique elements.
Sets are commonly used for mathematical operations such as union, intersection, and
difference.
2 Definition
A set is an unordered and mutable collection of elements with no duplicate values.
Key Characteristics:
Stores unique elements only
Unordered collection
n
Mutable (elements can be added or removed)
ira
Defined using curly braces {}
iK
3 Syntax
set_name = { element1 , element2 , element3 }
h
nt
4 Creating a Set
ra
4.1 Using Curly Braces
K
numbers = {1 , 2 , 3 , 4}
P.
print ( numbers )
Output:
{1, 2, 3, 4}
4.2 Using set() Constructor
letters = set ([ " a " , " b " , " c " , " a " ])
print ( letters )
Output:
{’a’, ’b’, ’c’}
Note: Duplicate elements are automatically removed.
1
5 Accessing Set Elements
Set elements cannot be accessed using index positions. Elements are accessed using loops.
for item in numbers :
print ( item )
6 Adding Elements to a Set
6.1 add() Method
Adds a single element to the set.
numbers . add (5)
n
print ( numbers )
ira
6.2 update() Method
iK
Adds multiple elements.
numbers . update ([6 , 7 , 8])
print ( numbers )
h
nt
7 Removing Elements from a Set
ra
7.1 remove() Method
K
Removes a specified element. Raises an error if element is not present.
numbers . remove (3)
P.
7.2 discard() Method
Removes element without raising error.
numbers . discard (10)
7.3 pop() Method
Removes and returns a random element.
numbers . pop ()
2
7.4 clear() Method
Removes all elements.
numbers . clear ()
print ( numbers )
Output:
set()
8 Set Operations
8.1 Union
n
A = {1 , 2 , 3}
ira
B = {3 , 4 , 5}
print ( A | B )
iK
Output:
{1, 2, 3, 4, 5}
h
8.2 Intersection
nt
print ( A & B )
ra
Output:
{3}
K
P.
8.3 Difference
print ( A - B )
Output:
{1, 2}
8.4 Symmetric Difference
print ( A ^ B )
Output:
{1, 2, 4, 5}
3
9 Additional Set Methods
9.1 issubset()
Checks whether one set is a subset of another.
A = {1 , 2}
B = {1 , 2 , 3 , 4}
print ( A . issubset ( B ))
Output:
True
Explanation: All elements of set A are present in set B.
n
9.2 issuperset()
ira
Checks whether a set contains all elements of another set.
print ( B . issuperset ( A ))
iK
Output:
True
h
nt
9.3 isdisjoint()
Checks whether two sets have no common elements.
ra
C = {5 , 6}
print ( A . isdisjoint ( C ))
K
Output:
P.
True
Explanation: Sets A and C have no common elements.
9.4 copy()
Creates a shallow copy of a set.
D = A . copy ()
print ( D )
Output:
{1, 2}
4
9.5 difference update()
Removes elements found in another set.
A = {1 , 2 , 3 , 4}
B = {3 , 4}
A . difference_u pdate ( B )
print ( A )
Output:
{1, 2}
9.6 intersection update()
n
Keeps only common elements.
ira
A = {1 , 2 , 3}
B = {2 , 3 , 4}
A . intersec t i o n_ u p da t e ( B )
iK
print ( A )
Output:
h
{2, 3}
nt
9.7 symmetric difference update()
ra
Keeps elements that are not common.
A = {1 , 2 , 3}
K
B = {3 , 4}
A. symmetric_difference_update (B)
P.
print ( A )
Output:
{1, 2, 4}
10 Set vs List
Feature Set List
Duplicates Not Allowed Allowed
Order Unordered Ordered
Indexing Not Supported Supported
Performance Faster lookup Slower lookup
5
11 Advantages of Sets
Eliminates duplicate values
Supports mathematical operations
Faster membership testing
Easy comparison of collections
12 Applications of Sets
Removing duplicate data
Membership testing
n
Mathematical operations
ira
Data comparison
iK
13 Exam & Viva Points
Sets do not allow duplicates
h
Sets store only unique elements
nt
Sets are unordered
ra
Cannot access elements using index
K
Empty set is created using set()
P.
Subset is checked using issubset()
Superset is checked using issuperset()
Empty set must be created using set()
14 Questions
14.1 Very Short Answer Questions (1 Mark)
1. Define a set in Python.
2. Are duplicate elements allowed in a set?
3. Write the syntax to create an empty set.
6
4. Which method is used to add an element to a set?
5. Which method is used to remove all elements from a set?
14.2 Short Answer Questions (2–3 Marks)
1. What is a set? Explain any two properties of sets.
2. Differentiate between set and list.
3. Explain the add() and remove() methods of a set.
4. What is the difference between remove() and discard() in sets?
14.3 Long Answer Questions (5 Marks)
n
ira
1. Explain Python sets and their properties with examples.
2. Explain set operations such as union, intersection, and difference with examples.
iK
3. Write a Python program to perform different set operations.
4. Compare dictionaries and sets based on storage, access, and performance.
h
15 Conclusion
nt
Python sets provide an efficient way to store unique elements and perform mathematical
ra
operations, making them useful in many programming scenarios.
K
P.