Tuples in Python are the types in which the user cannot change the elements once they are inserted or assigned. They are just similar to lists but we can change the elements of the list once assigned. A tuple can be created in python by putting the elements inside ‘( )’ these brackets. There is no boundary for the elements in a tuple. It can have a number of elements as a user wants. They can be of different data types as well such as int, float, char etc.
In case we want to put a single element inside a tuple, the user can put a comma after the single element. Otherwise, the program doesn’t consider that element to be in a tuple. For example, it can be written as (100, ).
Different ways to access the elements of a Tuple in Python:
Indexing: Just like an array, the index of a tuple starts from 0. The user can use ‘[ ]’ operator to find the index of an element. For example, if a tuple has 8 elements, the index will be from 0 to 7. This user gets an IndexError exception in case he tries to access the elements outside of the tuple index. There can be another error as well which is TypeError. It occurs when the index is not an integer. Hence, the index has to be integer always in a tuple.
Negative Indexing: The index in python can be negative as well. it process in a way such that -1 is the last item of the index, -2 is the second last, -3 is the third last as so on in the tuple. Check out the python code below to understand indexing and negative indexing in tuples:
dh_tuple = ('a','b','c','d','e','f','g','h','i') print(dh_tuple[1:4]) print(dh_tuple[:-7]) print(dh_tuple[7:]) print(dh_tuple[:])
The output of this program would be:
('b', 'c', 'd') ('a', 'b') ('h', 'i') ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')
Slicing: If a user wants to access a particular range of the elements of a tuple, he can use slicing. It is done using ‘:’ operator called the colon. It slices the tuple to take out a range that the user wants to work on.
Python program to understand the use of tuples using different data types:
dh_tuple = () print(dh_tuple) dh_tuple = (10, 20, 30) print(dh_tuple) dh_tuple = (100, "Welcome", 10.6) print(dh_tuple) dh_tuple = ("Developer Helps", [2, 0, 9], (71, 20, 34)) print(dh_tuple)
The output of this python program would be:
() (10, 20, 30) (100, 'Welcome', 10.6) ('Developer Helps', [2, 0, 9], (71, 20, 34))
Advantages of Tuples over Lists:
As we know that tuples are very much similar to the lists. We prefer lists of tuples where we do not wish to change the data types in future. As tuples can have multiple data types, they are highly used.
- As elements cannot be changed of a tuple, iteration through them is much faster as compared to the lists. Hence, performance in a tuple is much better.
- Tuples also play a major role in dictionary as they are non mutable.
- The work done using tuple is always write-protected. This property is because of the fact that elements in a tuple cannot be changed.
- Elements in a tuple can have different types of data types in a similar code, whereas it is not possible in case of lists.