528,360 views
25 votes
25 votes
What will be the output of the following Python code?

>>>names = ['Amir', 'Bear', 'Charlton', 'Daman']

>>>print(names[-1][-1])​
a. A

b. n

c. Error

d. Daman

User RWRkeSBZ
by
2.5k points

1 Answer

26 votes
26 votes

Answer:

n

Step-by-step explanation:

List is a data structure in Python that is used to hold multiple items. If we would like to access any item in a list, we may use the index. Index refers to the position of the item in a list and starts from 0. For example, names[0] refers to the first item, Amir, names[1] refers to the Bear in our list. We can also use negative index values. For example, names[-1] refers to the last item, Daman, names[-2] refers to Charlton in our list.

A list called names is initialized with four string variables and we would like to print the value of names[-1][-1].

We already said that names[-1] is the last item in our list. Then, the question turns out to print the 'Daman'[-1]. As we already said, the items are strings. Actually, string variables are kind of lists that consist of characters. That means, they can also be manipulated using index. So, 'Daman'[-1] would give us the last item, n, in our string, Daman.

User Trevorhpittman
by
2.9k points