0% found this document useful (0 votes)
13 views3 pages

Practical_008 (1)

This lab manual provides practical exercises for programming with Python, focusing on set operations and methods. It includes resource requirements, descriptions of various set operations such as union, intersection, and methods like add, remove, and clear. Additionally, it contains example Python programs for creating sets, performing operations, and finding maximum/minimum values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Practical_008 (1)

This lab manual provides practical exercises for programming with Python, focusing on set operations and methods. It includes resource requirements, descriptions of various set operations such as union, intersection, and methods like add, remove, and clear. Additionally, it contains example Python programs for creating sets, performing operations, and finding maximum/minimum values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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 set1.union(set2) set1.difference(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.
set1.intersection(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 set.add(element)
remove() Removes a specific element (error set.remove(element)
if absent)
discard() Removes a specific element (no set.discard(element)
error)
clear() Removes all elements set.clear()
union() Returns the union of two sets set1.union(set2)
intersection() Returns common elements of two set1.intersection(set2)
sets
difference() Returns elements in one set but not set1.difference(set2)
other
symmetric_difference() Returns elements not common in set1.symmetric_difference(set2)
both sets
issubset() Checks if a set is a subset set1.issubset(set2)
issuperset() Checks if a set is a superset set1.issuperset(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:", set1.intersection(set2))

# Union
print("Union:", set1.union(set2))

# Difference
print("Difference (set1 - set2):", set1.difference(set2))
# Symmetric Difference
print("Symmetric Difference:", set1.symmetric_difference(set2))

# Clear a set
set1.clear()
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

You might also like