Python Tuple – What is a Tuple in Python?

What is a Tuple in Python and How to Use It

In this tutorial, you'll learn what a tuple is in Python and how to use it. You'll learn how to create, copy, sort, add and delete a tuple. And, you also learn how to count the number of items in the tuple.

What is a Tuple in Python?

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

In Python, tuples are defined by enclosing the elements in parentheses ( ). The items of a tuple do not have to be the same type.

Creating a Tuple in Python

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

Let's take a look at examples:

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

Here, we've created four tuples, an empty tuple, a tuple containing a single item, a tuple containing multiple items, and a tuple containing different types of items.

Accessing Items

To access items in a tuple, you need to write an index number inside square brackets.
The index numbers start at 0 (zero).

Here are examples that illustrate how to get 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

Output:

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

Changing Tuple Values

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

Removing Items

Since tuples are immutable, removing tuple items is not possible. However, you can delete a tuple completely.

To delete a tuple is to use the del keyword.

Here is an example:

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 an Item Exists

To check if a specified item is present in a tuple, use the in keyword and IF statement.

The following illustrates an example of how to check if an 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!")

Output:

The item exists in the tuple!

Counting Items in a Tuple

Count the Number of Occurrences of an Item

You can use the count() method to count the number of occurrences of the item in a tuple.

tuple.count(item)

Here is an example:

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

number_of_occurrences = fruits.count("Apple")

print(number_of_occurrences)

Output:

3

Count the Number of Elements

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

len(tuple)

Here is an example:

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

number_of_elements = len(fruits )

print(number_of_elements)

Output:

8

Iterating a Tuple in Python

The simple way to iterate over a tuple is to use a FOR loop statement.

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

print(fruit)

Output:

Orange
Melon
Grape
Grape
Orange
Apple

Finding the Maximum or Minimum Value in a Tuple

To find the minimum value in a tuple, use the min() function. And to find the maximum value, use the max() function.

max(tuple)

min(tuple)

Example:

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)

Output:

The maximum value is 300
The minimum value is 100

Sum Values in a Tuple

The easy way to sum all numbers in a tuple is to use the sum() function.

sum(tuple)

Example:

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

Output:

The total value is 1340

However, if you don't want to use the sum() function, you can the FOR loop statement to loop through a tuple and then sum all values.

Here is an example:

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

sum += number

print("The total value is", sum)

Using the FOR loop statement to loop through and sum all values in a tuple is a good idea. I recommend using the built-in function instead, if possible.
However, if you want to sum values in a tuple with specified conditions, you can use the FOR loop statement together with the IF statement.

In this tutorial, you've learned what a tuple is in Python and how to create, copy, sort, add, and delete a tuple. And, you have also learned how to count the number of items in the tuple.


See also:
Python return Statement with Examples
Python List Methods and Functions with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python min() – Find the Smallest Value in Python

Leave a Comment