Prof. Andrea Gallegati
A tuple is a sequence of values.
These values:
Tuples are a lot like lists.
The important difference is that tuples are immutable.
Syntactically, it is a comma-separated list of values:
t = 'a', 'b', 'c', 'd', 'e'
Although not necessary, it's common to enclose them into parentheses:
t = ('a', 'b', 'c', 'd', 'e')
To create a tuple with a single element, include a final comma:
t1 = 'a',
type(t1)
tuple
A single value within parentheses is not a tuple:
t2 = ('a')
type(t2)
str
... otherwise, use the built-in function tuple
.
Without argument, it creates an empty tuple:
t = tuple()
t
()
or with a sequence argument (string, list or tuple)
t = tuple('lupins')
t
('l', 'u', 'p', 'i', 'n', 's')
... avoid using tuple
as a variable name.
Most list operators also work on tuples.
The square bracket operator indexes an element:
t = ('a', 'b', 'c', 'd', 'e')
t[0]
'a'
The slice operator selects a range of elements:
t[1:3]
('b', 'c')
When trying to modify a tuple element, we get an error:
t[0] = 'A'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-9-d8f50996a189> in <module>() ----> 1 t[0] = 'A' TypeError: 'tuple' object does not support item assignment
Tuples are immutable.
We can replace a tuple with another:
t = ('A',) + t[1:]
t
('A', 'b', 'c', 'd', 'e')
t
refer to itrelational operators work with tuples and other sequences.
(0, 1, 2) < (0, 3, 4)
True
(0, 1, 2000000) < (0, 3, 4)
True
Subsequent elements are then not considered.
It's so useful to swap the values between two variables.
temp = a
a = b
b = temp
we have to use a temporary variable.
This is cumbersome: tuple assignment is more elegant!
a, b = b, a
Variables on the left must be the same number of values on the right:
a, b = 1, 2, 3
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-19-f840016b8414> in <module>() ----> 1 a, b = 1, 2, 3 ValueError: too many values to unpack (expected 2)
The right side can be any kind of sequence (string, list or tuple).
addr = 'monty@python.org'
uname, domain = addr.split('@')
this splits an email address into
split
returns a list with two elements:
uname
'monty'
domain
'python.org'
Strictly speaking, functions can return one value only.
If that value is a tuple, same effect as returning multiple values!
Dividing two integers and computing:
it's inefficient to compute
x/y
and then
x%y
divmod
built-in function, takes two arguments and returns a tuple of two values:
t = divmod(7, 3)
t
(2, 1)
we can store the result as a tuple
quot, rem = divmod(7, 3)
quot
2
rem
1
or use tuple assignment to store them separately.
def min_max(t):
return min(t), max(t)
another example, making use of these built-in functions finding:
max
: largest element of a sequencemin
: smallest element of a sequenceit then returns both as a tuple of two values.
This chapter presents one more built-in type, the tuple.
Then shows how
work together.
One note: there is no consensus on how to pronounce “tuple”: