Click for All Topics
ValueError
when the passed element does not exist in the tuple.
Syntax –
tuple.index(element, start, end)
Parameters –
Example 1-
my_tuple=(9,99,999,9999,99999,999999)
index_of_99=my_tuple.index(99)
print("Index of 99:",index_of_99)
# Output -
# Index of 99: 1
99
by using the .index()
method and store it in the variable index_of_99
.
Example 2- find the index of an element using the start and end parameter
my_tuple=('Hello', 'Student', 'this', 'is', 'edSlash')
index_of_is=my_tuple.index('is',1,4)
print("Index Position of 'is' :",index_of_is)
# Output -
# Index Position of 'is' : 3
Example 3-find the index of an element that doesn’t exist in the tuple
my_tuple=('a','b','c','d','e','f')
print(my_tuple.index('g'))
# Output -
# Traceback (most recent call last):
# File "/home/main.py", line 2, in
# print(my_tuple.index('g'))
# ValueError: tuple.index(x): x not in tuple
Example 4- find the index value of an element without using any in-built(.index()
) method
t=(10,20,30,40,50,60,10,70,20)
for i in range(len(t)):
if t[i]==70:
print(i)
break
# Output -
# 7
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India