Python Tuples

Python Tuples – Introduction and Functions

Welcome to another chapter of our Python Tutorial series, Learn Python with me. This chapter will discuss Python Tuples, followed by its basic operations.


Find the link to all Chapters here: Learn Python


Python Tuples

Like Lists, Python is also a sequence that stores a group of items or elements. The significant difference is that the Tuples in Python are immutable and cannot be changed. On the other hand, Lists in Python are mutable and can be changed.

It means that you cannot perform operations such as clear(), pop(), remove(), insert(), extend(), and append() on Python tuples due to its immutable behaviour.

Let us look at the examples below to understand how we can create tuples differently in Python programs.

#examples to create tuples differently in Python

tuple1 = () #an empty tuple
tuple2 = (7,) #a tuple with one element
tuple3 = (7,14,-7,'Chennai','Bangalore') #tuple with different kinds of elements
tuple4 = (777,33,40) #a tuple with only integers
tuple5 = 11, 22, 33 #elements separated by commas will also create tuples

print(tuple1)
print(tuple2)
print(tuple3)
print(tuple4)
print(tuple5)
Output

() 

(7,) 

(7, 14, -7, 'Chennai', 'Bangalore') 

(777, 33, 40) 

(11, 22, 33) 

You can notice from the example above that elements separated by commas also form as tuples in Python.

Let us also look at a few other ways to create tuples to help you in real-world programming scenarios.

#create tuples from a range of elements
tuple6 = tuple(range(10,17,2))
print(tuple6)

#create tuples from a list
lst1 = [11,22,33]
tuple7 = tuple(lst1)
print(tuple7)
Output

(10, 12, 14, 16) 

(11, 22, 33) 

How to access elements in Python Tuple?

Please refer to the program below to understand how we can access the elements in the tuple.

#how to access elements in tuple
tuple_eg = (22,33,44,55,66,77)
print(tuple_eg[0]) #access the first element
print(tuple_eg[4]) #access the fifth element
print(tuple_eg[-1]) #access first element from last
print(tuple_eg[-2]) #access second element from last
print(tuple_eg[:]) #print all elements in the tuple
print(tuple_eg[2:5]) #print 3rd element till 5th element
print(tuple_eg[::-2]) #print elements from last with step size of 2
print(tuple_eg[-3:-1]) #prints from 3rd element from last till second last element
Output

22
66
77
66
(22, 33, 44, 55, 66, 77)
(44, 55, 66)
(77, 55, 33)
(55, 66)

Let us also look at a few other ways in the program below to access elements from tuples by storing the elements into separate variables.

#access elements from tuples by storing
#into other variables

employee = (71234, 'Victor', 77, 99, 777)
emp_no, ename = employee[0:2]
print("The employee number is: {}".format(emp_no))
print("The name of employee is: {}".format(ename))

employee_scores = employee[2:]
print("The scores of the employees are as below: ")
for score in employee_scores:
  print(score)
Output

The employee number is: 71234
The name of employee is: Victor
The scores of the employees are as below: 
77
99
777

Functions you can use on Python Tuples

You can use a few functions listed below on tuples to perform your essential operations as per the requirements.

Function Description 
sorted() To sort the elements in ascending order. To sort in descending order, please use reverse=True 
index() Returns the first occurrence of the element in a tuple 
count() Returns the number of times an element is found in a tuple 
max() Returns the largest element in the tuple 
min() Returns the smallest element in the tuple 
len() Returns the number of elements present in a tuple 

Let us now look at a Python program to accept elements as tuples from the keyboard and show their average and sum.

#Python program to find the average and sum of elements in a tuple

num = eval(input("Enter elements in (): "))
total_sum = 0
print("The declared tuple is: \n{}".format(num))
length_of_tuple = len(num)
for element in range(length_of_tuple):
  total_sum+=num[element]
print("Sum of the numbers: {}".format(total_sum))
print("Average of numbers: {}".format(total_sum/length_of_tuple))
Output

Enter elements in (): (10,20,30,40,50)
The declared tuple is: 
(10, 20, 30, 40, 50)
Sum of the numbers: 150
Average of numbers: 30.0

You can notice a new function in the program mentioned above called eval(). It is handy to evaluate whether the elements you enter will be stored as lists or tuples based on your brackets. If you use square braces like [11,22,33], they will be stored as lists. But if you use (11,22,33), it will be stored as a tuple.

Let us look at a Python program to find the first occurrence of an element in a tuple.

#Python program to find the first occurrence of an element in a tuple

#accepting elements as strings separated by commas
str1 = input("Add elements separated by commas: ").split(',')
list1 = [int(n) for n in str1] #convert string to integers and store as list
tuple1 = tuple(list1) #convert the list into tuple
print("The tuple is: {}".format(tuple1))
element = int(input("Enter an element to search: "))
try:
  position = tuple1.index(element)
  print("The element is positioned at: {}".format(position+1))
except ValueError:
  print("Entered element is not available in the tuple")
Output

Add elements separated by commas: 10,20,30,40,50,20
The tuple is: (10, 20, 30, 40, 50, 20)
Enter an element to search: 20
The element is positioned at: 2

In the program mentioned above, we accept elements from the keyboard in the form of strings separated by commas. We then convert the string of elements first to a list and then to a tuple before searching for the occurrences of elements. 

Scroll to Top