PyForge · Python practice library
All topics

Topic 07 of 17

Tuples & Sets

Immutable tuples and unique-element sets — when and why to use each.

Tuple packing/unpacking
Set algebra
Frozenset
Hashability

Basic

1 program

Program 1

Tuple unpacking

Basic

Assign multiple values at once.

python
person = ("Alice", 30, "Engineer")
name, age, job = person
print(name, age, job)

Intermediate

2 programs

Program 1

Set operations

Intermediate

Union, intersection, difference.

python
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print("Union:", a | b)
print("Intersect:", a & b)
print("Diff:", a - b)
print("Sym diff:", a ^ b)

Program 2

Unique words count

Intermediate

Use a set to count distinct words.

python
text = "to be or not to be that is the question"
unique = set(text.split())
print("Unique words:", len(unique))

Advanced

1 program

Program 1

Find common elements

Advanced

Intersect any number of lists.

python
lists = [[1,2,3,4], [2,3,5], [2,3,7,9]]
common = set(lists[0]).intersection(*lists[1:])
print(common)