Tuples
Another immutable data type in Python is the tuple.
At times, it's useful create a data structure that won't be altered later in a program. That structure might protect constant data from being overwritten by accident or improve performance for iterating over data. These situations are where tuples come in.
You create tuples in much the same way as you create lists, except that you use parentheses instead of brackets:
t = (1, 2, 3)
t
The output is:
(1, 2, 3)
Because tuples are immutable, you can't change elements within them:
t[1] = 2.0
The error output is:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-110-823b86083d61> in <module>
----> 1 t[1] = 2.0
TypeError: 'tuple' object does not support item assignment
However, you can refer to elements within a tuple:
t[1]
The output is:
2
You can also slice a tuple:
t[:2]
The output is:
(1, 2)
You can create a tuple from a list:
l = ['baked', 'beans', 'spam']
l = tuple(l)
l
The output is:
('baked', 'beans', 'spam')
Or, you can create a list from a tuple:
l = list(l)
l
The output is:
['baked', 'beans', 'spam']
Membership testing
As your Python programming becomes more complex, you'll want to test lists and tuples for the membership of specific data. You use the in operator to do that.
tup = ('a', 'b', 'c')
'b' in tup
The output is:
True
You can also test whether something is not in a list or tuple by using not in:
lis = ['a', 'b', 'c']
'a' not in lis
The output is:
False
Try it yourself
- What happens if you run
lis in lis? Is that the behavior you expected? - If not, think about the nested lists we discussed in an earlier unit.
Hint (expand to reveal)
lis in lis
False