Python Programming – Tuple

In this Page, We are Providing Python Programming – Tuple. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – Tuple

Tuple

There is also another sequence type- tuple. Tuples is a sequence just like list. The differences are that tuple cannot be changed i.e. tuple is immutable, while list is mutable, and tuple use parentheses, while list use square brackets. Tuple items need not to be of same data type. Also, as mentioned previously, Python allow adding a trailing comma after last item of tuple.

>>> a= ( ' spam ' , ' eggs ' , 100 , 1234 )
>>> a
( ' spam ' , ' eggs ' , 100 , 1234 )
>>> a= ( ' spam ' , ' eggs ' , 100 , 1234 , )
>>> a
( ' spam ' , ' eggs ' , 100 , 1234 )

The above expressions are examples of a “tuple packing” operation i.e. the values ‘ spam ‘, ‘ eggs ‘, 10 0, and 123 4 are packed together in a tuple. The reverse operation is also possible, for example:

>>> a1 , a2 , a3 , a4=a 
>>> a1
' spam '
>>> a2 ' eggs '
>>> a3 100
>>> a4 1234

This is called “sequence unpacking”, and works for any sequence on the right-hand side. Sequence unpacking requires the group of variables on the left to have the same number of elements as the length of the sequence. Note that multiple assignments are really just a combination of tuple packing and sequence unpacking.

>>> a , b=10 , 20 
>>> a 
10
>>> b 
20

Tuple creation

Tuple can be created in many ways.

Using parenthesis

Creating a tuple is as simple as grouping various comma-separated-values, and optionally these comma-separated values between parentheses.

>>> a= ( ' spam ' eggs ' 100 , 1234 )
>>> a
( ' spam ' , ' eggs ' , 100 , 1234 )
>>> a= ( ' spam ' , ' eggs ' , 100 , 1234 )
>>> a
( ' spam ' , ' eggs ' , 100 , 1234 )
>>> a= ' spam ' , ' eggs ' , 100 , 1234 
>>> a
( ' spam ' , ' eggs ' , 100 , 1234 )
>>> a= ' spam ' , ' eggs ' , 100 , 1234 ,
>>> a
( ' spam ' , ' eggs ' , 100 , 1234 )

A tuple with one item is created by a comma after the item (it is not necessary to enclose a single item in parentheses).

>>> a= ( 10 )
>>> a 
10
>>> type ( a )
<type ' int ' >
>>> b= ( 10 ,)
>>> b
( 10 , )
>>> type ( b )
<type ' tuple ' >
>>> c=10,
>>> c
( 10 , )
>>> type ( c )
<type ' tuple ' >

It is also possible to create an empty tuple by assigning parenthesis to a variable.

>>> a= ( ) 
>>> a
( )

Using other tuples

A tuple can also be created by copying a tuple or slicing a tuple.

>>> a= ( ' spam ' , ' eggs ' , 100 , 1234 )
>>> b=a [ : ]
>>> b
( ' spam ' , ' eggs ' , 100 , 1234 )
>>> c=a [ 1 : 3 ]
>>> c
( ' eggs ' , 100 )

Using built-in function

Tuple can also be created using the built-in function tuple ( ). The tuple ( [iterable] ) function return a tuple whose items are the same and in the same order as items of the iterable. The iterable may be a sequence, a container that supports iteration, or an iterator object. If iterable is already a tuple, it is returned unchanged. If no argument is given, an empty tuple is returned.

>>> tuple ( [ ' apple ' , ' pineapple ' , ' banana ' ] )
( ' apple ' , ' pineapple ' , ' banana ' )
>>> tuple ( ' apple ' )
( ' a ' , ' p ' , ’ p ' , ’ 1 ’ , ’ e ’ )
>>> tuple ( [ ' apple ' ] ) 
( ' apple ' , )

Accessing tuple elements

The tuple index starts with 0, and to access values in a tuple, use the square brackets with the index or indices to obtain a slice of the tuple.

>>> a= ( ' spam ' , ' eggs ' , 100 , 1234 )
>>> a[ 0 ]
' spam '
>>> a [ 1 : 3 ]
( ' eggs ' , 100 )

Update tuple

Tuple is immutable, so changing element value or adding new elements is not possible. But one can take portions of existing tuples to create a new tuples.

>>> tuple1= ( 1 , 2 , 3 )
>>> tuple2=( ' a ' , ' b ' , ' c ' )
>>> tuple3=tuple1+tuple2 
>>> tuple3
( 1 , 2 , 3 , ' a ' , ' b ' , ' c ' )
>>> tuple3=tuple1 [ 0 : 2 ]+tuple2 [ 1 : 3 ] 
>>> tuple3
( 1 , 2 , ' b ' , ' c ' )

It is also possible to create tuple which contain mutable objects, such as lists.

>>> a= [ 1 , 2 , 3 ] , [ 4 , 5 , 6 , 7 ]
>>> a
( [ 1 , 2 , 3 ] , [ 4 , 5 , 6 , 7 ] )
>>> a [ 0 ] [ 1 ]=200
>>> a 
( [ 1 , 200 , 3 ] , [ 4 , 5 , 6 , 7 ] )

Deleting tuple

Removing individual tuple elements is not possible. To explicitly remove an entire tuple, just use the del statement.

>>> a= ( ' spam ' ' eggs ' , 100 , 1234 )
>>> del a

Swapping tuples

There might be a scenario in which multiple tuples needs to be swapped among themselves. This is can done easily using multiple assignments expression.

>>> a= ( 10 , 20 , 30 )
>>> b= ( 40 , 50 , 60 )
>> c= ( 70 , 80 , 90 )
>>> a , b , c=c , a , b 
>>> a
( 70 , 80 , 90 )
>>> b
( 10 , 20 , 30 )
>>> c
( 40 , 50 , 60 )