The Python Tuple is almost similar to a List except that the Tuples are immutable, and Lists are mutable. It means Once we declare the Python Tuple, we cannot change the values or items inside the Tuple, something like Constant keyword in other programming languages.
A Python Tuple is a sequence of multiple values in an ordered sequence. Tuples are declared using Open and Closed Parenthesis ( ). Unlike Python Strings, Tuple allows us to store different types of data such as integer, float, string, etc.
How to Declare Python Tuple?
The following are the list of possible ways to declare a Tuple in Python.
Tuple_Name = () is an empty Tuple that contains no values.
String_Tuple = (‘apple’, ‘Orange’, ‘Grape’, ‘Mango’) is a string tuple that contains four string values.
Mixed_Tuple = (‘apple’, 2, 3.50, ‘Mango’) is a mixed tuple that contains one integer, one float, and two integer values.
Nested_Tuple = (‘Python’, ‘Tutorial’, (1, 2, 3) ) is an example of tuple inside another tuple (Nested Tuple)
List_Tuple = (‘Python’, ‘Tutorial’, [1, 2, 3] ) is an example of List inside a tuple
TIP: To declare a Tuple with one item, we have to specify a comma after the item. For instance: Tuple_Name = (245,). Forgot to add a comma, then it is an integer type (Not a Tuple).
Accessing Python Tuple items
We can access the Tuple items using indexes. Using the index, we can access each item present in the Tuples separately. Index value starts at 0 and ends at n-1, where n is the Tuple size.
For instance, if a Tuple store 5 elements, then the index starts at 0 and ends with 4. To access the first value, use Tuple_name[0] and to access the fifth value, use Tuple_name[4]. Let us see the Tuple example for better understanding:
x = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Positive Indexing print(x[0]) print(x[4]) print(x[8]) print('=======\n') # Negative Indexing print(x[-2]) print(x[-4]) print(x[-8]) print('=======\n') # Accessing Nested Tuple Items Mixed_Tuple = ((1, 2, 3), [4, 5, 6], 'Python') print(Mixed_Tuple[0][0]) print(Mixed_Tuple[1][0]) print(Mixed_Tuple[2][0]) print(Mixed_Tuple[2][4])
TIP: Using the Negative numbers as an index, Python starts looking for items from right to left
Iterating Python Tuple
The For Loop is the most popular way to traverse the Tuple items. The following statement helps to iterate the Tuple and print the items inside the Fruits tuple.
Fruits = ('Apple', 'Orange', 'Grape', 'Banana', 'Strawberry','Cherry', 'Kiwi') for Fruit in Fruits: print(Fruit)
Performing Arithmetic Operations on Python Tuple
In this example, we show the use of Arithmetic Operators on Python Tuple to perform arithmetic operations.
X = (1, 2, 3) Y = (4, 5, 6) Sum = X + Y print(Sum) # Using * Operator Z = X * 3 print(Z)
From the above Tuple screenshot, you can observe that
- + operator is concatenating the Tuple
- * operator is repeating the Tuple for a given number of times. Here it is three times
Python Tuple slice
In python Tuple Slice syntax, the first integer value is the index position where the slicing starts, and the second one is the index position where the slicing end.
TIP: Python Tuple Slicing goes up to second integer value but not include the value at this index position. For instance, if we specify tuple_exmp[1:4], then slicing starts at index position 1 and ends at position 3 (not 4).
x = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Slicing the Tuple using two indexes a = x[2:6] print("Both Indexes = ", a) # Slicing the Tuple using First indexes b = x[:6] print("No First Index = ", b) # Slicing the Tuple using Second indexes c = x[2:] print("No Second Index = ", c) # Slicing the Tuple without using two indexes d = x[:] print("No Indexes = ", d) # Slicing the Tuple using Negative indexes e = x[-3:] print("Negative First Index = ", e) # Slicing the Tuple using Negative indexes f = x[:-2] print("Negative Second Index = ", f)
Tuple Slicing analysis
- If no first index, then Slicing starts from the beginning
- If the second index not provided, Slicing starts from the first index and continue to the last
- Using the Negative numbers as the index, Slicing starts from right to left.
Altering Python Tuple items
Python won’t allow altering the items inside the Tuple. Let see what happens when we do that?
Tuple analysis
- Within the First statement, We assigned a new value at index position two. Since Tuple is immutable, the output is throwing a Type Error.
- Within the Second statement, We assigned a new value to the Tuple inside the Tuple at index position one. Although it is a Nested tuple, the output is throwing a Type Error because Tuple is immutable.
- Within the Third statement, We assigned a new value at position [1][1], and that is List inside the Tuple. Since List is mutable, we successfully changed the value from 5 to 14.
Python Tuple Methods
Python provides the following methods to perform on Tuple
- Tuple(): This method helps to convert the String or Lists into Tuple.
Tuple sum
The tuple sum function calculates the sum of all items inside the given Tuple.
# Python tuple sum function x = (10, 20, 30, 40, 50, 60, 70, 80, 90) print("Tuple : ", x) total = sum(x) print("The Sum of Integer Tuple is : ", total) numbers = (9, -5, 7, 0, 24, -1, 2, 10) print("\nNumbers Tuple : ", numbers) print("The Sum of a Numbers Tuple is : ", sum(numbers))
Tuple min
Tuple min function helps us to find the Smallest item.
# Python tuple min function fruits = ('Banana', 'Orange', 'Blackberry', 'Apple', 'Kiwi', 'Grape') print("Tuple : ", fruits) minimum = min(fruits) print("The Minimum Value in Fruits Tuple is : ", minimum) numbers = (9, -5, 7, 0, 24, -1, 2, 10) print("\nNumbers Tuple : ", numbers) print("The Minimum Value Numbers Tuple is : ", min(numbers))
Tuple max
Tuple max function finds the Largest item in a Tuple
# Python tuple max function fruits = ('Banana', 'Orange', 'Blackberry', 'Apple', 'Kiwi', 'Grape') print("Tuple : ", fruits) maximum = max(fruits) print("The Maximum Value in Fruits Tuple is : ", maximum) numbers = (9, -5, 7, 0, 24, -1, 2, 10) print("\nNumbers Tuple : ", numbers) print("The Maximum Value Numbers Tuple is : ", max(numbers))
Tuple len
Tuple Len method finds or calculates the Length of a Tuple (Number of items in a Tuple)
# Python tuple len function fruits = ('Apple', 'Banana', 'Orange', 'Blackberry', 'Kiwi', 'Grape') print("Tuple : ", fruits) length = len(fruits) print("The length of a Fruits Tuple is : ", length) numbers = (9, -5, 7, 0, 24, -1, 2, 10) print("\nNumbers Tuple : ", numbers) print("The length of a Numbers Tuple is : ", len(numbers))
Python Tuple sorted
Tuple sorted method helps to sort the items in ascending order.
# Python tuple sorted function fruits = ('Banana', 'Orange', 'Blackberry', 'Apple', 'Kiwi', 'Grape') print("Old Tuple : ", fruits) new_fruits = sorted(fruits) print("Sorted Tuple : ", new_fruits) numbers = (9, -5, 7, 0, 24, -1, 2, 10) print("\nNumber Tuple : ", numbers) new_numbers = sorted(numbers) print("Sorted Numbers Tuple is : ", new_numbers)
Tuple index
The tuple index function returns the index position of a specified value.
# Python tuple index function fruits = ('Banana', 'Orange', 'Blackberry', 'Apple', 'Kiwi', 'Grape') print("Tuple : ", fruits) print("The index position of Apple : ", fruits.index('Apple')) print("The index position of Orange : ", fruits.index('Orange')) print("The index position of Kiwi : ", fruits.index('Kiwi')) numbers = (9, -5, 7, 0, 24, -1, 2, 10) print("\nNumber Tuple : ", numbers) print("The index position of -1 : ", numbers.index(-1)) print("The index position of 0 : ", numbers.index(0)) print("The index position of 10 : ", numbers.index(10))
Tuple count
Tuple count function counts the total number of times the specified value repeated inside the Tuple.
# Python tuple count function fruits = ('Banana', 'Kiwi', 'Grape', 'Apple', 'Kiwi', 'Grape') print("Tuple : ", fruits) print("Number of Times Apple is Repeated : ", fruits.count('Apple')) print("Number of Times Kiwi is Repeated : ", fruits.count('Kiwi')) print("Number of Times Grape is Repeated : ", fruits.count('Grape')) numbers = (2, -5, 7, 9, 2, -5, 2, 10) print("\nNumber Tuple : ", numbers) print("Number of Times 2 is Repeated : ", numbers.count(2)) print("Number of Times -5 is Repeated : ", numbers.count(-5)) print("Number of Times 10 is Repeated : ", numbers.count(10))
In the below program, we are applying all the Tuple methods. It might help to see all the Tuple functions and save the image.
Fruits = ('Apple', 'Orange', 'Banana', 'Kiwi', 'Grape', 'Blackberry') x = (9, 4, -5, 0, 22, -1, 2, 14) #Finding Sum of all item in a Tuple using Python Sum() Method print('Sum of all items in a x Tuple = ', sum(x)) #Calculating Length of a Tuple using Python len() Method print('Length of a Fruits Tuple = ', len(Fruits)) print('Length of a x Tuple = ', len(x)) #Finding Minimum item in a Tuple using Python min() Method print('Minimum item in a Fruits Tuple = ', min(Fruits)) print('Minimum item in a x Tuple = ', min(x)) #Finding Maximum item in a Tuple using Python max() Method print('Maximum item in a Fruits Tuple = ', max(Fruits)) print('Maximum item in a x Tuple = ', max(x)) # Sorting the Tuple using Python Sorted() Method print('After Sorting Fruits Tuple = ', sorted(Fruits)) print('After Sorting x Tuple = ', sorted(x)) # Index position of an item in a Tuple using index() Method print('The Index position of Banana = ', Fruits.index('Banana')) print('The Index position of -1 = ', x.index(-1)) # Counting items in a Tuple using Python count() Method y = (9, 4, 1, 4, 9, -1, 2, 4) print('Number of Times 4 is repeated = ', y.count(4)) print('Number of Times 9 is repeated = ', y.count(9)) # Converting List into Tuple z = [1, 2, 3, 4, 5] print(tuple(z))