Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner15 min

Visual reference

Immutable data — values that do not change in placeImmutable data — values that do not change in place
GPS coordinates — lat and lng as a fixed pairGPS coordinates — lat and lng as a fixed pair
RGB color model — three channel valuesRGB color model — three channel values

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.

python
t = (10, 20, 30)

# Parentheses optional
t2 = 10, 20, 30

Both create the same tuple.

Ordered collection (immutable)
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

python
t = (10, "RS", 34.67, True, 2 + 3j)

print(t)
print(type(t))
text
(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

python
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.

Positive and negative indexing
python
t = (10, "RS", 34.67, True, 2 + 3j)

print(t[0])
print(t[-1])
text
10
(2+3j)

Chapter 6 — Slicing

Slicing works like lists — a slice returns a new tuple.

python
t = (10, "RS", 34.67, True, 2 + 3j)

print(t[1:4])
text
('RS', 34.67, True)

Reverse a tuple

python
print(t[::-1])
text
((2+3j), True, 34.67, 'RS', 10)
[::-1] returns a reversed tuple

Chapter 7 — Immutability

This is the defining property of tuples.

python
t = (10, 20, 30)

# t[0] = 100
# TypeError: 'tuple' object does not support item assignment
Read-only after creation

To “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

python
a = (10)

print(type(a))
text
<class 'int'>

Parentheses here only group an expression — like in math.

Correct one-element tuple

python
a = (10,)

print(type(a))
text
<class 'tuple'>

Your notes also allow:

python
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

python
t = ()

print(t)
print(len(t))
text
()
0

Also: 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

python
lst = [100, "RS", 34.56]

t = tuple(lst)

print(t)
text
(100, 'RS', 34.56)

Tuple → list

python
t = (100, "RS", 34.56)

lst = list(t)

print(lst)
text
[100, 'RS', 34.56]

From a string

python
print(tuple("NISSAN"))
text
('N', 'I', 'S', 'S', 'A', 'N')

Use the playground to uncomment t[0] = 100 and read the TypeError.

Practice exercises

1. Create a tuple

python
t = (1, 2, 3)
print(t)

2. One-item tuple

python
x = 42
t = (x,)
print(type(t))

3. List → tuple → list

python
lst = [1, 2, 3]
t = tuple(lst)
print(list(t))

Quick knowledge check

  1. Why use a tuple instead of a list?
    Because values should stay fixed (immutable).

  2. What is the type of (10)?
    int — use (10,) for a tuple.

  3. 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 an int; (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…