KnowledgeBoat Logo
|

Informatics Practices

What will be the output of the following statement?

list1 = [1, 2, 3, 4, 5]
list1[len(list1)-1]

Python List Manipulation

1 Like

Answer

[1, 2, 3, 4, 5]

Working

In the given code, list1 is initialized as [1, 2, 3, 4, 5]. The len(list1) returns the number of elements in the list, which is 5. The len(list1) - 1 evaluates to 4 (i.e., 5 - 1). The list1[len(list1) - 1] accesses the element at index 4, which is 5. However, since the result is not assigned to any variable or printed, it has no effect on the list.

Answered By

1 Like


Related Questions