Python tuple data type is similar to the Python list in various ways. Like lists, you can use tuples to store different types of data. The only thing that differs is that tuples are immutable that means once a tuple is created you can’t update data inside it.
The items or elements of a tuple are generally enclosed within the parentheses but it is not always necessary.
In this article, you will learn about Python tuples.
Examples of Python tuples
The following are some of the examples of tuples –
# Example of empty tuple mytuple=() # Tuple contains only numbers numbers=(1,5,8,9,7) # Given tuple contains string elements days=('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun') # Given tuple has mixed elements mixed_tuple = (1, 2, 'Hi', 3,'abc')
Creating tuple in Python
A tuple is created when you assign elements to a variable. Using parentheses is optional but this is a good practice to use them.
Creating empty tuple
my_tuple = () print(my_tuple)
Creating tuple without parentheses
my_tuple = 1, 2, 'Hi' print(my_tuple)
Creating tuple with parentheses
my_tuple = (1, 2, 3, 'Hi') print(my_tuple)
Creating a tuple with a single element
While creating a tuple that has a single element always use a comma otherwise it will not be considered as tuple type.
my_tuple= ("Hi",) my_tuple1=("Hi") print(type(my_tuple)) print(type(my_tuple1))
Now when you execute this program you will see –
Tuple unpacking
You can unpack or extract the elements of a tuple into variables. Remember always to use variables equal to the number of tuple elements otherwise, you will be prompted with an error.
For example –
x=(5,8,9) a,b,c=x print(a) print(b) print(c)
Here 5, 8, 9 will be assigned to variables a, b, c respectively.
Looping in tuple
Using in operator with for loop you can loop in a tuple. By using len()
function you can find the length of a tuple then you can use a loop that iterates from the index 0
to the length-1
.
x=(1, 2, 3, 4, 5) for i in x: print(i)
OR use while loop, as given below, to iterate through the tuple –
x=(1, 2, 3, 4, 5) i=0 while i < len(x): print(x[i]) i=i+1
Accessing tuple elements
You can access the tuple element using positive or negative indexing.
For example –
x=(1,5,7,6,3)
To print the element at index 1 we will use –
print(x[1])
OR print the element at index -1 with the following statement –
print(x[-1])
If you want to access a range of items of a tuple then use slicing. For example, to print elements from index 1 to 3 use –
print(x[1:4])
Here, the tuple element at index 4 will be excluded. When you run this program you will see the following output –
Tuple methods
Python tuples are immutable so there is no method to add or remove elements in the tuple. It provides the following two methods –
Using count() method –
You can use this method to know how many times an element occurs in a tuple. For example –
x=(1,5,7,6,3,5,3,5) print(x.count(5))
You can see the output in the given image –
Using index() method –
The index method returns the index of an element if an element occurs multiple times it returns the index of element which occurs first.
In the above example you can check the index of an element for example you can check 5 or 7 as given in the image –
Conclusion
Since tuples are immutable, operations such as iteration through a tuple are faster than with a list. So you get a performance boost.
Now if you have a query on the given topic then write us in the comments below.