🐍 Python for kids · Class 7 · Lesson 5 of 18 · Free
Tuples & Sets – Fixed Data and Unique Items
⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Create a tuple, read from it and unpack it into variables.
- Remove duplicates and test membership with a set.
- Compare two groups with set union, intersection and difference.
Your date of birth never changes, so it should be locked. The list of students in the chess club must have no repeats. Python has a tool for each job: a tuple for locked data and a set for unique items.
What is a tuple in Python?
A tuple is like a list, but it cannot be changed after it is made. We say it is immutable.
You write a tuple with round brackets ( ) instead of square brackets. Indexing and slicing work just like lists.
Use a tuple for data that belongs together and stays fixed, like a date or map point.
▶birthday = (15, "August", 2013)2print(birthday[1])3print("Year:", birthday[-1])4print(len(birthday), "parts")5print(birthday[0:2])
No variables yet.
▌✏️ Change this code and run it yourself
August Year: 2013 3 parts (15, 'August')
Why can't I change a tuple?
A tuple has no append, remove or sort. Trying to replace an item gives a TypeError.
This is a safety lock. If some code tries to change fixed data by mistake, Python stops it.
To "change" a tuple, you build a new one. The old tuple stays as it was.
Saffron colour: (255, 153, 51) Not allowed: 'tuple' object does not support item assignment New tuple: (255, 200, 150)
💡 try and except catch the error so the program can keep going. You will learn them properly in Lesson 17.
What is tuple unpacking in Python?
Unpacking takes the items of a tuple and puts each one in its own variable, in one line.
You already did this in Lesson 2 with return a, b. A function that returns two values really returns a tuple.
Unpacking also gives a neat way to swap two variables without a third one.
▶city = ("Jaipur", 26.9, 75.8)2name, lat, lon = city3print(name, "is at", lat, "N,", lon, "E")4a, b = 5, 95a, b = b, a6print("a =", a, "b =", b)
No variables yet.
▌✏️ Change this code and run it yourself
Jaipur is at 26.9 N, 75.8 E a = 9 b = 5
What is a set in Python?
A set is a collection with no duplicates. If you add the same item twice, it is stored once.
Sets use curly brackets, like {1, 2, 3}. An empty set is set(), because {} makes an empty dictionary.
Sets have no positions, so set[0] does not work. They are very fast at answering "is this item in here?"
▶rolls = [4, 2, 6, 2, 4, 4, 1]2faces = set(rolls)3print("Different faces:", faces)4print("How many:", len(faces))5faces.add(3)6faces.add(6)7print("After adding:", faces)8print("Rolled a 5?", 5 in faces)
No variables yet.
▌✏️ Change this code and run it yourself
Different faces: {1, 2, 4, 6}
How many: 4
After adding: {1, 2, 3, 4, 6}
Rolled a 5? False
How do I find common items with set intersection?
Sets can be compared like Venn diagrams from your Maths book.
a | b is the union: everything in either set. a & b is the intersection: items in both.
a - b is the difference: items in a but not in b. sorted() turns the result into a tidy list.
Both clubs: ['Meera', 'Zoya'] Any club: ['Aarav', 'Kabir', 'Meera', 'Rohan', 'Zoya'] Only chess: ['Aarav', 'Kabir']
💡 A set of words has no fixed order. sorted() makes a list, so the output is the same every time.
When should I use a list, tuple, set or dictionary?
Use a list when order matters and items may change, like a queue of students.
Use a tuple for a small group of fixed values, like (day, month, year).
Use a set when you only care about unique items or quick membership checks. Use a dictionary to look up values by a key.
['Asha', 'Bilal', 'Charu'] (26, 'January') 2 flavours 98xxxx1234
🤔 Predict the output
Think like Python: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
t = (10, 20, 30) x, y, z = t print(y + z)
Question 2. What does this print?
s = {3, 1, 3, 2, 1}
print(len(s))Question 3. What does this print?
a = {1, 2, 3, 4}
b = {3, 4, 5}
print(sorted(a - b), sorted(a & b))🧩 Your challenge
Two friends list the states they have visited. Read 4 states for each friend into two sets. Print the states both visited, and the total number of different states visited by either. Print words in sorted order.
Riya visited: Goa Riya visited: Kerala Riya visited: Gujarat Riya visited: Rajasthan Arjun visited: Kerala Arjun visited: Punjab Arjun visited: Goa Arjun visited: Sikkim Both visited: ['Goa', 'Kerala'] Different states in total: 6
⚠️ Common mistakes (and how to fix them)
❌ Wrong
days = ("Mon", "Tue")
days.append("Wed")AttributeError: 'tuple' object has no attribute 'append'
✅ Right
days = ("Mon", "Tue")
days = days + ("Wed",)
print(days)Tuples cannot change, so they have no append(). Build a new tuple with + instead, or use a list.
❌ Wrong
colours = {"red", "green", "blue"}
print(colours[0])TypeError: 'set' object is not subscriptable
✅ Right
colours = {"red", "green", "blue"}
print("red" in colours)
print(sorted(colours)[0])A set has no positions, so it cannot be indexed. Check membership with in, or sort it into a list first.
❌ Wrong
seen = {}
seen.add("Pune")AttributeError: 'dict' object has no attribute 'add'
✅ Right
seen = set()
seen.add("Pune")
print(seen){} makes an empty dictionary, not a set. An empty set must be written set().
📌 Remember
- A tuple is written with ( ) and cannot be changed after it is made.
- Tuples support indexing and slicing, and unpack into variables: a, b = b, a swaps.
- A set keeps only unique items and has no positions; set() makes an empty one.
- in is a fast way to check if an item is in a set.
- | is union, & is intersection and - is difference for sets.
- Pick list, tuple, set or dictionary by what your data needs.
Finished the quiz and the challenge?
Questions students ask
What is the difference between a list and a tuple in Python?
A list uses [ ] and can be changed. A tuple uses ( ) and is locked once it is made.
How do I remove duplicates from a list in Python?
Turn it into a set with set(my_list). If you need a list again, wrap it: sorted(set(my_list)).
Why is my set printed in a different order?
Sets do not keep any order. Python may print the items in any order, so use sorted() when order matters.
How do I make a tuple with one item?
Add a comma after the item, like ("Wed",). Without the comma, Python sees only a value in brackets.