May 2019
Beginner
528 pages
29h 51m
English
As discussed in the preceding chapter, tuples are immutable and typically store heterogeneous data, but the data can be homogeneous. A tuple’s length is its number of elements and cannot change during program execution.
To create an empty tuple, use empty parentheses:
In [1]: student_tuple = ()In [2]: student_tupleOut[2]: ()In [3]: len(student_tuple)Out[3]: 0
Recall that you can pack a tuple by separating its values with commas:
In [4]: student_tuple = 'John', 'Green', 3.3In [5]: student_tupleOut[5]: ('John', 'Green', 3.3)In [6]: len(student_tuple)Out[6]: 3
When you output a tuple, Python always displays its contents in parentheses. You may surround a tuple’s comma-separated list of values with optional parentheses: ...
Read now
Unlock full access