Python Set


1) Create a Set

Ask the user to enter 5 values (any type) and store them in a set. Print the set.


2) Set From List

Given the list:

lst = [1, 2, 2, 3, 3, 4]

Convert it to a set and print.


3) Add Single Item

Create an empty set and add one item using add(). Print the set.


4) Add Multiple Items

Start with a set and add several items one by one using a loop.


5) Remove an Item

Create a set with a few numbers and remove one using remove(). Print result.


6) Discard vs Remove

Create a set and try to remove an item that doesn’t exist using discard(), then using remove(). Observe behavior.


7) Loop Through Set

Create a set and print each element using a for loop.


8) Set Length

Create a set and print its length using len().


9) Check Membership

Ask user for a number and check if it’s in a set.


10) Clear Set

Create a set, use clear(), and print.


11) Union of Sets

Given two sets:

Print the union.


12) Intersection of Sets

Using the same sets above, print the intersection.


13) Difference of Sets

Find and print items in a that are not in b.


14) Symmetric Difference

Print items that are in either a or b but not both.


15) Copy a Set

Copy a set to another variable and print both.


16) Set without Duplicates

Ask user to enter values separated by spaces, split into a list, convert to a set, and print.

Example input:


17) Add Tuple to Set

Create a set and add a tuple inside it (allowed). Print result.


18) Remove All Even Numbers

Create a set of numbers from 1–10 and use a loop to remove even numbers.


19) Compare Subsets

Ask user to create two sets and check if one is a subset of the other.


20) Superset Check

Ask user for two sets and print whether the first is a superset of the second.


📝 Notes for Students

🔹 Sets are unordered collections of unique items. 🔹 Common methods/properties:

Operation
Method

Add item

add()

Remove item

remove() / discard()

Clear all

clear()

Union

union() or `

Intersection

intersection() or &

Difference

difference() or -

Symmetric diff

symmetric_difference() or ^

Check subset

issubset()

Check superset

issuperset()


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated