How would you access the last element of a list stored in a variable named my_list?
- my_list[-1]
- my_list[0]
- my_list[1]
- my_list[len(my_list)]
In Python, my_list[-1]
is used to access the last element of a list. my_list[0]
would access the first element. my_list[1]
would access the second element. my_list[len(my_list)]
would cause an IndexError as the index would be out of range.
Loading...
Related Quiz
- In Python, which method can be used to add an item at the end of a list?
- What is the result of the following operation in Python? ('apple',) * 3
- Given a scenario where a system should notify a user if the storage space goes below 5% or above 95%. Which of the following combinations of operators will be suitable for the condition?
- In Python, the operator == checks for _______.
- For supporting operations like obj[key], the class should define the _______ method.