Python Tuples

Tuple is a container which are store single and group of elements. This element are exists and defined within parentheses and separated of (,)comma.

tupleVariable=(item-1,item-2,...,item-n)

For example

#Tuple value
tupleItem=("python","is","number",1,True)
#display all elements
print(tupleItem)
Output
('python', 'is', 'number', 1, True)

In this example Tuples are contain 4 element. tuple base index is start with 0 . Used of those element by similar to python list. look at view how to storing its elements.

Python Tuples Example

Tuple is internally use key and value pair this. and we can access this value using of its index. index value are positive and negative value both are allowed in tuple. so how to use tuple element in our program. view this example.

#Tuple value
tupleItem=("python","is","number",1,True)

#display element by index
print ("Positive index result")
print(tupleItem[0])
print(tupleItem[1])
print(tupleItem[2])
print(tupleItem[3])
print(tupleItem[4])

print ("Negative index result")
# Formula : tupleItem[totalElement (negative index)]
print(tupleItem[-1]) # tupleItem[5+(-1)] =tupleItem[4]
print(tupleItem[-2]) # tupleItem[5+(-2)] =tupleItem[3]
print(tupleItem[-3]) # tupleItem[5+(-3)] =tupleItem[2]
print(tupleItem[-4]) # tupleItem[5+(-4)] =tupleItem[1]
print(tupleItem[-5]) # tupleItem[5+(-5)] =tupleItem[0]
Output
Positive index result
python
is
number
1
True
Negative index result
True
1
number
is
python

Note that left to right tuple are start with zero. And increment next index values by one, until the not end the tuple element. similar way right to left (backward direction) are start with -1 index and decrement by 1, until of not get first index of tuple. In given example described the formula of how to use negative index.

When not given a valid index then it will produce error. see example.

#Tuple value
# index	   0  1	 2 (left to right)		
tupleItem=(1, 2, 3)
# index	  -3 -2	-1 (right to left)	

print(tupleItem[2]) #display 3

#tupleItem[3+(-2)] =tupleItem[1]
print(tupleItem[-2]) #display 2

#tupleItem[3+(-4)] =tupleItem[-1]--> index out of range
print(tupleItem[-4]) 
print(tupleItem[ 4]) #index out of range
Output
3
2
Traceback (most recent call last):
  File "test.py", line 12, in <module>
    print(tupleItem[-4]) 
IndexError: tuple index out of range

Note that given index is out of range on given list so it will produce an error.

Slice tuple element

slicing are used to get particular portion of given tuple. This are useful to getting the element between the range. This are similar to python list. and there are following ways.

#Tuple value	
tupleItem=("India","Canada","America","England","china")

print(tupleItem[0:5]) #display all element

print(tupleItem[1:4]) #display index value (1,2,3) 

print(tupleItem[0:4]) #display index value (0,1,2,3) 

print(tupleItem[0:])  #display all element

print(tupleItem[:])   #display all element

print(tupleItem[:1])  #display first element

print(tupleItem[-4:3])#display index (1,2)

print(tupleItem[-5:])#display all

print(tupleItem[4:0])#empty tuple
Output
('India', 'Canada', 'America', 'England', 'china')
('Canada', 'America', 'England')
('India', 'Canada', 'America', 'England')
('India', 'Canada', 'America', 'England', 'china')
('India', 'Canada', 'America', 'England', 'china')
('India',)
('Canada', 'America')
('India', 'Canada', 'America', 'England', 'china')
()

In given program view few example for slicing. Note that if not given first index value it will assume that value is 0 (zero), and not given ending index value then it will assume are -1 value. and second index value is less than first index then result are display the empty result (last case of program).

Multiple tuples

multiple tuple means one tuples exist inside another. in similar way there are many nested tuples possible.

#Tuple value	
tupleItem=(("Match",("Runs",(4,6,4,6,2,4,2))),(True,False,("Out")) )
print(tupleItem)
Output
(('Match', ('Runs', (4, 6, 4, 6, 2, 4, 2))), (True, False, 'Out'))

There are some case we will need to define nested multi-tuples. this is an example to define a tuple inside another one. Using this image visualize internal structure of tuples in given example.

Python Multi Tuples

Note that when define nested list it will create new list and assign this reference to specified position. so how to use particular element in this tuples. so how to use that element?. view this example

#Tuple value	
tupleItem=(("Match",("Runs",(4,6,4,6,2,4,2))),(True,False,("Out")) )


print(tupleItem[0][0]) #match

print(tupleItem[0][1][0]) #Runs

print(tupleItem[0][1][1]) #(4,6,4,6,2,4,2)

print(tupleItem[0][1][1][4]) #2

print(tupleItem[0][1][1][5]) #4

print(tupleItem[1]) #(True,False,("Out"))

print(tupleItem[1][2]) #Out
Output
Match
Runs
(4, 6, 4, 6, 2, 4, 2)
2
4
(True, False, 'Out')
Out

Note that main list are exist only two elements. this are used to reference of other list so help of first list index there is possible to access other list element.



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved