Thursday , September 19 2024

What is the output of the following snippet? my_list = [3, 1, -2] print(my_list[my_list[-1]])

Questions BankCategory: Python Essentials 1What is the output of the following snippet? my_list = [3, 1, -2] print(my_list[my_list[-1]])
What is the output of the following snippet?

my_list = [3, 1, -2]
print(my_list[my_list[-1]])
  • 1
  • -2
  • 3
  • -1

Explanation: Let’s analyze this example:

  • the list my_list is created with the integer elements 3, 1, -2,
  • using a negative index (-1), the value of the last element in the list is obtained, which is -2,
  • -2 is used to obtain the value of the second last element, which is 1,
  • 1 is printed on the console.

More Questions: Python Essentials 1 – Module 3 Test