Python Tuple

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.

What is a tuple in Python?

A Python Tuple is a sequence of multiple values in an ordered sequence. Python Tuples are declared using Open and Closed Parenthesis ( ). Unlike Strings, Python Tuple allows us to store different types of data such as integer, float, string, etc.

How to Declare Python Tuple?

To declare an empty Tuple that contains no values, use Tuple_Name = (). 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). The following are the list of possible ways to declare a String, Mixed, Nested and List Tuples in Python.

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.

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).

How to access 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. Using the Negative numbers as an index, Python starts looking for items from right to left.

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])
1
5
9
=======

8
6
2
=======

1
4
P
o

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)
Iterating Python Tuple

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)

Performing the arithmetic Operations on Tuple output

(1, 2, 3, 4, 5, 6)
(1, 2, 3, 1, 2, 3, 1, 2, 3)

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.

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)
Both Indexes =  (3, 4, 5, 6)
No First Index =  (1, 2, 3, 4, 5, 6)
No Second Index =  (3, 4, 5, 6, 7, 8, 9)
No Indexes =  (1, 2, 3, 4, 5, 6, 7, 8, 9)
Negative First Index =  (7, 8, 9)
Negative Second Index =  (1, 2, 3, 4, 5, 6, 7)

Tuple Slicing analysis

  • If no first index, then Python 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?

Removing Python Tuple Data

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. In 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. The Tuple() 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 :  (10, 20, 30, 40, 50, 60, 70, 80, 90)
The Sum of Integer Tuple is :  450

Numbers Tuple :  (9, -5, 7, 0, 24, -1, 2, 10)
The Sum of a Numbers Tuple is :  46

Tuple min

The 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))

Smallest or Minimum item in a Tuple

Tuple :  ('Banana', 'Orange', 'Blackberry', 'Apple', 'Kiwi', 'Grape')
The Minimum Value in Fruits Tuple is :  Apple

Numbers Tuple :  (9, -5, 7, 0, 24, -1, 2, 10)
The Minimum Value Numbers Tuple is :  -5

Tuple max

The 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))

Maximum or Largest Item in a Tuple

Tuple :  ('Banana', 'Orange', 'Blackberry', 'Apple', 'Kiwi', 'Grape')
The Maximum Value in Fruits Tuple is :  Orange

Numbers Tuple :  (9, -5, 7, 0, 24, -1, 2, 10)
The Maximum Value Numbers Tuple is :  24

Tuple len

The 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))

Tuple length output

Tuple :  ('Apple', 'Banana', 'Orange', 'Blackberry', 'Kiwi', 'Grape')
The length of a Fruits Tuple is :  6

Numbers Tuple :  (9, -5, 7, 0, 24, -1, 2, 10)
The length of a Numbers Tuple is :  8

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 sorted function output

Old Tuple :  ('Banana', 'Orange', 'Blackberry', 'Apple', 'Kiwi', 'Grape')
Sorted Tuple :  ['Apple', 'Banana', 'Blackberry', 'Grape', 'Kiwi', 'Orange']

Number Tuple :  (9, -5, 7, 0, 24, -1, 2, 10)
Sorted Numbers Tuple is :  [-5, -1, 0, 2, 7, 9, 10, 24]

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 index Function output

Tuple :  ('Banana', 'Orange', 'Blackberry', 'Apple', 'Kiwi', 'Grape')
The index position of Apple :  3
The index position of Orange :  1
The index position of Kiwi :  4

Number Tuple :  (9, -5, 7, 0, 24, -1, 2, 10)
The index position of -1 :  5
The index position of 0 :  3
The index position of 10 :  7

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))

Tuple count Function output

Tuple :  ('Banana', 'Kiwi', 'Grape', 'Apple', 'Kiwi', 'Grape')
Number of Times Apple is Repeated :  1
Number of Times Kiwi is Repeated :  2
Number of Times Grape is Repeated :  2

Number Tuple :  (2, -5, 7, 9, 2, -5, 2, 10)
Number of Times 2 is Repeated :  3
Number of Times -5 is Repeated :  2
Number of Times 10 is Repeated :  1

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))

Output of all the available Tuple functions.

Sum of all items in a x Tuple =  45
Length of a Fruits Tuple =  6
Length of a x Tuple =  8
Minimum item in a Fruits Tuple =  Apple
Minimum item in a x Tuple =  -5
Maximum item in a Fruits Tuple =  Orange
Maximum item in a x Tuple =  22
After Sorting Fruits Tuple =  ['Apple', 'Banana', 'Blackberry', 'Grape', 'Kiwi', 'Orange']
After Sorting x Tuple =  [-5, -1, 0, 2, 4, 9, 14, 22]
The Index position of Banana =  2
The Index position of -1 =  5
Number of Times 4 is repeated =  3
Number of Times 9 is repeated =  2
(1, 2, 3, 4, 5)