Product SiteDocumentation Site

7.4. Tuples

Tuples are data separated by comma.
>>> a = 'Fedora', 'Debian', 'Kubuntu', 'Pardus'
>>> a
('Fedora', 'Debian', 'Kubuntu', 'Pardus')
>>> a[1]
'Debian'
>>> for x in a:
...     print x,
...
Fedora Debian Kubuntu Pardus

You can also unpack values of any tuple in to variables, like
>>> divmod(15,2)
(7, 1)
>>> x, y = divmod(15,2)
>>> x
7
>>> y
1

Tuples are immutable, that means you can not del/add/edit any value inside the tuple. Here is another example
>>> a = (1, 2, 3, 4)
>>> del a[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion

Above you can see python is giving error when we are trying to delete a value in the tuple.

To create a tuple which contains only one value you have to type a trailing comma.
>>> a = (123)
>>> a
123
>>> type(a)
<type 'int'>
>>> a = (123, ) #Look at the trailing comma
>>> a
(123,)
>>> type(a)
<type 'tuple'>

Using the buitin function type() you can know the data type of any variable. Remember the len() function we used to find the length of any sequence ?
>>> type(len)
<type 'bulletin_function_or_method'>