Tuple is a immutable datatype in python.It is written in small bracket ( ).
Exampletu=("Vikas" , "Small Code" , "Sangam")Indexing of tuple
Indexing of tuple in both direction left to right and right to left.
Left to right have positive indexing and right to left is negative indexing.
Positive indexing start from "0" and negative start from "-1".
tu=(5 , 10 , 15, 20, 25, 30, 35, 40, 45) Positive indexing. 0 1 2 3 4 5 6 7 8 Negative indexing. -9 -8 -7 -6 -5 -4 -3 -2 -1Accessing the element of tuple:
If we want to access the element of tuple use the index value.like Example
tu=("Vikas" , "Small Code" , "Sangam") print(tu[1]) print(tu[-3])Output
Small Code VikasChanging the value of element in tuple:-
We are well known tuple are immutable means we cannot change the value of tuple.
Exampletu=("Vikas" , "Small Code" , 12) tu[0]="Vicky" print(tu[0])Output
tu[0]="Vicky"
TypeError: 'tuple' object does not support item assignment.
Existing of element in tuple:-
If we want to check the element is belong to the tuple or not use in function.
Exampletu=("Vikas" , "Small Code" , 12) if "Vikas" in tu: print("Element is Present.") else: print("Element is not present.")Output
Element is Present.Print the tuple in output screen:-
If we want to print the tuple we just print the name of tuple.
Exampletu=("Vikas" , "Small Code" , 12) print(tu)Output
("Vikas" , "Small Code" , 12)Print the length of tuple
If we want to find how many element in tuple use len() function.
Exampletu=("Vikas" , "Small Code" , 12) print(len(tu))Output
3Changing the value of element in tuple
We are all known tuple is immutable datatype so we cannot change in the value of tuple.
Only one way to change the value that is first we convert tuple to list and then add element and then after convert into tuple.
tu=("Vikas" , "Small Code" , 12) print("Element before Changing.") tu=list(tu) tu[0]="Vicky" tu=tuple(tu) print("Element after Changing.") print(tu)Output
Element before Changing. ("Vikas" , "Small Code" , 12) Element after Changing. ("Vicky" , "Small Code" ,12)Delete the element of the tuple
we are well known that list is immutable so we cannot delete the single element of the tuple.
If we want to delete all the element of the tuple use del function.
tu=("Vikas" , "Small Code" , 12) print("Tuple before delete.") del tu print("Tuple after delete.") print(tu)Output
Tuple before delete. ("Vikas" , "Small Code" , 12) Tuple after delete NameError: name 'li' is not definedJoining of two tuple
In tuple we can join two or more tuple use simply + operator.
Exampletu1=("Vikas" , "Small Code") tu2=("Sangam" , "Satyam") tu3=tu1+tu2 print(tu3)Output
("Vikas" , "Small Code" , "Sangam" , "Satyam")Index of the element
index() function returns the index of the element.
Exampletu=("Vikas" , "Small Code") print(li.index("Vikas"))Output
0count the element in tuple
Count() function returns the number of element in tuple.
Exampletu=("Vikas" , "Small Code",12) print(tu.count())Output
3Maximum and minimum in tuple
max() function returns maximum element in tuple and for minimum min() function.
Exampletu=(12,3,45,6,43,54,34) print(tu.max()) print(tu.min())Output
54 3