LEARN Python - 4.2 - Indexing Finding Things Like a Treasure Hunter - zerotopyhero.com

Learn Python – 4.2: Indexing – Finding Things Like a Treasure Hunter

So now Python can remember many things at once.

That’s great.

But there’s an obvious follow-up question:

“Okay… how do I get one specific thing out of the list?”

That’s where indexing comes in.

Indexing is how you tell Python where to look.

The Big Idea: Positions, Not Names

Lists don’t store items by name.
They store them by position.

Think of a list like a row of boxes:

				
					["apple", "banana", "cherry"]

				
			

Python doesn’t see “banana” as special.
It sees:

  • Position 0 → “apple”

  • Position 1 → “banana”

  • Position 2 → “cherry”

Those positions are called indexes.

The Rule That Confuses Everyone (At First)

Python starts counting at zero.

Not one.
Zero.

So this list:

				
					fruits = ["apple", "banana", "cherry"]

				
			

Looks like this to Python:

				
					index:   0        1         2
value: "apple" "banana" "cherry"

				
			

Yes, it’s weird at first.
Yes, everyone trips over it.
Yes, you’ll get used to it.

Accessing an Item by Index

To get an item, use square brackets with a number:

				
					fruits = ["apple", "banana", "cherry"]

print(fruits[0])

				
			

Output:

				
					apple

				
			

Another one:

				
					print(fruits[1])

				
			

Output:

				
					banana

				
			

You’re telling Python:

“Give me the item at position 1.”

What Happens If You Go Too Far?

This will not work:

				
					print(fruits[3])

				
			

Why?

Because index 3 doesn’t exist.

Python will raise an error and basically say:

“That position is not in the list.”

That’s not Python being mean.
It’s Python protecting you from grabbing imaginary items.

Negative Indexing (A Nice Surprise)

Python has a neat trick.

You can count from the end of the list using negative numbers.

				
					print(fruits[3])

				
			

Output:

				
					cherry

				
			

Here’s how that works:

				
					-1 → last item
-2 → second to last
-3 → third to last

				
			

This is incredibly useful when you don’t know the list length.

Using Indexing with Variables

Indexes don’t have to be hard-coded numbers.

				
					numbers = [10, 20, 30, 40]
i = 2

print(numbers[i])

				
			

Output:

				
					30

				
			

This becomes very powerful when combined with loops later.

Changing an Item Using Its Index

Lists can change, remember?

You can replace items like this:

				
					colors = ["red", "green", "blue"]
colors[1] = "yellow"

print(colors)

				
			

Output:

				
					['red', 'yellow', 'blue']

				
			

You told Python:

“Replace whatever is at index 1.”

And it did.

Indexing + Input (Real Example)

				
					foods = ["pizza", "burger", "pasta"]

choice = int(input("Choose a number (0–2): "))
print(f"You chose {foods[choice]}")

				
			

This lets the user control which item is selected.

(We’ll make this safer later, for now, it’s just about learning indexing.)

Common Beginner Mistakes (Very Normal)

Mistake 1: Forgetting zero-based indexing

				
					print(fruits[1])  # This is NOT the first item

				
			

Python always starts at 0.

Mistake 2: Mixing up indexes and values

				
					print(fruits["banana"])  # Not gonna work

				
			

Lists don’t work by name.
That’s coming later with dictionaries.

Mistake 3: Going out of range

If Python complains about “index out of range,”
you asked for something that isn’t there.

How to Think About Indexing

Instead of thinking:

“Give me the apple”

Think:

“Give me what’s at position 0”

That mental shift makes indexing feel logical instead of annoying.

What You’ve Learned

You now know:

  • What an index is

  • Why Python starts at zero

  • How to access list items

  • How to change items by index

  • How negative indexing works

  • What “index out of range” means

This unlocks a lot of power.

Mini Quiz

Try these:

  1. What index does the first item have?

  2. What does my_list[-1] return?

  3. What happens if you use an index that doesn’t exist?

  4. What will this print?

				
					letters = ["a", "b", "c"]
print(letters[2])

				
			

     5. How would you replace "b" with "x" in the list above?

ZeroToPyHero