×

python · level 1 · unit 6 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Every item in a list has an , starting at 0.

Picture a queue at an ice cream van. Python numbers each place in the queue, but it starts at 0, not 1. That number is the index. queue[0] is the person at the front.

Negative numbers count from the back, so queue[-1] is the last person. To swap someone, use = with an index: queue[1] = "Dev" puts Dev in place 1.

To add someone, them: queue.append("Eli") puts Eli on the end. queue.remove("Ava") takes Ava out, wherever she is, and everyone behind her moves up.

queue = ["Ava", "Ben", "Cleo"]
print(queue[0], queue[-1])
queue[1] = "Dev"
queue.append("Eli")
queue.remove("Ava")
print(queue)

Ask for an index the list does not have, like queue[7], and you get an IndexError.