Informatics Practices
What will be the output of the following statement?
list1 = [1, 2, 3, 4, 5]
list1[len(list1)-1]
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.
Related Questions
What will be the output of the following statement?
list1 = [12, 32, 65, 26, 80, 10] sorted(list1) print(list1)What will be the output of the following statement?
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list1[::-2] list1[:3] + list1[3:]What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for i in range(0, len(myList)): if i % 2 == 0: print (myList[i])What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] del myList[3:] print(myList)