KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

What will be the output of the code given below:

my_list = [10, 20, 30, 40, 50]
print("The number is ", my_list[-2])

Python List Manipulation

1 Like

Answer

The number is 40

Working

In Python, negative indexing is used to access elements from the end of a list. The index -1 refers to the last element, and -2 refers to the second last element. In the list [10, 20, 30, 40, 50], the second last element is 40, so it is printed.

Answered By

3 Likes


Related Questions