Python is known for various In-built Functions. These In-Built functions are beneficial in many different ways as these functions make code less complex, enhance readability and also reduce the coding time and debugging time. The “index()” Function in Python is one such function. Let us learn about this function in detail with the help of examples.
Python Index Function:
The index function in Python is a built-in function that is used to find the first occurrence of a given element in a sequence. The sequence can be a list, a string, a tuple, or any other iterable object. The index function returns the index of the first occurrence of the given element in the sequence. If the element is not found in the sequence, then it raises a “ValueError”.
The index function is used when we want to find the position of a specific element in a sequence. It is a very useful function and is used frequently in programming. The index function is an important part of Python and is very easy to use.
Syntax of the Index Function in Python
The syntax of the index function in Python is as follows:
sequence.index(element, start, end)
Here, “sequence” is the sequence in which we want to find the element. The element is the element that we want to find in the sequence. The “start” and “end” parameters are optional. The start parameter specifies the starting position of the search, and the end parameter specifies the ending position of the search.
Parameters of the Index Function in Python
The index function in Python has three parameters:
- element: The element that we want to find in the sequence.
- start: The starting position of the search. It is optional.
- end: The ending position of the search. It is optional.
Working of the Index Function in Python
The index function in Python works by searching the given sequence for the first occurrence of the given element. It starts the search from the start parameter, and the end parameter specifies the ending position of the search. If the start and end parameters are not specified, then the search starts at the beginning of the sequence and ends at the end of the sequence.
If the element is found in the sequence, then the index of the first occurrence of the element is returned. If the element is not found in the sequence, then the index function raises a ValueError.
Examples of using the Index Function in Python
Let us understand the index() Function with the help of some examples.
Example 1 of index() Function in Python
In this example, we will find the index of a specific element in a list.
Code:
list1 = [10, 20, 30, 40, 50] index = list1.index(30) print("Index of 30 is:", index)
Output:
Index of 30 is: 2
Explanation:
In this example, we have a list of five elements. We use the index function to find the index of element 30. The index of 30 is 2, which is printed on the screen.
Example 2 of index() Function in Python
In this example, we will find the index of a specific element in a string.
Code:
str1 = "Hello, World!" index = str1.index("o") print("Index of 'o' is:", index)
Output:
Index of 'o' is: 4
Explanation:
In this example, we have a string "Hello, World!". We use the index function to find the index of the letter ‘o’. The index of ‘o’ is 4, which is printed on the screen.
Example 3 of index() Function in Python
In this example, we will find the index of a specific element in a tuple.
Code:
tuple1 = (10, 20, 30, 40, 50) index = tuple1.index(40) print("Index of 40 is:", index)
Output:
Index of 40 is: 3
Explanation:
In this example, we have a tuple of five elements. We use the index function to find the index of element 40. The index of 40 is 3, which is printed on the screen.
Example 4 of index() Function in Python
In this example, we will search for a specific element in a list and handle the case when the element is not found in the list.
Code:
list1 = [10, 20, 30, 40, 50] try: index = list1.index(60) print("Index of 60 is:", index) except ValueError: print("Element not found in the list")
Output:
Element not found in the list
Explanation:
In this example, we have a list of five elements. We use the index function to find the index of element 60. However, since 60 is not present in the list, the index function raises a ValueError. We handle this exception using a try-except block and print an appropriate message on the screen.
Conclusion
The index function in Python is a very useful function that is used to find the index of a specific element in a sequence. It is a built-in function and is very easy to use. The index function works by searching the sequence for the first occurrence of the given element and returns the index of that element. If the element is not found in the sequence, then it raises a ValueError. The index function can be used on various sequence types like lists, strings, tuples, and others.
FAQs Related to Index Function
Ques 1. In which sequence types can the index function be used?
Ans. The index function can be used on various sequence types like lists, strings, tuples, and others.
Ques 2. What happens if the element is not found in the sequence?
Ans. If the element is not found in the sequence, then the index function raises a ValueError.
Ques 3. Can we use the index function to find the index of all occurrences of an element in a sequence?
Ans. No, the index function only returns the index of the first occurrence of the element in the sequence.
Ques 4. Can we use the index function on a set?
Ans. No, the index function cannot be used on a set since sets are unordered collections of unique elements and do not have a specific order.