Index and slice
Every item in a list has its own number — that’s called the index. And yes, just like with range: counting starts at 0.
Grab an item by index
Section titled “Grab an item by index”Inputs (for input())
One value per line — read in order by input().
fruits[0]— the first item (index 0!)fruits[2]— the third itemfruits[-1]— a negative index counts from the end: the last item. A very handy trick!
Slice — a part of the list
Section titled “Slice — a part of the list”A colon grabs a range: list[start:stop]. Just like with range, the stop index is not included:
Inputs (for input())
One value per line — read in order by input().
[1:3] → indexes 1 and 2 (3 not included). [:2] → from the start up to index 2.
A missing index = an error
Section titled “A missing index = an error”If a list has 4 items, what does fruits[10] give you? IndexError — “there’s no such index.” You’ll see this error yourself in a mission soon. 😉
Missions
Section titled “Missions”First and last
+10 XP ✓ CompletedPrint the first and last team in the table — use a negative index for the last one.
Inputs (for input())
One value per line — read in order by input().
The middle ones
+15 XP ✓ CompletedWith a slice, print only the middle two fruits in the list.
Inputs (for input())
One value per line — read in order by input().
Catch the error
+15 XP ✓ CompletedThis program raises an IndexError — run it, read the error, and fix it with an index that exists.
Inputs (for input())
One value per line — read in order by input().
Next lesson: a list isn’t frozen — add items, remove them, sort them. Continue →