Tuples in Python

A tuple is a collection of elements which cannot be changed once it is assigned. Tuples are written in parenthesis. A tuple can have elements of different data types and can also contain duplicate elements.

The following program shows a tuple of strings, a tuple of integers, a tuple of floating point numbers, tuple containing different data types and a tuple of duplicate elements.

FirstTuple = ("pen", "pencil", "eraser", "sharpener")
print(FirstTuple)

# Tuple of integers
FirstTuple = ("12", "14", "74", "45")
print(FirstTuple)

# Tuple of Floating point numbers
FirstTuple = ("12.24", "85.21", "23.45", "15.45")
print(FirstTuple)

# Tuple containing different data types
FirstTuple = ("pen", "14", "15.45")
print(FirstTuple)

# Tuple containing duplicate items
FirstTuple = ("pen", "pencil", "eraser", "pen", "eraser")
print(FirstTuple)

Output:

('pen', 'pencil', 'eraser', 'sharpener')
('12', '14', '74', '45')
('12.24', '85.21', '23.45', '15.45')
('pen', '14', '15.45')
('pen', 'pencil', 'eraser', 'pen', 'eraser')

Tuples containing a single element needs to have a comma at the end of the element to identify that it is a tuple. The following program shows a tuple that has only one element.

FirstTuple = ("pen", )
print(FirstTuple)

output:

('pen',)

Tuple can also be made from built in tuple( )  constructor. The following program shows a tuple which is made from tuple( ) constructor.

FirstTuple = tuple(("pen", "pencil", "eraser"))
print(FirstTuple)

Output:

('pen', 'pencil', 'eraser')

The elements of a tuple can be accessed with its index number which starts from [0]. Negative index number starts from the last item from index[-1]. The following program shows accessing of tuple elements with the index number.

# Accessing tuple elements
FirstTuple = ("pen", "pencil", "eraser")
print(FirstTuple[2])

# Negative indexing
FirstTuple = ("pen", "pencil", "eraser")
print(FirstTuple[-1])

output:

eraser
eraser

A range of elements can be selected from tuple with its index number. The index starts from first and ends in last which is not included. In the following program index [3] and [-2] are not included.

When there is no index number from start value, it takes elements from the start. When there is no index number at the end, it takes elements up to the end. When there is no index at start or end, it takes all the elements of the tuple. The following program prints the selected range of elements. Positive index number starts from[0] from the start of a tuple. Negative index starts from[-1] from the end of the tuple.  

FirstTuple = ("pen", "pencil", "eraser", "sharpener", "notebook")
print(FirstTuple[1:3])

print(FirstTuple[:3])

print(FirstTuple[1:])

print(FirstTuple[-4:-2])

print(FirstTuple[:])

Output:

('pencil', 'eraser')
('pen', 'pencil', 'eraser')
('pencil', 'eraser', 'sharpener', 'notebook')
('pencil', 'eraser')
('pen', 'pencil', 'eraser', 'sharpener', 'notebook')

To find that an element is present in a tuple ‘in‘ keyword is used. The following program shows ‘in’ keyword to find an item in the tuple. 

FirstTuple = ("pen", "pencil", "eraser")
if "pencil" in FirstTuple:
    print("Pencil is in this tuple")

Output:

Pencil is in this tuple

Two tuples can be added together using the ‘+’ operator to form a new tuple, also a new tuple can have a multiple number of elements using the ‘*’ operator.

The following program shows the use of ‘+’ and ‘*’ operator to make a new tuple.

FirstTuple = ("pen", "pencil", "eraser")
SecondTuple = ("glue", "stapler")
ThirdTuple = (FirstTuple + SecondTuple)
print(ThirdTuple)

FourthTuple = FirstTuple*2
print(FourthTuple)

Output:

('pen', 'pencil', 'eraser', 'glue', 'stapler')
('pen', 'pencil', 'eraser', 'pen', 'pencil', 'eraser')

An entire tuple can be deleted using the keyword ‘del’. The following program shows the use of keyword ‘del’.

FirstTuple = ("pen", "pencil", "eraser")
del FirstTuple

Tuple elements can be iterated using for loop and while loop. The following program shows iterating through tuple elements and printing its values .

FirstTuple = ("pen", "pencil", "eraser")
for items in FirstTuple:
    print(items)

items = 0
while items < len(FirstTuple):
    print(FirstTuple[items])
    items = items + 1

Output:

pen
pencil
eraser
pen
pencil
eraser

A tuple has the built-in methods to count a certain value and return the specified index number of the element. These are the count( ) and index( ) methods.

The following program shows the use of count( ) method to count number of value ‘pencil’ in the tuple  and index( ) method to return the index number of ‘pen’.

# count() method

FirstTuple = ("pencil", "pen", "pencil", "eraser", "pencil")
num = FirstTuple.count("pencil")
print(num)

# index() method

num = FirstTuple.index("pen")
print(num)

output:

3
1
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments