Python Tuple with Examples


How to Use the Python Tuple

In this tutorial, we'll learn what is a tuple in Python, how to create and delete a tuple, access items in a tuple, iterate a tuple, count the number of elements in a tuple, and so on.

What is a Tuple in Python?

In Python, a tuple is a collection of elements, which is ordered and immutable. Unlike a list, once a tuple is created, you can change its value.

A tuples is defined by enclosing the elements in parentheses ( ). The elements of a tuple do not have to be same type.

Creating a Tuple in Python

A tuple is created by surrounding objects with ( ) and separating the items with commas (,). 

The following example illustrates how to create a tuple:

tuple_empty = () #an empty tuple

tuple_single_element = ("A", ) #a tuple has a single element

students = ("Student A", "Student B", "Student C", "Student E", "Student F")

tuple_different_types = ("A", "B", 2*4*5, 10, "C", "D", 100/2) #a tuple that has different types of elements

Accessing Items

To access items in a tuple, we write an index number inside square brackets. Note that index numbers start at 0 (zero).

For example, the following illustrates how to get the items in a tuple:

employees = ("Employee A", "Employee B", "Employee C", "Employee D", "Employee E")
employee = employees[4] #get the item at position 4
print("employee:", employee)
tuple = employees[1:3] #slice a tuple
print("tuple:", tuple)
print("tuple[0]:", tuple[0]) #get the item at position 0
print("tuple[1]:", tuple[1]) #get the item at position 1

employee: Employee E
tuple: ('Employee B', 'Employee C')
tuple[0]: Employee B
tuple[1]: Employee C

Changing Tuple Values

Once a tuple is created, it's not possible to change its values. Tuples are immutable.

Removing Items

Since tuples are immutable, removing tuple items is not possible, but we can delete the tuple completely.

To delete a tuple, we use the del keyword.

The following example illustrates how to delete a tuple:

employees = ("Employee A", "Employee B", "Employee C")
print(employees)
del employees
print(employees) #this causes an error because employees is no longer available

Checking if Item Exists

To check if a specified item is present in a tuple use the in keyword.

The following is an example illustrates how to check if the item exists in a tuple:

fruits = ("Apple", "Orange", "Melon", "Grape")
if "Melon" in fruits:
print("The item exists in the tuple!")
else:
print("The item does not exist in the tuple!")

The item exists in the tuple!

Counting Items in a Tuple

To count the number of occurrences of items in a tuple, we use the count() method.

fruits = ("Apple", "Orange", "Melon", "Grape", "Grape", "Apple", "Orange", "Apple")

number_of_occurrences = fruits.count("Apple")

print(number_of_occurrences)

3

To count the number of elements in a tuple, we use the len() method.

fruits = ("Apple", "Orange", "Melon", "Grape", "Grape", "Apple", "Orange", "Apple")

number_of_elements = len(fruits )

print(number_of_elements)

8

Iterating a Tuple in Python

We can the tuple items using a For loop statement.

fruits = ("Orange", "Melon", "Grape", "Grape", "Orange", "Apple")
for fruit in fruits:

print(fruit)

Orange
Melon
Grape
Grape
Orange
Apple

Finding the Maximum or Minimum Value in a Tuple

We can use the min() function to find the minimum value, and max() function to find the maximum value.

numbers = (200, 300, 200, 300, 240, 100)
max = max(numbers)
min = min(numbers)
print("The maximum value is", max)
print("The minimum value is", min)

The maximum value is 300
The minimum value is 100

Sum Values in a Tuple

The easy way to sum values in a tuple is to use the sum() function as you can in the following example:

numbers = (200, 300, 200, 300, 240, 100)
print("The total value is", sum(numbers))

The total value is 1340

However, I show you an example illustrates how to sum values in a tuple without using the sum() method. Let's take a look at the following example:

numbers = (200, 300, 200, 300, 240, 100)
sum = 0;
for number in numbers:

sum += number

print("The total value is", sum)

Writing your own program to sum values in a tuple is very easy, but I recommend using the built-in function instead if possible.

In this tutorial, we've learn what is a tuple in Python, how to create and delete a tuple, access items in a tuple, iterate a tuple, count the number of elements in a tuple, and so on.