+ 1
Question on code instructionn course. Update: anwsered
Update: thnx guys! I was indeed doing slicing instead of indexing, still learning not to confuse them :) Hi all, I'm stuck on this question in Module 13 of the introduction to coding course. I should output the second value of the list using positive indexing. animals = ["cat", "dog", "bird"] print(animals[ ]) I should output the second value of the list using positive indexing. I've tried 1:2, but it's incorrect. Can someone explain what I'm doing wrong? Thnx!
2 odpowiedzi
+ 5
As Wong Hei Ming said, you are slicing the list, which means that animals[1:2] returns a list with one item ("dog"), so you would need to do animals[1:2][0] (slice the list and return the first item). However, it is much better to just write animals[1], becasue it is not considered slicing, so it means that the returned value will be string and not a list.
+ 9
print(animals[1:2]) is not positive index, but slice, and it returns a list, not a value.
Your output: ['dog']
print(animals[1])
Output: dog