🧩Index
Also known as Array Index, Position
The number of a spot in a list. In most languages the first spot is 0.
Think of it like
Think of it like a seat number at the cinema. It tells you which seat is yours.
Example
Jack asks for fruits[3] in a list of three fruits. Python stops with an IndexError, because the spots are 0, 1 and 2.
Why it matters
Every time code picks one item out of many, it uses an index. Getting it one off is a very common bug.
Definition
Example code
python
>>> fruits = ["apple", "pear", "plum"]
>>> fruits[2]
'plum'
>>> fruits[3]
IndexError: list index out of rangeCommon mistake
Key takeaways
- 1A spot number. It says where an item sits.
- 2Zero first. The first item is at index 0.
- 3Stay in range. Past the end is an error.