0% found this document useful (0 votes)
13 views2 pages

Python index() Function Explained

The index() function in Python is used to find the index of a specific element in a sequence, returning the index of its first occurrence. If the element is not found, it raises a ValueError. This function simplifies searching in sequences, making it especially useful for large datasets.

Uploaded by

arun tiwari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Python index() Function Explained

The index() function in Python is used to find the index of a specific element in a sequence, returning the index of its first occurrence. If the element is not found, it raises a ValueError. This function simplifies searching in sequences, making it especially useful for large datasets.

Uploaded by

arun tiwari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INDEX () FUNCTION METHOD IN PYTHON

Python's built-in index() function is a useful tool for finding the index of a specific element in
a sequence. This function takes an argument representing the value to search for and
returns the index of the first occurrence of that value in the sequence.

If the value is not found in the sequence, the function raises a Value Error. For
example, if we have a list [1, 2, 3, 4, 5], we can find the index of the value 3 by
calling list. Index(3), which will return the value 2 (since 3 is the third element in
the list, and indexing starts at 0).

The index() function is a powerful tool in Python as it simplifies the process of finding the
index of an element in a sequence, eliminating the need for writing loops or conditional
statements. This function is especially useful when working with large datasets or complex
structures, where manual searching could be time-consuming and error-prone.

Syntax :-
Example 1 :-
list_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
element = 3
print(list_numbers.index(element)) # 2 output : 02

example 2 :-
animals = ['cat', 'dog', 'rabbit', 'horse']

# get the index of 'dog'


index = [Link]('dog') # Output: 1
print(index)

example 03
# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']

# index of 'e' in vowels


index = [Link]('e')

print('The index of e:', index)

# element 'i' is searched


# index of the first 'i' is returned
index = [Link]('i')

print('The index of i:', index) #Output The index of e:

The index of i: 2

You might also like