Python – Tuples

After reading this Python Tuples topic, you will understand its the theory and examples, you will understand how to create the tuples, and you will know how to accessing Items, changing Items and use tuples built-in methods in Python.

A tuple is a variable that collects same or different type of elements (items) in a sequence. The elements of the tuple cannot change.

How to create Tuple in python?

The tuple items should be written within the round brackets (i.e. ()) and each item separated by a comma (,).

Example:

Program (1): To demonstrate how to create and display tuple in Python.

# storing items 2,3 and 4 in the tuple
x = (2,3,4)
# print a tuple
print(x)

Output(1)

(2, 3, 4)

Accessing Items from Tuple

Example:

Program (1): To demonstrate how to create tuple and access tuple items in Python.

# storing items 2,3 and 4 in the tuple
x = (2,3,4)
print(x[0])
print(x[1])

Output(1)

2
3

Explanation: for Program(1) and Output(1)

  • statement print(x[0]) gives output 2 describe the first item of tuple,  associated with index number 0.
  • statement print(x[1]) gives output 3 describe the second item of tuple, associated with index number 1.

Changing Items are not applicable in Tuple

Example:

Program (1): To show how changing tuple items not possible in Python.

# storing items 2,3 and 4 in the tuple
x = (2,3,4)
#print original tuple
print(x)
#change second item of tuple
x[1]=5
# print tuple with changed items
print(x)

Output(1)

TypeError: 'tuple' object does not support item assignment

Using loop over the Tuple

Using loop over the tuple means we move through the tuple and visit each item of the tuple.

Example:

Program (1): To demonstrate how to use a loop on the tuple to access tuple items in Python.

# storing items 2,3 and 4 in the tuple
x = (2,3,4)
# use for loop in order to print each item of the tuple
for i in x:
print(i)

Output(1)

2
3
4

Tuple handling built-in methods

Some of commonly used tuple handling methods like len(), sum(), max() and min().

len(): This method gives the total number of items in the tuple.

Example:

Program (1): To demonstrate how to use len() function on the tuple in Python.

# storing items 2,3 and 4 in the tuple
x = (2,3,4)
# using len() method to calculate to the total number of items in the tuple.
print(len(x))

Output(1)

3

sum(): This method gives the sum of all the items in tuple.

Example:

Program (1): To demonstrate how to use sum() function on the tuple in Python.

# storing items 2,3 and 4 in the tuple
x = (2,3,4)
# using sum() method gives addition of all the items in tuple
print(sum(x))

Output(1)

9

max() : This method gives largest item in the tuple.

Example:

Program (1): To demonstrate how to use max() function on the tuple in Python.

# storing items 2,3 and 4 in the tuple
x = (2,3,4)
# using max() method to get minimum item
print(max(x))

Output(1)

4

min(): This method gives the smallest item in the tuple.

Example:

Program (1): To demonstrate how to use min() function on the tuple in Python.

# storing items 2,3 and 4 in the tuple
x = {2,3,4}
# using min() method to get minimum item
print(min(x))

Output(1)

2

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *