Tuples are a form of immutable data type.
>>> eggs = ('hello', 42, 0.5)
>>> eggs[0]
'hello'
>>> eggs[1:3]
(42, 0.5)
>>> len(eggs)
3
>>> eggs[1] = 99
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
Note tuples can’t have an index reassigned to a value like lists can.
To define a single tuple value you put a ,
after the value. Otherwise Python
will think it’s something else.
>>> type(('hello',))
<class 'tuple'>
>>> type(('hello'))
<class 'str'>
Tuples are more efficient to deal with in Python and can run faster if that’s a concern.