Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why it is returning output as n

I have come across one question during where the person ask me
what is the output of this below code

Code :

names = ['Chris', 'Jack', 'John', 'Daman']
print(names[-1][-1])

Then i said the answer will be : John

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But the person who was interviewing he said the answer is 'n'

Can some one illustrate how it is n

>Solution :

The [-1][-1] is not [-2] at all.

Take the last word, then its last letter

>> names  
['Chris', 'Jack', 'John', 'Daman']
>> names[-1]
'Daman'
>> names[-1][-1]
'n'                # 'n' of Daman
>> names[-1][-2]
'a'                # last 'a' of Daman
>> names[0][1]
'h'                # 'h' of Chris
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading