The tuple data type
For Python, a tuple is a data type or object. A tuple could contain multiple data types such as integer, float, string, and even another tuple. All data items are included in a pair of parentheses as shown in the following example:
>>>x=('John',21) >>>x ('John', 21)
We can use the len()
function to find out how many data items are included in each variable. Like C++, the subscript of a tuple starts from 0. If a tuple contains 10 data items, its subscript will start from 0 and end at 9:
>>>x=('John',21) >>>len(x) 2 >>>x[0] 'John' >>>type(x[1]) <class 'int'>
The following commands generate an empty tuple and one data item separately:
>>>z=() >>>type(z) <class 'tuple'> >>>y=(1,) # generate one item tuple >>>type(y) <class 'tuple'> >>>x=(1) # is x a tuple?
For a tuple, one of its most important features as shown in the following example, is...