Mastering the Art of Tuples in Python
Unlike Lists, Data in the tuple cannot be altered once a tuple is created. Below are some of the features of Tuples.
- Tuples can hold data of different data types.
- Tuples can not be amended.
- Data in the tuple can be accessed by using index. Index of an element will remain same once defined.
- Tuples can hold duplicate data.
In this post, we will see how to access the data in Tuples and couple of tuple methods.
Creating a Tuple in Python
Like any other data type in Python, Tuple can be created by defining the data inside round brackets.
# Creating Tuple with numeric data
a_tuple = (1, 2, 3)
# Creating Tuple with Strings
b_tuple = ("FOUR", "FIVE", "SIX")
# Creating Tuple with Boolean data
c_tuple = (True, False, True)
# Creating Tuple with different types
d_tuple = (1, "TWO", False)
Tuple can be created with data of different data types (or same type).
- ‘a_tuple’ is created with numeric data.
- ‘b_tuple’ is created with character data.
- ‘c_tuple’ is created with boolean data.
- ‘d_tuple’ is created with data of different data types.
All the tuples defined above has more than one element. Let’s see how to define a tuple with just one element.
Should it just be mentioning one element within round brackets? No, Just mentioning one element with in round brackets would consider the variable as the type of the data mentioned within round brackets.
This can be easily understood with below example.
# Creating a tuple with one element
a_tuple = ("ONE")
b_tuple = (1,)
print(type(a_tuple)) # <class 'str'>
print(type(b_tuple)) # <class 'tuple'>
- ‘a_tuple’ is defined just by mentioning a string with in round brackets. This wouldn’t be considered as a tuple instead variable ‘a_tuple’ would be defined as a string.
- ‘b_tuple’ is defined by mentioning comma (‘,’) after the first element. This would define the variable as a tuple.
Accessing the data in a Tuple
Elements in a tuple can be accessed using index. Index starts with ‘0’ and incremented by ‘1’.
Tuple - (1, "TWO", 3, "FOUR", 5)
Index - 0 1 2 3 4
-5 -4 -3 -2 -1
Elements of a Tuple can be accessed from the last element with the negative index value. As we can see above, Index of last element would be ‘-1’.
If we need to retrieve the second element (“TWO”), index ‘1’ or ‘-4’ needs to be mentioned within square brackets.
print(a_tuple[1]) #prints the second element of a list
print(a_tuple[-4]) #prints the fourth element from end of a list
In the above example, index is mentioned with in square brackets. Yes, Index needs to be mentioned with in square brackets for a tuple (not in round brackets like it is defined).
# Creating a tuple with duplicate data
a_tuple = (1, 2, 3, 2, 5)
print(a_tuple.index(2)) # 1
By using the index() method we would know the first occurrence of an element. Let’s say if there are duplicate entries like in above example, we would need to pass the starting index along with the value.
# Creating a tuple with duplicate data
a_tuple = (1, 2, 3, 2, 5)
print(a_tuple.index(2, 2)) # 3
In the above example (Line — 4), ‘index(2, 2)’ indicates to check for the value ‘2’ (first argument) from the index ‘2’ (second argument) and returns the index.
Count the number of occurrences
We have seen how to retrieve the index of an element. Let’s say we are only interested in knowing how many times a value is present in the list (and not interested in the index), method ‘count()’ is helpful.
Method ‘ count()’ accepts the value to be checked as an arguments and returns the number of occurrences the data is present in a tuple.
# Creating a tuple with duplicate data
a_tuple = (1, 2, 3, 2, 5, 2)
print(a_tuple.count(2)) # 3
In the above example, ‘2’ is present ‘3’ times. So, count() should return ‘3’.
Hope the above details were a bit of help to you in understanding more about Tuples in Python.
Originally published at https://www.codewithpr.com.