A set represents unordered unique elements. Sets are created with {} or set().
Set properties
Sets have two important properties:
- Unordered: you cannot access a fixed position with an index.
- Unique: duplicate values are merged.
numbers = {1, 9, 2, 0, 0, 9}
print(numbers)The output order is not guaranteed, but the set keeps only one 0 and one 9.
KNOWLEDGE QUIZ
QUESTION
What is a core property of a set?
Set operations
Sets support common mathematical operations:
- Intersection:
s1 & s2ors1.intersection(s2). - Union:
s1 | s2ors1.union(s2). - Difference:
s1 - s2ors1.difference(s2).
s1 = {1, 2, 3}
s2 = {3, 4, 5}
print(s1 & s2)
print(s1 | s2)
print(s1 - s2)INTERACTIVE LAB
Live Venn diagram
ACCESSFREE
Choose an operation, then click a number to toggle its membership in A or B. The highlighted region and result update together.
A & B = {3, 4}Removing duplicates from a list
Sets are often used to remove duplicates:
lst = [1, 9, 2, 0, 0, 9]
lst = list(set(lst))
print(lst)One possible output:
[0, 1, 2, 9]After using set() to remove duplicates, element order may not stay the same. If you need to preserve order, you can learn a steadier approach later.
KNOWLEDGE QUIZ
QUESTION
What is {1, 2, 3} & {3, 4, 5}?
HANDS-ON PRACTICE
Check whether a sentence covers the full alphabet
Read a sentence containing lowercase letters and spaces, then decide whether it includes every letter from a to z.
- Read a sentence.
- Use a set to collect letters.
- Print Yes if all 26 letters are present; otherwise print No.