A tuple is a sequence of values much like a list. The values stored in a tuple can be any type, and each item has an index indicating its position in the list. The first item in a tuple is at index 0, the second at index 1, and so on. The important difference between list and tuple is that tuples are immutable.
A tuple is a comma-separated list of values inside parentheses (). The parentheses are optional.
>>> t = 'a', 'b', 'c', 'd', 'e'
>>> t = ('a', 'b', 'c', 'd', 'e')
There are two ways to construct an empty tuple. You can construct an empty tuple by having () with no values in them
>>> t = ()
>>> print(t)
()
To construct an empty tuple you can also use the built-in function tuple like this
>>> t = tuple()
>>> print(t)
()
To create a tuple with a single element, you have to include the final comma:
>>> t1 = ('a',)
>>> type(t1)
<type 'tuple'>
Without the comma Python treats ('a') as an expression with a string in parentheses that evaluates to a string:
>>> t2 = ('a')
>>> type(t2)
<type 'str'>
Another way to construct a tuple is the built-in function tuple.
>>> t2 = tuple('a')
>>> print(t2)
('a',)
Most list operators also work on tuples. The bracket operator indexes an element:
>>> t = ('a', 'b', 'c', 'd', 'e')
>>> print(t[0])
'a'
And the slice operator selects a range of elements.
>>> print(t[1:3])
('b', 'c')
But if you try to modify one of the elements of the tuple, you get an error:
>>> t[0] = 'A'
TypeError: object doesn't support item assignment
The + operator concatenates tuples:
>>> t1 = (10,20,30)
>>> t2 = (7,8,60)
>>> t3 = t1 + t2
>>> print(t3)
(10, 20, 30, 7, 8, 60)
The * operator repeats a tuple a given number of times:
>>> a = (20,30,40)
>>> b = a*2
>>> print(b)
(20, 30, 40, 20, 30, 40)
You can use for loop to iterate over a tuple. Here are some examples:
Program (forloop1.py)
colors = ('orange', 'green', 'blue')
for color in colors:
print(color)
Output
orange green blue
Program (forloop2.py)
for fruit in ('apple', 'banana', 'mango'):
print(fruit)
Output
apple banana mango
The function len returns the length of a tuple.
>>> num = (10,4,90)
>>> len(num)
3
Returns the largest item in a tuple.
>>> num = (10,4,90)
>>> max(num)
90
Returns the smallest item in a tuple.
>>> num = (10,4,90)
>>> min(num)
4
Returns a sorted list of the specified tuple.
>>> t = (10,4,1,55,3)
>>> sorted(t)
[1, 3, 4, 10, 55]
Returns the number of times a specified value occurs in a tuple
>>> num = (10,4,90,4)
>>> num.count(4)
2
Searches the tuple for a specified value and returns the position of where it was found
>>> num = (10,4,90,4)
>>> num.index(90)
2
One of the unique syntactic features of the Python language is the ability to have a tuple on the left side of an assignment statement. This allows you to assign more than one variable at a time when the left side is a sequence.
In this example we have a two-element list and assign the first and second elements of the sequence to the variables x and y in a single statement.
>>> m = [ 'have', 'fun' ]
>>> x, y = m
>>> x
'have'
>>> y
'fun'
Stylistically when we use a tuple on the left side of the assignment statement, we omit the parentheses, but the following is an equally valid syntax:
>>> m = [ 'have', 'fun' ]
>>> (x, y) = m
>>> x
'have'
>>> y
'fun'
A particularly clever application of tuple assignment allows us to swap the values of two variables in a single statement:
>>> a, b = b, a
You can use the built-in list() function to convert a tuple to a list and the built-in tuple() function to convert a list to a tuple.
>>> n1 = (1, 2, 3)
>>> n2 = list(n1)
>>> print(n2)
[1, 2, 3]
>>> x1 = ['red', 'green', 'blue']
>>> x2 = tuple(x1)
>>> print(x2)
('red', 'green', 'blue')