Python Tuples


Tuples and Sequences

The Python Tuple is a another standard sequence data type and consists of a number of values separated by commas.
Tuples are immutable (object with a fixed value). Immutable objects (numbers, strings, tuples) can not be modified and can be used like constants.

Examples

Extract values from a tuple

>>> v = 123, 87649, 'abc', 7
>>> v
(123, 87649, 'abc', 7)
>>> v[0]
123
>>> v[1]
87649
>>> v[2]
'abc'
>>> v[3]
7
>>>

Assigns values to variables from a tuple

>>> v = 123, 87649, 'abc', 7
>>> v
(123, 87649, 'abc', 7)
>>> a, b, c, d = v
>>> a
123
>>> b
87649
>>> c
'abc'
>>> d
7
>>> 

Get the length of tuple

>>> v1 = 123, 45678, 'xyz', 17, 34
>>> len(v1)
5
>>> v2 = 1, 3, 25
>>> len(v2)
3
>>>