Understanding index in python

plese i have a problem with index in python
like why we use index
for example this code garden[2] = “Strawberries” and when we use it

An index is a position in a list.

Say we have a literal list:

  1. apples
  2. strawberries
  3. bannanas

If we want to tell someone were to look on the list we might say, “Get the third item.”
The same idea works in programming, with the index being the number of item we want to get.

However one major difference is that in programming our counting usually starts at 0 instead on 1, so our list would be:

  1. apples
  2. strawberries
  3. bannanas

We use the index whenever we want a specific item from the list.

2 Likes
  • list []

To which we could add the other iterables, as well.

  • string ""
  • tuple ()
  • set {}
1 Like