🟢 Beginner · Lesson 13
Tuples and Sets in Python
Tuples and Sets
Besides lists, Python has two more collections: tuple (ordered but cannot be changed) and set (unordered, only unique items). Each has its own job.
Tuple — Ordered & Fixed
A tuple uses round brackets ( ). It is immutable — once created, items cannot be changed. Use it for data that should not change, like coordinates or fixed config.
Python – tuple.py
point = (10, 20) print(point[0]) # access like a list print(len(point)) # point[0] = 5 -> ERROR: tuples cannot change
10
2
Set — Unique & Unordered
A set uses curly braces { } and automatically removes duplicates. It has no order, so no indexing.
Python – set.py
nums = {1, 2, 2, 3, 3, 3}
print(nums) # duplicates gone
nums.add(4)
print(nums){1, 2, 3}
{1, 2, 3, 4}
Program 1: Tuple Basics
Python – tbasics.py
colors = ("red", "green", "blue")
for c in colors:
print(c)
print("Total:", len(colors))red
green
blue
Total: 3
Program 2: Returning Multiple Values
Tuples are great for returning more than one value from a function.
Python – minmax.py
def min_max(nums):
return min(nums), max(nums) # returns a tuple
low, high = min_max([5, 2, 9, 1])
print("Low:", low, "High:", high)Low: 1 High: 9
The function returns two values as a tuple; we unpack them into low and high.
Program 3: Remove Duplicates with Set
Python – dedupe.py
marks = [85, 90, 85, 78, 90, 100]
unique = set(marks)
print("Unique marks:", unique)
print("How many unique:", len(unique))Unique marks: {100, 85, 90, 78}
How many unique: 4
Converting a list to a set instantly removes repeated values.
Program 4: Set Operations
Python – setops.py
maths = {"Aman", "Riya", "Karan"}
science = {"Riya", "Karan", "Sara"}
print("Both subjects :", maths & science) # intersection
print("Any subject :", maths | science) # union
print("Only maths :", maths - science) # differenceBoth subjects : {'Riya', 'Karan'}
Any subject : {'Aman', 'Riya', 'Karan', 'Sara'}
Only maths : {'Aman'}
&= in both (intersection).|= in either (union).-= in first but not second (difference).
List vs Tuple vs Set
| Feature | List [ ] | Tuple ( ) | Set { } |
|---|---|---|---|
| Ordered | Yes | Yes | No |
| Changeable | Yes | No | Yes (add/remove) |
| Duplicates | Allowed | Allowed | Not allowed |
| Indexing | Yes | Yes | No |
Common Mistakes
- Trying to change a tuple item (it is immutable).
- Expecting a set to keep order or allow indexing.
- Writing a 1-item tuple as
(5)— it must be(5,)with a comma.
Practice Tasks
- Store the days of the week in a tuple and print each.
- Take a list with duplicates and print only unique values.
- Find common elements between two lists using sets.
- Write a function that returns both sum and average as a tuple.
Summary
- Tuple: ordered, immutable, allows duplicates — use
( ). - Set: unordered, unique items, no indexing — use
{ }. - Set operations:
&intersection,|union,-difference.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.