Python programming language provides programmers a huge number of exception handler libraries that help them to handle different types of exceptions.
One such exception is the IndexError exception, let's understand it in detail.
IndexError Exception in Python is thrown when the passed value is not in the range of the given sequence [inputted value is out of range exception]. If it is not integer simply TypeError is thrown.
Here is an example that will help you to learn more about it, suppose you have a list of names of 10 students in a class. And the user entered the input for the 12th student. This throws an IndexError.
Program to illustrate index error in Python
Output:
Explanation:
In the above code, we have created a list consisting of the names of 5 employees of a company. And then prompted the user to enter the id of the employee to be searched. If the ID is out of the range of list, IndexError exception is thrown otherwise the ID is printed.
need an explanation for this answer? contact us directly to get an explanation for this answer