Module 3.2.3
Module 3.2.3
Prof. Sarju S
13 November 2024
Module 3
Page 2
Module 3
► SELECTION AND ITERATION USING PYTHON:- if-else, elif, for loop, range, while loop.
► SEQUENCE DATA TYPES IN PYTHON - list, tuple, set, strings, dictionary, Creating and
using Arrays in Python (using Numpy library).
Page 3 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Python Set
Page 4
What is a Set?
Page 5 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Creating a Set
Page 6 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Creating an Empty Set
► In Python, curly braces {} are used to create both dictionaries and sets.
► However, when you write {} alone, Python interprets it as an empty dictionary rather
than an empty set.
# Empty set
empty_set = set()
print(type(empty_set)) # Output: <class 'set'>
Page 7 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Basic Set Operations
Page 8
Basic Set Operations
Operation Explanation Example
A = {1, 2, 3}
Add (add()) Adds a single specified element to the set. A.add(4)
Output: {1, 2, 3, 4}
A = {1, 2, 3}
Adds multiple elements from another set or iterable (like a list) to
Update (update()) A.update([4, 5, 6])
the set.
Output: {1, 2, 3, 4, 5, 6}
A = {1, 2, 3}
Removes a specific element from the set; raises an error if the
Remove (remove()) A.remove(2)
element is not found.
Output: {1, 3}
A = {1, 2, 3}
Removes a specific element from the set, but does not raise an
Discard (discard()) A.discard(4)
error if it’s missing.
Output: {1, 2, 3}
A = {1, 2, 3}
A.pop()
Pop (pop()) Removes and returns an arbitrary element from the set.
Output: {2, 3} (removes one item
randomly)
A = {1, 2, 3}
Clear (clear()) Removes all elements from the set, making it empty. A.clear()
Output: set() (an empty set)
Page 9 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Iterating through a Set
Note: The order in which items are printed may differ when you run the program because sets do
not maintain a fixed order.
Page 10 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Common Set Operations
Page 11
Common Set Operations
A = {1, 2, 3}
B = {3, 4, 5}
C = A | B # or A.union(B)
print(C) # Output: {1, 2, 3, 4, 5}
Page 12 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Common Set Operations
A = {1, 2, 3}
B = {3, 4, 5}
C = A & B # or A.intersection(B)
print(C) # Output: {3}
Page 13 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Common Set Operations
A = {1, 2, 3}
B = {3, 4, 5}
C = A - B # or A.difference(B)
print(C) # Output: {1, 2}
Page 14 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Common Set Operations
A = {1, 2, 3}
B = {3, 4, 5}
C = A ^ B # or A.symmetric_difference(B)
print(C) # Output: {1, 2, 4, 5}
Page 15 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Why Use Sets?
► Set Operations: If you need to perform union, intersection, or difference, sets make
these tasks straightforward.
Page 16 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Class Room Exercises
Page 17
Class Room Exercises
► Problem: A store wants to track the unique customer IDs of people who visit
each day. Each customer has a unique ID, and some customers visit more
than once a day. Create a program that adds customer IDs to a set, then
displays the unique customers for the day.
► Instructions
► Use add() to add customer IDs to a set.
► Display the set to see all unique customer IDs.
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/set-unique-customer.py
Page 18 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Class Room Exercises
► Instructions
► Create a set for the current inventory.
► Use update() to add new items from a shipment.
► Display the updated inventory.
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/set-update-inventory.py
Page 19 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Class Room Exercises
► Problem: The store wants to remove specific items from the inventory when
they are out of stock. Use remove() or discard() to handle this.
► Instructions
► Create a set with current stock items.
► Use remove() to remove an item that’s out of stock.
► Try to use discard() to remove another item (including one that might not exist).
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/set-remove-item.py
Page 20 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Class Room Exercises
► Instructions
► Add student IDs to a set.
► Use pop() to randomly remove and display a winner.
► Ensure the winner is removed from the set.
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/set-random-winner.py
Page 21 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Class Room Exercises
► Problem: A store has a set of discount codes, but all codes have expired, so
they need to be removed. Write a program to clear the discount codes.
► Instructions
► Create a set of discount codes.
► Use clear() to remove all codes.
► Display the set to confirm it’s empty.
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/set-discount-codes.py
Page 22 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Class Room Exercises
► Problem: A store has a set of discount codes, but all codes have expired, so
they need to be removed. Write a program to clear the discount codes.
► Instructions
► Create a set of discount codes.
► Use clear() to remove all codes.
► Display the set to confirm it’s empty.
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/set-discount-codes.py
Page 23 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Class Room Exercises
► Problem: A warehouse has a set of products ready for shipment, but one
product fails the quality check and must be removed. Use discard() to
remove it safely without raising an error if it’s missing.
► Instructions
► Create a set of product IDs.
► Use discard() to remove a failed product.
► Print the set to see the updated list of products.
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/set-quality-check.py
Page 24 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Thank You
Prof. Sarju S
Department of Computer Science and Engineering
St. Joseph’s College of Engineering and Technology, Palai (Autonomous)
[email protected]
Page 25 Disclaimer - This document contains images/texts from various internet sources. Copyright belongs to the respective content creators.
Document is compiled exclusively for study purpose and shall not be used for commercial purpose.