Tuples in Python
Tuples in Python
Tuples in Python are a kind of containers which stores elements of same or different types. These elements are ordered meaning they have a particular position at which they are placed in the tuple but unlike lists, they are immutable meaning they cannot be changed. Elements inside a tuple can be accessed by the method of indexing. No new element can be added in it.
Creating Tuples in Python
In Python, tuples can be created by separating the elements by a comma and enclosing them within round brackets or by using the function tuple().
# Python program to exemplify tuple creation #Tuple with different datatypes Info = ('Brenna', 22, 'Eugene') print(Info) #Creating tuple using constructor tuple() colors = tuple(('Yellow', 'Red', 'Green')) print(colors)
Output:
('Brenna', 22, 'Eugene') ('Yellow', 'Red', 'Green')
Access Elements of Tuples in Python
An element of a tuple can be accessed by using an index number. This index number can be either positive or negative. Positive index numbers starts from 0 and index elements from the beginning. Negative index numbers start from -1 and index elements from the end of the tuple.
Tuple Indexing Example:
# Python program to exemplify tuple indexing days = ('MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN') #forward indexing print(days[1]) #backward indexing print(days[-1])
Output:
TUE SUN
Access Range of Elements of Tuples in Python
Range of elements of a tuple can be selected or obtained by index slicing which requires two indices; start_index and end_index and are separated by a colon like this; start_index:end_index. In index slicing, start_index element is included and end_index element is excluded.
# Python program to exemplify index slicing in tuples days = ('MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN') print(days[1:5]) print(days[-5:-2],"\n") print(days[2:]) print(days[:-4],"\n") print(days[:])
Output:
('TUE', 'WED', 'THU', 'FRI') ('WED', 'THU', 'FRI') ('WED', 'THU', 'FRI', 'SAT', 'SUN') ('MON', 'TUE', 'WED') ('MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN')
Modify value of an Element of a Tuple
Elements of a tuple are immutable implying that they are unchangeable. But modification can be achieved by first converting the tuple into a list using list() function and then modifying it. Then the modified list is converted back into a tuple using tuple() function. In the end, you will get a modified tuple.
# Python program to exemplify modification of an element of a tuple Info = ('Brenna', 22, 'Eugene') #tuple Info converted into list Info = list(Info) #Making required changes Info[0] = 'Dragon' #list Info converted back to tuple Info = tuple(Info) print(Info)
Output:
('Dragon', 22, 'Eugene')
Add/Delete Elements of a Tuple
Tuple is an immutable container in Python which implies that no new elements can be added in it and no element can deleted from it. Trying to do so will produce an error. However, the entire tuple can be deleted using the del keyword.
Element Addition in a Tuple Example:
# Python program to exemplify element addition in tuple days = ('SUN', 'MON', 'TUE') # returns an error days[3] = 'WED' print(days)
Output:
Traceback (most recent call last): File "main.py", line 4, in <module> days[3] = 'WED' TypeError: 'tuple' object does not support item assignment
Element Deletion in a Tuple Example:
# Python program to exemplify element deletion in tuple days = ('SUN', 'MON', 'TUE') # returns an error del days[2] print(days)
Output:
Traceback (most recent call last): File "main.py", line 4, in <module> del days[2] TypeError: 'tuple' object doesn't support item deletion
Tuple Length
Length of a tuple can be found by using len() function. It returns the number of elements of a tuple.
#Python program to exemplify tuple length usinf len() function colors = ('Red', 'Blue', 'Yellow', 'Green', 'Orange') print(len(colors))
Output:
5
Check an Element in a Tuple
An element can be checked if it is a part of a tuple or not using if statement along with the in operator.
# Python program to exemplify if an element is a part of a tuple or not numbers = (1,2,3,4,5,6) if 2 in numbers: print('Yes, 2 is an element of numbers.') else: print('No, 2 is not an element of numbers.')
Output:
Yes, 2 is an element of numbers.
Join Tuples
Two tuples can be joined to obtain a new tuple using + operator.
# Python program to exemplify joining of two tuples colors = ('Red', 'Orange', 'Green') numbers = (1, 2) Tuple = colors + numbers print(Tuple)
Output:
('Red', 'Orange', 'Green', 1, 2)
Single Element Tuple
To create a tuple with only one element, the element is followed by a comma and the element and comma are placed inside a set of round brackets.
# Python program to exemplify tuple creation with a single element #this is a tuple number = (1,) print(type(number)) #this is an int number = (1) print(type(number))
Output:
<class 'tuple'> <class 'int'>