1. Tuples are ordered and unchangeable (immutable) collections of data. They can be thought of as immutable lists. They are written in round brackets:
1 2 3 4 5 |
my_tuple = (1, 2, True, "a string", (3, 4), [5, 6], None) print(my_tuple) my_list = [1, 2, True, "a string", (3, 4), [5, 6], None] print(my_list) |
Each tuple element may be of a different type (i.e., integers, strings, booleans, etc.). What is more, tuples can contain other tuples or lists (and the other way round).
2. You can create an empty tuple like this:
1 2 3 |
empty_tuple = () print(type(empty_tuple)) # outputs: <class 'tuple'> |
3. A one-element tuple may be created as follows:
1 2 |
one_elem_tuple_1 = ("one", ) # Brackets and a comma. one_elem_tuple_2 = "one", # No brackets, just a comma. |
If you remove the comma, you will tell Python to create a variable, not a tuple:
1 2 3 4 5 |
my_tuple_1 = 1, print(type(my_tuple_1)) # outputs: <class 'tuple'> my_tuple_2 = 1 # This is not a tuple. print(type(my_tuple_2)) # outputs: <class 'int'> |
4. You can access tuple elements by indexing them:
1 2 |
my_tuple = (1, 2.0, "string", [3, 4], (5, ), True) print(my_tuple[3]) # outputs: [3, 4] |
5. Tuples are immutable, which means you cannot change their elements (you cannot append tuples, or modify, or remove tuple elements). The following snippet will cause an exception:
1 2 |
my_tuple = (1, 2.0, "string", [3, 4], (5, ), True) my_tuple[2] = "guitar" # The TypeError exception will be raised. |
However, you can delete a tuple as a whole:
1 2 3 |
my_tuple = 1, 2, 3, del my_tuple print(my_tuple) # NameError: name 'my_tuple' is not defined |
6. You can loop through a tuple elements (Example 1), check if a specific element is (not)present in a tuple (Example 2), use the len()
function to check how many elements there are in a tuple (Example 3), or even join/multiply tuples (Example 4):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Example 1 tuple_1 = (1, 2, 3) for elem in tuple_1: print(elem) # Example 2 tuple_2 = (1, 2, 3, 4) print(5 in tuple_2) print(5 not in tuple_2) # Example 3 tuple_3 = (1, 2, 3, 5) print(len(tuple_3)) # Example 4 tuple_4 = tuple_1 + tuple_2 tuple_5 = tuple_3 * 2 print(tuple_4) print(tuple_5) |
7. You can also create a tuple using a Python built-in function called tuple()
. This is particularly useful when you want to convert a certain iterable (e.g., a list, range, string, etc.) to a tuple:
1 2 3 4 5 6 7 8 9 10 11 |
my_tuple = tuple((1, 2, "string")) print(my_tuple) my_list = [2, 4, 6] print(my_list) # outputs: [2, 4, 6] print(type(my_list)) # outputs: <class 'list'> tup = tuple(my_list) print(tup) # outputs: (2, 4, 6) print(type(tup)) # outputs: <class 'tuple'> |
By the same fashion, when you want to convert an iterable to a list, you can use a Python built-in function called list()
:
1 2 3 |
tup = 1, 2, 3, my_list = list(tup) print(type(my_list)) # outputs: <class 'list'> |
Exercise 1
What happens when you attempt to run the following snippet?
1 2 |
my_tup = (1, 2, 3) print(my_tup[2]) |
The program will print 3
to the screen.
Exercise 2
What is the output of the following snippet?
1 2 3 |
tup = 1, 2, 3 a, b, c = tup print(a * b * c) |
The program will print 6
to the screen. The tup
tuple elements have been “unpacked” in the a
, b
, and c
variables.
Exercise 3
Complete the code to correctly use the count()
method to find the number of duplicates of 2
in the following tuple.
1 2 3 4 |
tup = 1, 2, 3, 2, 4, 5, 6, 2, 7, 2, 8, 9 duplicates = # Write your code here. print(duplicates) # outputs: 4 |
Solution:
1 2 3 |
tup = 1, 2, 3, 2, 4, 5, 6, 2, 7, 2, 8, 9 duplicates = tup.count(2) print(duplicates) # outputs: 4 |
What is the output of the following code snippet?
1 2 3 4 |
tpl = ('a', 'b', 'c', 'd') del tpl[-3:-1] print(tpl) |
A TypeError
exception
- Tuples are immutable and do not support item deletion.
Ex. 5
Which of the following statements about lists and tuples are false?
(Select two answers)
- Both lists and tuples are indexed (with initial index equal to
0
). sort()
,pop()
, andappend()
are list methods and cannot be used with tuples.
Ex. 6
According to the following code snippet, which of the following statements generates an exception?
(Select two answers)
1 |
tpl = (3, 'xy', ['A', 'B'], 2, (9, 10)) |