Lesson 10
tuple — Immutable Ordered Collection
Tuples are ordered collections like lists, but their contents cannot be changed after creation. Ideal for fixed data such as coordinates, RGB colors, and database records.
Visual reference
What you will learn
- Create tuples using parentheses or commas
- Store mixed types and duplicate values
- Access elements with indexing and slicing
- Understand tuple immutability
- Avoid the
(10)vs(10,)mistake
Chapter 1 — What is a tuple?
A tuple is an ordered collection that stores multiple values in one object.
Purpose (your notes): store multiple values (same type, different type, or both) with unique and duplicates as constant / fixed values. In practice, tuples often represent data that should not change accidentally.
Biggest difference from list: a tuple is immutable — once created, its elements cannot be replaced via assignment.
Syntax: varname = (Val1, Val2, ..., Val-n) — parentheses are optional when commas make the meaning clear.
t = (10, 20, 30)
# Parentheses optional
t2 = 10, 20, 30Both create the same tuple.
| Property | tuple behavior |
|---|---|
| Order | Insertion order preserved |
| Index / slice | Yes (+ve and −ve) |
| Mutability | Immutable — no item assignment |
| Duplicates | Allowed |
Note from your notes: tuple behavior is almost the same as list, except list is mutable and tuple is immutable.
Chapter 2 — Why use tuples?
Use tuples when values should stay fixed for the life of the program (or at least should not be edited in place).
| Use case | Example |
|---|---|
| GPS coordinates | (17.3850, 78.4867) |
| RGB color | (255, 128, 0) |
| Database row | (101, "Alice", "HR") |
| Screen size | (1920, 1080) |
Each tuple is one logical object — like a laminated document (tuple) vs an editable notebook (list).
Chapter 3 — Creating tuples
Mixed data types
t = (10, "RS", 34.67, True, 2 + 3j)
print(t)
print(type(t))(10, 'RS', 34.67, True, (2+3j))
<class 'tuple'>A tuple can hold int, str, float, bool, complex, and even another tuple (nested tuples).
Empty and non-empty
t1 = ()
t2 = tuple()
print(t1, len(t1))
t3 = (10, 20, 30, 20) # duplicates OK
print(t3, len(t3))Chapter 4 — Lists vs tuples
| Feature | list |
tuple |
|---|---|---|
| Syntax | [] |
() or commas |
| Ordered | Yes | Yes |
| Indexing | Yes | Yes |
| Slicing | Yes | Yes |
| Mutable | Yes | No |
| Duplicates | Yes | Yes |
Memory trick: list → editable notebook · tuple → laminated document.
Chapter 5 — Indexing
Tuples support positive and negative indexing, same rules as lists.
t = (10, "RS", 34.67, True, 2 + 3j)
print(t[0])
print(t[-1])10
(2+3j)Chapter 6 — Slicing
Slicing works like lists — a slice returns a new tuple.
t = (10, "RS", 34.67, True, 2 + 3j)
print(t[1:4])('RS', 34.67, True)Reverse a tuple
print(t[::-1])((2+3j), True, 34.67, 'RS', 10)Chapter 7 — Immutability
This is the defining property of tuples.
t = (10, 20, 30)
# t[0] = 100
# TypeError: 'tuple' object does not support item assignmentTo “change” a tuple, build a new one (or convert to list, edit, then tuple(...) again).
Chapter 8 — The single-element tuple rule
A common beginner mistake:
Not a tuple
a = (10)
print(type(a))<class 'int'>Parentheses here only group an expression — like in math.
Correct one-element tuple
a = (10,)
print(type(a))<class 'tuple'>Your notes also allow:
a = 10,
print(a, type(a)) # (10,) <class 'tuple'>| Expression | Type |
|---|---|
(10) |
int |
(10,) |
tuple |
10, |
tuple |
Rule: the comma creates the tuple, not the parentheses.
Chapter 9 — Empty tuple
t = ()
print(t)
print(len(t))()
0Also: t = tuple() — same empty tuple, length 0.
Chapter 10 — Converting between list and tuple
From iterable: tuple(object) when object has multiple values (list, string, range, etc.).
List → tuple
lst = [100, "RS", 34.56]
t = tuple(lst)
print(t)(100, 'RS', 34.56)Tuple → list
t = (100, "RS", 34.56)
lst = list(t)
print(lst)[100, 'RS', 34.56]From a string
print(tuple("NISSAN"))('N', 'I', 'S', 'S', 'A', 'N')Use the playground to uncomment t[0] = 100 and read the TypeError.
Practice exercises
1. Create a tuple
t = (1, 2, 3)
print(t)2. One-item tuple
x = 42
t = (x,)
print(type(t))3. List → tuple → list
lst = [1, 2, 3]
t = tuple(lst)
print(list(t))Quick knowledge check
-
Why use a tuple instead of a list?
Because values should stay fixed (immutable). -
What is the type of
(10)?
int— use(10,)for a tuple. -
Does a tuple support slicing?
Yes.
Summary
- A tuple is an ordered, immutable collection.
- Insertion order and duplicates are allowed.
- Commas create tuples; parentheses are optional except for clarity.
(10)is anint;(10,)is a one-element tuple.- Indexing and slicing work; item assignment does not.
Next lesson: set — unique unordered collection (set operations and duplicate removal).
Loading playground…