A tuple is a sequence of values much like a list. The values stored in a tuple can be any type, and they are indexed by integers.
The main difference between the tuples and the lists is that the tuples cannot be changed unlike lists. So this is why tuples are immutable.
Tuples use parentheses, whereas lists use square brackets.
Syntactically, a tuple is a comma-separated list of values:
>>> t = 'a', 'b', 'c', 'd', 'e'
Although it is not necessary, it is common to enclose tuples in parentheses to help us quickly identify tuples when we look at Python code:
>>> t = ('a', 'b', 'c', 'd', 'e')
To create a tuple with a single element, you have to include the final comma:
>>> tp = ('a',)
>>> type(tp)
<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. With no argument, it creates an empty tuple:
>>> t = tuple()
>>> print(t)
()
If the argument is a sequence (string, list, or tuple), the result of the call to tuple is a tuple with the elements of the sequence:
>>> t = tuple('lupins')
>>> print(t)
('l', 'u', 'p', 'i', 'n', 's')
Because tuple is the name of a constructor, you should avoid using it as a variable name.
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
You can’t modify the elements of a tuple, but you can replace one tuple with another:
>>> t = ('A',) + ('b','c','d','e',)
>>> print(t)
('A', 'b', 'c', 'd', 'e')
Accessing Values in Tuples
To access values in tuple, use the square brackets for slicing along with the index or indices to obtain the value available at that index.
For example-
You can also delete elements from tuple like the way we do in list
Tuple assignment
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 (which is a sequence) 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'
A particularly clever application of tuple assignment allows us to swap the values
of two variables in a single statement:
>>> a, b = b, a
Both sides of this statement are tuples, but the left side is a tuple of variables; the right side is a tuple of expressions. Each value on the right side is assigned to its respective variable on the left side. All the expressions on the right side are evaluated before any of the assignments.
The number of variables on the left and the number of values on the right must be
the same:
>>> a, b = 1, 2, 3
ValueError: too many values to unpack
More generally, the right side can be any kind of sequence (string, list, or tuple).
For example, to split an email address into a user name and a domain, you could write:
>>> h = 'Hey Hi '
>>> h1,h2 = h.split(' ')
The return value from split is a list with two elements; the first element is assigned to uname, the second to domain.
>>> print(h1)
Hey
>>> print(h2)
Hi
Basic Tuple Operation
Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new tuple, not a string.
In fact, tuples respond to all of the general sequence operations we used on strings in the previous chapter-
Python Expression | Result | Description |
---|---|---|
len((1,2,3)) | 3 | Length |
(1,2) + (5,6) | (1,2,3,4) | Concatenation |
('T') * 3 | ('T','T','T') | Repetition |
3 in (1, 2, 3) | True | Membership |
for x in (1,2,3) :
print (x,end=' ') |
1 2 3 | Iteration |
Since tuples are sequences, indexing and slicing work the same way for tuples as they do for strings, assuming the following input
Python Expression | Result | Description |
---|---|---|
list(0) | 1 | Offsets start at zero |
list(-1) | 5 | Negative: count from the right |
list(0:) | 1,2,3,4,5 | Offsets start at zero |
0 Comments