Tuples
A tuple (pronounced either like two-ple or tuh-ple) is an immutable data type that are ordered and separated by a comma. A tuple can be created as follows:
>>> my_tuple = 1, 2, 3, 4, 5 >>> my_tuple (1, 2, 3, 4, 5)
Since tuples are immutable, the value at a given index cannot be modified:
>>> my_tuple[1] = 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
A tuple can consist of a number, string, or a list. Since lists are mutable, if a list is a member of a tuple, it can be modified. For example:
>>> my_tuple = 1, 2, 3, 4, [1, 2, 4, 5] >>> my_tuple[4][2] = 3 >>> my_tuple (1, 2, 3, 4, [1, 2, 3, 5])
Tuples are especially useful in scenarios where the value cannot be modified. Tuples are also used to return values from a function. Let's consider the following example:
>>> for value in my_dict.items...