Logo

Python: Tuples

Besides primitive types, Python has composite data types that store multiple values at once. A university student is described by a name, age, and GPA. A film has a title, release year, and rating. These groups of values are natural to store together.

A tuple is simpler than any other composite type. It stores several values in a strictly defined order. Once created, it cannot be changed.

A tuple works well for data that always belongs together.

student = ('Alice', 20, 4.8)       # name, age, GPA
film = ('Inception', 2010, 8.8)    # title, year, rating

Creating a tuple

A tuple is written in parentheses with values separated by commas.

point = (10, 20)
colors = ('red', 'green', 'blue')
mixed = (42, 'hello', 3.14)

A single-element tuple requires a trailing comma. Without it, Python treats the parentheses as grouping an expression.

single = (42,)    # a tuple with one element
not_tuple = (42)  # just the number 42

The parentheses are optional. Python recognizes a tuple by the commas.

point = 10, 20
print(type(point))  # => <class 'tuple'>

Accessing elements

Tuple elements are numbered from zero. They are accessed by index.

point = (10, 20)
print(point[0])  # => 10
print(point[1])  # => 20

Tuples are immutable

A tuple cannot be modified after creation. Attempting to replace an element raises an error.

point = (10, 20)
point[0] = 5  # TypeError: 'tuple' object does not support item assignment

Immutability is built into tuples deliberately. No matter where a tuple is passed, its data stays the same.

Unpacking

Tuple elements can be assigned to multiple variables at once.

point = (10, 20)
x, y = point

print(x)  # => 10
print(y)  # => 20

Python matches values to variables in order. The number of variables must equal the number of elements.

Instructions

Two cities lie on the same road. Each is described by a tuple with a name and a position in kilometers from the start of the route:

city_a = ('Moscow', 10)
city_b = ('Saint Petersburg', 644)

Calculate the distance between the cities and print the result in this format:

From: Moscow
To: Saint Petersburg
Distance: 634 km

Tips

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Found a bug? Have something to add? Pull requests are welcome!