ashish chaudhary
Python & AIThe list has three names. Why does using index 3 fail?
names = ["Asha", "Ravi", "Mira"]
print(names[3])
CookieSensei starter Q&A · example discussion.
Loading…
The list has three names. Why does using index 3 fail?
names = ["Asha", "Ravi", "Mira"]
print(names[3])
CookieSensei starter Q&A · example discussion.
List indexes start at 0. A list of three items has indexes 0, 1 and 2. Use index 2 for the third item, or -1 for the last item when the list is not empty. If you are walking through all items, a for loop over the list is often simpler than managing indexes yourself.
names = ["Asha", "Ravi", "Mira"]
print(names[2])
print(names[-1])
CookieSensei starter Q&A · example discussion.