Register to get access to free programming courses with interactive exercises

Corteges Python Basics

We have encountered strings, numbers, and boolean values up to this point. These are all simple data, so they do not include data of other types.

In programming, you often have to work with several data types. It is why many programming languages have composite types which can include data of other types.

Composite types allow you to describe entities of any complexity and therefore are very useful. In this lesson, we will learn about tuples, one of the complex data types in Python.

What are tuples

A tuple is several values written and separated by commas. Here are a few examples:

rgb_colour = (255, 127, 64)
name_and_age = ('Bob', 42)
three_booleans = (True, False, True)
two_pairs_of_numbers = ((1, 2), (3, 4))

In this case, the tuple can consist of only one element:

tuple = (42,)  # We put a comma to indicate a tuple

Note the comma; we must place it even if the tuple has only one element.

Now let us look at the example without the comma. In this case, Python will think that we want to calculate an arithmetic expression with parentheses:

not_a_tuple = (42)  # No comma, so Python does not consider it a tuple

How to change tuples

One of the peculiarities of tuples is that they are immutable data types. It means that you cannot change a tuple after its creation. To add a new value, you need to create a new tuple.

If we try to change the tuple, we get this error:

name_and_age = ('Bob', 42)
name_and_age[0] = 'Alice'
# Traceback (most recent call last)
# TypeError: 'tuple' object does not support item assignment
name_and_age # ('Bob', 42)

How to work with tuples

Tuples are useful when you need to return multiple values at once. Take a function that returns two values simultaneously: the result of integer division and the remainder of the division.

In the code, it looks like this:

def div_mod(a, b):
    quotient = a // b
    modulo = a % b
    return (quotient, modulo)

div_mod(13, 4)  # (3, 1)

How to extract values from tuples

Above, we learned how to create tuples. Now, let us try to extract values from them. To do this, refer to the element of the tuple by index:

name_and_age = ('Bob', 42)

name_and_age[0]  # 'Bob'
name_and_age[1]  # 42

The tuple also has a length, which we can obtain with the len() function:

tuple = (42,)  # (42,)
len(tuple)     # 1
pair = (1, 2)  # (1, 2)
len(pair)      # 2

How to separate values from tuples

Often, tuples contain values of different types. It is difficult to remember what index each value is denoted by.

To make things easier, you can disassemble the motorcade:

name_and_age = ('Bob', 42)

(name, age) = name_and_age
name  # 'Bob'
age   # 42

We can also call this process destructuring or unpacking.

Using this method, you can also get and immediately parse the values that the function returns:

(quotient, modulo) = div_mod(13, 4)

Accordingly, we can disassemble a tuple with one element as follows:

(a,) = (42,)
a  # 42

If you do not put a, after the variable name, there will be no syntax error. But we will write the tuple to the variable in its entirety. We do not unpack anything. It is a logical error, so we will not get the expected result.

How to assign tuples to multiple variables

Tuples are easy to assemble and disassemble, so doing this and assigning them to multiple variables is convenient. Take a look:

(a, b, c) = (1, 2, 3)
a  # 1
b  # 2
c  # 3

You can exchange values by assigning them to two variables. It is what it looks like:

a = 100
b = 'foo'

(a, b) = (b, a)
a  # 'foo'
b  # 100

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY