Editor settings

General settings

Save settings

Learn to program with Python

Python Tuples #

In Python, a tuple is similar to a list, but tuples have a property called immutability, meaning that once a tuple is defined, the data inside it cannot be changed.

We can define a tuple using parentheses (), which is different from creating a list with square brackets []:

list_of_numbers = [1, 2, 3] # Define a list
tuple_of_numbers = (1, 2, 3) # Define a tuple
print(tuple_of_numbers)

We can also create a tuple from a list by calling the function tuple(), which takes a list as an argument:

my_list = ["A", "B", "C", "D"]
my_tuple = tuple(my_list)
print(my_tuple) # Output: ('A', 'B', 'C', 'D')

Because of immutability, we cannot modify the data inside a tuple like we can with a list. If we try to modify a tuple, we'll get a TypeError: 'tuple' object does not support item assignment error:

tuple_of_numbers = (1, 2, 3)
tuple_of_numbers[2] = 5 # TypeError: 'tuple' object does not support item assignment

We can use the len() function to get the length of a tuple, just like with a list or string:

t = (1, 2, 3)
print(len(t)) # 3

Accessing Elements in a Tuple We can access elements in a tuple just like with a list:

fruits = ("apple", "banana", "orange", "apple")
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[-2]) # orange

Exercise #

  • Change the code so that numbers is a tuple instead of a list.
Output will be displayed here