Programming with ‘Python’ Practical: 08 Lab Manual (Solved)
Resource required (additional)
Sr. Remark if
Name of Resource Broad Specification Quantity
No. any
1 Computer System Intel i3, 4 GB RAM
For All
2 Operating System Windows/Linux 20
Experiments
3 Development Software Python IDE And VS Code
Practical related Questions
1. Describe the various set operations
Set Operations in Python: Difference (- or difference())
Union (| or union()) Returns elements present in the first set
Combines elements from two sets, but not in the second.
removing duplicates. Syntax: set1 - set2 or
Syntax: set1 | set2 or [Link](set2) [Link](set2)
Example: Example:
a = {1, 2, 3} a = {1, 2, 3}
b = {3, 4, 5} b = {3, 4, 5}
print(a | b) # Output: {1, 2, 3, 4, 5} print(a - b) # Output: {1, 2}
Intersection (& or intersection()) Symmetric Difference (^ or
Returns common elements between two symmetric_difference())
sets. Returns elements present in either of the
Syntax: set1 & set2 or sets but not in both.
[Link](set2) Syntax: set1 ^ set2 or
Example: set1.symmetric_difference(set2)
a = {1, 2, 3} Example:
b = {3, 4, 5} a = {1, 2, 3}
print(a & b) # Output: {3} b = {3, 4, 5}
print(a ^ b) # Output: {1, 2, 4, 5}
Jamia Polytechnic Akkalkuwa Page No:01 Prepared by: Sayyed Waliullah
Programming with ‘Python’ Practical: 08 Lab Manual (Solved)
2. Describe the various methods of set
Method Description Syntax
add() Adds an element to the set [Link](element)
remove() Removes a specific element (error [Link](element)
if absent)
discard() Removes a specific element (no [Link](element)
error)
clear() Removes all elements [Link]()
union() Returns the union of two sets [Link](set2)
intersection() Returns common elements of two [Link](set2)
sets
difference() Returns elements in one set but not [Link](set2)
other
symmetric_difference() Returns elements not common in set1.symmetric_difference(set2)
both sets
issubset() Checks if a set is a subset [Link](set2)
issuperset() Checks if a set is a superset [Link](set2)
Exercise:
1. Write a Python program to create a set, add member(s) in a set and remove one item
from set.
# Creating a set
my_set = {1, 2, 3}
print("Initial Set:", my_set)
# Adding member to the set
my_set.add(4)
print("After Adding 4:", my_set)
# Removing an item from the set
my_set.remove(2)
print("After Removing 2:", my_set)
2. Write a Python program to perform following operations on set: intersection of sets,
union of sets, set difference, symmetric difference, clear a set.
# Create two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
Jamia Polytechnic Akkalkuwa Page No:02 Prepared by: Sayyed Waliullah
Programming with ‘Python’ Practical: 08 Lab Manual (Solved)
# Intersection
print("Intersection:", [Link](set2))
# Union
print("Union:", [Link](set2))
# Difference
print("Difference (set1 - set2):", [Link](set2))
# Symmetric Difference
print("Symmetric Difference:", set1.symmetric_difference(set2))
# Clear a set
[Link]()
print("After Clearing set1:", set1)
3. Write a Python program to find maximum and the minimum value in a set.
# Create a set of numbers
num_set = {10, 25, 45, 5, 78, 30}
# Find and display maximum and minimum values
print("Maximum Value:", max(num_set))
print("Minimum Value:", min(num_set))
4. Write a Python program to find the length of a set.
# Create a set
my_set = {1, 3, 5, 7, 9}
# Find and display the length of the set
print("Length of the Set:", len(my_set))
Jamia Polytechnic Akkalkuwa Page No:03 Prepared by: Sayyed Waliullah