PyComplete Python Course
Beginner 30 min

Module 07 β€” Tuples

An ordered collection that, once created, can never be changed.

Prerequisite: Module 06 β€” Lists

After this lesson, you will be able to

  • Create and index tuples using parentheses
  • Explain why tuples are immutable and when that matters
  • Use tuples to return more than one value from a function

Concept & Syntax

A tuple looks and behaves almost exactly like a list β€” it's ordered, and you can index and slice it the same way β€” with one crucial difference: once created, a tuple cannot be changed. No append, no item assignment, no remove.

That might sound like a downgrade, but immutability is a feature: it signals "this data shouldn't change," lets Python optimize storage slightly, and β€” most usefully β€” is exactly what functions use under the hood whenever they return more than one value.

Syntax
point = (3, 4)
x, y = point   # unpacking
point[0]       # 3

Examples

Creating and indexing a tuple Very Easy

Creating and indexing a tuple
point = (3, 4)
print(point[0], point[1])
Output3 4

Tuples use the same indexing rules as lists β€” parentheses instead of square brackets is the only visual difference.

Tuples are immutable Easy

Tuples are immutable
colors = ("red", "green")
try:
    colors[0] = "blue"
except TypeError as e:
    print("Can't do that:", e)
OutputCan't do that: 'tuple' object does not support item assignment

Attempting to change an item raises TypeError β€” this is what 'immutable' means in practice.

Returning multiple values Real World

Returning multiple values
def min_max(numbers):
    return min(numbers), max(numbers)
 
lowest, highest = min_max([4, 9, 1, 7])
print(lowest, highest)
Output1 9

return a, b actually returns a single tuple (a, b), which you can immediately unpack into two variables.

Visual Explanation

point = (3, 4) β€” a fixed pair of coordinates3041
A tuple's contents are locked in place once created.
πŸ’‘ Why does this work?

Read the concept explanation above again slowly, line by line β€” every keyword there maps directly onto a line of code in the examples. If it still doesn't click, re-run the traced example (if this lesson has one) one step at a time.

πŸ” What happens internally? (advanced)

Under the hood, Python compiles your source into bytecode and runs it on a virtual machine (the CPython interpreter). Variables are names in a namespace dictionary pointing at objects in memory β€” which is exactly what the memory panel in the tracer above is showing you.

Common Mistakes

βœ— Common mistake 1

A single-item tuple needs a trailing comma β€” without it, Python just sees ordinary parentheses.

βœ— Avoid this
This raises prints <class 'int'>, not a tuple β€” a silent bug, not a crash
single = (5)
print(type(single))
βœ“ Better approach
Fixed
single = (5,)
print(type(single))
βœ“ Best practices
  • Use a tuple for data that represents a fixed structure, like (x, y) coordinates or (r, g, b) colors.
  • Use tuple unpacking (a, b = my_tuple) instead of manual indexing where possible.
  • Reach for a list instead if you expect to add, remove, or reorder items.

Exercise & Challenge

Exercise 1 β€” Predict the output

Given this code
dimensions = (1920, 1080)
width, height = dimensions
print(f"{width}x{height}")

What does this print?

πŸ† Challenge

Write a function circle_stats(radius) that returns a tuple of (circumference, area), then unpack and print both, rounded to 2 decimal places.

  • Use pi = 3.14159
  • Return both values as a tuple
  • Unpack the result into two named variables
πŸ’‘ Need a hint?

circumference = 2 * pi * radius, area = pi * radius ** 2

πŸ” Show a sample solution
Python
def circle_stats(radius):
    pi = 3.14159
    return 2 * pi * radius, pi * radius ** 2
 
circ, area = circle_stats(5)
print(round(circ, 2), round(area, 2))

Quiz

Answer every question, then submit to see your score and explanations.

1. What is the main difference between a list and a tuple?

Tuples can hold more data types
Tuples are immutable, lists are mutable
Lists use parentheses
There's no real difference
Immutability is the defining difference.

2. How do you write a tuple with a single item, 5?

(5)
(5,)
[5]
tuple(5)
A trailing comma is required, or Python treats (5) as just the integer 5.

3. What happens if a function does `return a, b`?

A syntax error
It returns two separate values with no connection
It returns a single tuple (a, b)
Only a is returned
Comma-separated return values are packed into one tuple automatically.

Summary

  • Tuples are ordered like lists but cannot be modified after creation.
  • A single-item tuple needs a trailing comma: (5,).
  • Functions commonly use tuples to return multiple values at once.

Related lessons

Saved privately in your browser β€” no account needed.