Today we’re talking about another built-in data type in Python – Tuples. A tuple in Python is used to store multiple elements in an ordered and unchangeable (immutable) manner.
If you need the data to be changeable, a Python list is what you need. Now, without any further delay, let’s get right into our Tuple datatype.
By the end of this tutorial, you’ll know:
- What is a Python tuple?
- How to define tuples?
- Accessing tuple elements
- Different functions for tuples
Python Tuple – Syntax and Examples
Tuples are defined with simple brackets. Here’s a sample definition of a tuple:
SampleTuple = ("Element1", "Element2", "Element3")
Here are some more examples of tuple definitions:
#an empty tuple emptyTup=() #tuple of integers intTup=(1,2,3,4,5)
Lastly, you don’t have to stick to a single data type when adding elements to tuples:
#Tuples with multiple datatypes multiTuple = ("This is a string", 1, True) print(multiTuple)
If you run the above code, you’ll have no issues getting the output tuple:
('This is a string', 1, True)
Accessing a Value in Python Tuple
You can access tuples with indexes. The index of the first element is 0 and the last element has an index of n-1.
#tuple of string strTup=('This','is','a','tuple') #accessing first element print(strTup) #accessing second element print(strTup) #accessing fourth element print(strTup)
The output of the above code will be like below-
Tuples also support negative indexing where the last element of a tuple is indexed -1
#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 Elements in Tuples
Since tuples are immutable objects, the defined elements cannot be changed or updated. But you can combine two tuples into a third one when required.
Similarly, deleting individual elements is not possible, but with the del keyword, you can remove the tuple from your program.
#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 will give you the output as show in the screenshot here:
Some of the built-in functions for Tuples in Python
Tuples offer a variety of built-in functions which reduce the lines of code required. Let’s look at some of the functionality offered by tuples.
#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.
And that brings us to the end of this tutorial on the Python tuple. Go over each piece of code thoroughly to get an understanding of the functions.
You can also refer to the Python tutorial if you want a roadmap to learning all the functions on your coding journey. #happy_coding 🙂