0Pricing
Python Academy · Lesson

Tuples and Immutability

Understand tuples, their immutability, and when to prefer them over lists.

Tuples and Immutability is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Tuples are ordered, immutable sequences. Once created, their elements cannot be changed.

Creating Tuples

t = (1, 2, 3) or t = 1, 2, 3 (parens optional). A single-element tuple needs a trailing comma: t = (1,).
t = (1, 2, 3)
singleton = (42,)
print(t, singleton)

Tuple Immutability

t[0] = 99 raises TypeError. Tuples cannot be modified after creation. This is their defining feature.
t = (1, 2, 3)
# t[0] = 99  # TypeError
print(t[0])

Tuple Indexing and Slicing

t[0], t[-1], t[1:3] all work the same as lists. Tuples support all read operations.
t = (10, 20, 30)
print(t[1])
print(t[:2])

Tuple Packing and Unpacking

a, b = (1, 2) unpacks the tuple. This also works: a, b = b, a to swap variables without a temp.
a, b = 1, 2
a, b = b, a
print(a, b)

Tuples as Dictionary Keys

Because tuples are hashable, they can be dict keys: coords = {(0,0): 'origin'}. Lists cannot be dict keys.
coords = {(0, 0): 'origin', (1, 0): 'right'}
print(coords[(0, 0)])

Tuple vs List — When to Use

Use tuples for heterogeneous data with fixed structure (e.g., (name, age)). Use lists for homogeneous, variable-length data.
point = (3.0, 4.0)  # tuple: fixed structure
scores = [85, 90, 78]  # list: variable

Named Tuples

from collections import namedtuple; Point = namedtuple('Point', ['x','y']); p = Point(3,4); p.x gives 3.
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(3, 4)
print(p.x, p.y)

Tuple Methods

Tuples only have count() and index(). t.count(2) counts occurrences. t.index(2) returns first position.
t = (1, 2, 2, 3)
print(t.count(2))
print(t.index(2))

Tuple Performance

Tuples are slightly faster and use less memory than lists because Python can optimize immutable objects.
import sys
print(sys.getsizeof((1,2,3)))
print(sys.getsizeof([1,2,3]))

Converting Between List and Tuple

list(t) converts tuple to list. tuple(lst) converts list to tuple. Useful when you need mutability temporarily.
t = (1, 2, 3)
lst = list(t)
lst.append(4)
t2 = tuple(lst)
print(t2)

Quick Check

Why can tuples be used as dictionary keys but lists cannot?

Recap

Tuples: immutable, ordered, hashable. Use for fixed-structure data, dict keys, and multiple return values. namedtuple adds attribute access.

Keep Going

Great work! Move on to the next lesson to continue building your skills.

Frequently asked questions

Is the “Tuples and Immutability” lesson free?

Yes — the full text of “Tuples and Immutability” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Tuples and Immutability”?

Understand tuples, their immutability, and when to prefer them over lists. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Tuples and Immutability” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating and Accessing Lists
  2. Modifying Lists
  3. Tuples and Immutability
  4. Nested Lists and Iteration
← Back to Python Academy