Today we are going to learn about Python Tuple. This is very similar to Python List. If you don’t know about list you can find them here.
Python Tuple
Python tuple is a sequence of Python objects. They are lot like lists. But the differences between a tuple and a list is that tuples can not be changed like we can change an item of a list. Also tuples use parentheses while lists use square brackets.
Python Tuple is a sequence of comma-separated values between parentheses. For example;
1 2 3 4 5 6 7 8 |
#an empty tuple emptyTup=() #tuple of string strTup=('This','is','a','tuple') #tuple of integers intTup=(1,2,3,4,5) |
Accessing a Value in Python Tuple
Accessing a value in a python tuple is quite similar to the way to access an item in a list. Through index number we can access a value in a tuple. First element has index number zero, second has index number one, and so forth.
1 2 3 4 5 6 7 8 9 10 |
#tuple of string strTup=('This','is','a','tuple') #accessing first element print(strTup[0]) #accessing second element print(strTup[1]) #accessing fourth element print(strTup[3]) |
The output of the above code will be like below-
Tuple in python also support negative indexing like list. A negative index means index number from the right of that tuple.
1 2 3 4 5 6 7 8 9 10 |
#tuple of string strTup=('This','is','a','tuple') #accessing first element from the right print(strTup[-1]) #accessing second element from the right print(strTup[-2]) #accessing second element from the right print(strTup[-4]) |
The output will be like below.
Update and Delete in Python Tuple
As we have mentioned earlier tuples in python are not changeable. So you can not update a single tuple element. But we can take two tuple and concat them to create a new one.
Also removing an individual tuple element is not possible. Though we can remove an entire tuple using del
statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#tuple 1 tup1=(1,2,3) #tuple 2 tup2=(4,5) #tuple 3 tup3=tup1+tup2 print(tup3) #to delete tuple 1 del tup1 #this will show a traceback as tup1 is deleted. So it is not defined now print(tup1) |
The above code’s output will be like following picture.
Some built-in Python Tuple Functions
There are some built-in functions available to manipulate tuple in python. Take a look at the following code for understanding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#a string tuple tup=('this','is','a','tuple') #len(tuple) gives total length of a tuple print(len(tup)) #max(tuple) gives maximum element of a tuple print(max(tup)) #min(tuple) gives minimum element of a tuple print(min(tup)) #count(x) gives number of occurances of x in the tuple print(tup.count('is')) #index(x) gives index of first occurances of x in the tuple print(tup.index('a')) |
You should get the following output if you run the above code.
So that’s it for python tuple. Make sure to run every piece of code on you own. Feel free to leave a comment if you have any doubt.
#happy_coding 🙂
Reference: https://docs.python.org/3.7/tutorial/datastructures.html#tuples-and-sequences