Python Tuple

Python tuples are a type of sequence, like strings. But unlike strings, Python tuples can contain elements of any type. That means you can have a tuple that can store name, number and score etc. Python Tuples are immutable sequence.

Creating Python Tuple

In this section you will learn how to create a Python tuple.

Empty Python Tuple

 tuple1 = ()
The empty tuple is written as two parentheses containing nothing.
If condition with empty tuple.

Example 1 :

tuple1 = ()
if tuple1:
    print "Something in tuple"
else :
    print "tuple is empty"
if condition on empty Python tuple
if condition on empty tuple

Creating Python Tuple with element

To create the tuple fill the values in tuple separated by commas with parentheses.
tup1 = ('Python ', 'PHP', 1700, 789)
tup2 = (1, 2, 3, 4, 5, 8, 0 )

Indexing tuple

You can specify a position number, in bracket, to access a particular element. Lets check in example Out-put
Python Tuple indexing
Tuple indexing

The below figure shows indexing of the tuple.
Python Tuple indexing by figure
Tuple indexing by figure

Slicing of Tuple

You can give begining and ending position. The result is a tuple containing every element between those positions
Python Tuple Slicing
Tuple Slicing

Getting values in Python tuple

In order to access values from Python tuple, use the square brackets with the index or indices.
Let us discuss the example

Example 2 :

tup1 = ('Python ', 'PHP', 1700, 789)
tup2 = (1, 2, 3, 4, 5, 8, 0 )
print "tup1[0]:", tup1[0]
print "tup2[1:3]:", tup2[1:3]
Accessing the Python Tuple elements
Accessing the Tuple elements

Updating values in Python tuple

As mentioned above Tuple is immutable sequence, you can not update the tuple value. But you can add tuples or portions of tuples.

Example 3 :

tuple1 = ('Python','PHP','HTML','C')
tuple2= ('1','2','3','4')
tuple3= tuple1+tuple2
print"tuple3: ", tuple3
tuple4 = tuple1[0:2]+tuple2[2:4]
print "tuple4: ", tuple4
Python addition of tuples
addition of tuples
In above example the tuple3 is the addition of the tuple1 and the tuple2. Similarly the tuple4 is addition of slices of the tuple1 and the tuple2. deleting the elements of a tuple is not possible. You can delete entire tuple using del statement.

Example 4 :

tuple1 = ('K','B','C')
print "Before deleting", tuple1
del tuple1
print "After deleting", tuple1
Python Tuple deletion
Tuple deletion
You can see the error "name 'tuple1' is not defined" because tuple1 have been already deleted.

Tuple functions

There are some built-in function for tuple.



The function len() gives the length of tuple means total number of elements in a tuple. Out-put
Python Tuple length
Tuple length



The function max(tuple) gives the element of tuple with maximum value.
Python tuple max() tuple
max value



The function min(tuple) gives the element of tuple with minimum value.
Python tuple min() tuple
min value

Operations of Tuples

In this section you will see the use of operators on Python tuple.

Addition and Multiplication

Addition and Multiplication of Python tuple
Addition and Multiplication of tuple
You can see addition and multiplication of the Python tuples.

in operator

In operator find out that whether particular word exists in tuple of not
tuple1 = ("Python", "PHP", "C", "JAVA")
lang = raw_input("Enter the langauge ")
if lang in tuple1:
    print "Yes; you can use: ", lang
else :
    print "You can't use: ", lang
Python in operator
in operator
You can see from output "PHP" exists but HTML not, in the tuple.