Set in Python
A set is a collection of unique elements in Python. A set is an unordered
collection of unique elements enclosed in curly braces {}.
A set is an unordered collection of unique elements in Python,
represented using curly braces {}. It supports mathematical
operations such as union, intersection, difference, and symmetric
difference.
Example
s = {10, 20, 30, 40}
print(s)
Output
{10, 20, 30, 40}
2. Characteristics of Sets
1. Unordered
○ Elements do not have a fixed position.
○ Indexing is not possible.
2. Unique Elements
○ Duplicate values are automatically removed.
3. Mutable
○ We can add or remove elements.
4. Different Data Types Allowed
Example
s = {10, "Python", 3.5, True}
print(s)
Syntax of Set
set_name = {value1, value2, value3}
Example
numbers = {1, 2, 3, 4}
Creating Sets
Normal Set
s = {1, 2, 3, 4}
print(s)
Empty Set
s = set()
print(s)
⚠️ {} creates a dictionary, not a set.
Set with Duplicate Values
s = {1, 2, 3, 2, 1, 4}
print(s)
Output
{1, 2, 3, 4}
Duplicates removed automatically.
Adding Elements to Set
Using add()
s = {10, 20, 30}
[Link](40)
print(s)
6. Adding Multiple Elements
Using update()
s = {1, 2, 3}
[Link]([4, 5, 6])
print(s)
Output
{1, 2, 3, 4, 5, 6}
Removing Elements
1. remove()
s = {10, 20, 30}
[Link](20)
print(s)
2. discard()
s = {1, 2, 3}
[Link](2)
print(s)
3. pop()
Removes a random element.
s = {10, 20, 30}
[Link]()
print(s)
8. Set Operations
Sets support mathematical operations.
1. Union
Combines elements of two sets.
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B)
Output
{1, 2, 3, 4, 5}
or
print([Link](B))
2. Intersection
Common elements.
A = {1, 2, 3}
B = {2, 3, 4}
print(A & B)
Output
{2, 3}
3. Difference
Elements in A but not in B.
A = {1, 2, 3}
B = {2, 3, 4}
print(A - B)
Output
{1}
4. Symmetric Difference
Elements in either set but not both.
A = {1, 2, 3}
B = {3, 4, 5}
print(A ^ B)
Output
{1, 2, 4, 5}
9. Membership Operations
s = {10, 20, 30}
print(20 in s)
print(50 in s)
Output
True
False
10. Set Functions
Length
s = {1, 2, 3, 4}
print(len(s))
Maximum
s = {5, 2, 9, 1}
print(max(s))
Minimum
print(min(s))
11. Frozen Set
A frozenset is an immutable version of a set.
Example
s = frozenset([1, 2, 3, 4])
print(s)
Elements cannot be added or removed.
12. Nested Sets
⚠️ Sets cannot contain normal sets because sets are mutable.
But they can contain frozensets.
Example
s = {1, 2, frozenset([3, 4])}
print(s)
Output
{1, 2, frozenset({3, 4})}
13. Set Programs for Lab
Program 1 – Create and Print Set
s = {10, 20, 30}
print("Set:", s)
Program 2 – Add Element to Set
s = {1, 2, 3}
[Link](4)
print("Updated set:", s)
Program 3 – Remove Element
s = {10, 20, 30}
[Link](20)
print(s)
Program 4 – Union of Two Sets
A = {1, 2, 3}
B = {3, 4, 5}
print("Union:", [Link](B))
Program 5 – Intersection
A = {1, 2, 3}
B = {2, 3, 4}
print("Intersection:", [Link](B))
Program 6 – Difference
A = {1, 2, 3}
B = {2, 3, 4}
print("Difference:", A - B)
Program 7 – Symmetric Difference
A = {1, 2, 3}
B = {3, 4, 5}
print("Symmetric Difference:", A ^ B)
Program 8 – Count Elements
s = {10, 20, 30, 40}
print("Total elements:", len(s))
Program 9 – Check Membership
s = {5, 10, 15}
num = int(input("Enter number: "))
if num in s:
print("Element exists")
else:
print("Element not found")
Program 10 – Convert List to Set
list1 = [1, 2, 2, 3, 4, 4]
s = set(list1)
print("Set:", s)
Output
{1, 2, 3, 4}
14. Difference Between List, Tuple, and Set
Feature List Tuple Set
Brackets [] () {}
Order Ordered Ordered Unordered
Duplicate Allowed Allowed Not allowed
Mutability Mutable Immutable Mutable
Indexing Yes Yes No
Lab Experiment
1. Program to Create and Display a Set
s = {10, 20, 30, 40}
print("Set elements:", s)
2. Program to Add an Element to a Set
s = {1, 2, 3}
[Link](4)
print("Updated set:", s)
Output
{1, 2, 3, 4}
3. Program to Add Multiple Elements
s = {10, 20}
[Link]([30, 40, 50])
print("Updated set:", s)
4. Program to Remove an Element
s = {10, 20, 30}
[Link](20)
print("After removal:", s)
5. Program to Find Union of Two Sets
A = {1, 2, 3}
B = {3, 4, 5}
result = [Link](B)
print("Union:", result)
Output
{1, 2, 3, 4, 5}
6. Program to Find Intersection of Two Sets
A = {1, 2, 3}
B = {2, 3, 4}
result = [Link](B)
print("Intersection:", result)
Output
{2, 3}
7. Program to Find Difference of Two Sets
A = {1, 2, 3}
B = {2, 3, 4}
print("Difference A-B:", [Link](B))
Output
{1}
8. Program to Find Symmetric Difference
A = {1, 2, 3}
B = {3, 4, 5}
print("Symmetric Difference:", A.symmetric_difference(B))
Output
{1, 2, 4, 5}
9. Program to Check Membership in Set
s = {10, 20, 30}
num = int(input("Enter number: "))
if num in s:
print("Element found")
else:
print("Element not found")
10. Program to Find Maximum and Minimum in Set
s = {5, 10, 2, 8}
print("Maximum:", max(s))
print("Minimum:", min(s))
11. Program to Find Length of Set
s = {1, 2, 3, 4, 5}
print("Number of elements:", len(s))
12. Program to Remove Duplicates from List Using Set
list1 = [1, 2, 2, 3, 4, 4, 5]
s = set(list1)
print("List without duplicates:", s)
Output
{1, 2, 3, 4, 5}
13. Program to Check Subset
A = {1, 2}
B = {1, 2, 3, 4}
print("A is subset of B:", [Link](B))
Output
True
14. Program to Check Superset
A = {1, 2, 3, 4}
B = {1, 2}
print("A is superset of B:", [Link](B))
Output
True
15. Program to Iterate Through Set
s = {10, 20, 30, 40}
for i in s:
print(i)
Output
10
20
30
40
1. Can sets have duplicate values?
No. Duplicate values are automatically removed.
Example
s = {1,2,2,3}
print(s)
Output
{1,2,3}
2. Can we access set elements using index?
No, because sets are unordered.
3. Can sets contain sets?
No, because sets are mutable.
But they can contain frozensets.
Example
s = {1, 2, frozenset([3,4])}
Set:
A set is an unordered collection of unique elements in Python,
represented using curly braces {}.
Difference Between List, Tuple, Set, and
Dictionary
Feature List Tuple Set Dictionary
Definitio Ordered Ordered Unordered Collection of
n collection of immutable collection of key-value pairs
elements collection unique
elements
Syntax [ ] ( ) { } {key:value}
Example [10,20,30 (10,20,30 {10,20,30} {"A":10,"B":2
] ) 0}
Order Ordered Ordered Unordered Ordered (Python
3.7+)
Mutable Mutable Immutable Mutable Mutable
(can (cannot
change) change)
Duplicat Allowed Allowed Not allowed Keys not allowed
e Values duplicate (values
allowed)
Indexing Yes Yes No Access using
keys
Use Store Store fixed Store unique Store data with
Case ordered data data key-value
data relationship
Example of Each Data Type
1. List Example
numbers = [10, 20, 30, 40]
print(numbers[1])
Output
20
List allows modification
numbers[1] = 50
2. Tuple Example
numbers = (10, 20, 30, 40)
print(numbers[2])
Output
30
Tuple cannot be modified
numbers[1] = 50 # Error
3. Set Example
s = {10, 20, 30, 40}
print(s)
Sets automatically remove duplicates
s = {1,2,2,3}
print(s)
Output
{1,2,3}
4. Dictionary Example
student = {
"name":"Rahul",
"age":20,
"marks":85
}
print(student["name"])
Output
Rahul
Dictionary stores data using keys and values.
Quick Memory Trick (Students Like This)
Type Bracket Stores
List [ ] Ordered
values
Tuple ( ) Fixed
values
Set { } Unique
values
Dictiona {key:va Key-value
ry lue} pairs
Simple Real-Life Example
List
Shopping list
["Milk","Bread","Eggs"]
Tuple
Coordinates (fixed values)
(10, 20)
Set
Unique student IDs
{101,102,103}
Dictionary
Student record
{"Name":"Rahul","Marks":85}