Lists: Operations & Slicing

An in-depth session of 41 slides. It covers indexing from 0, negative indexes, and slicing with the end excluded, along with the slice shortcuts, joining with + and repeating with *, changing items, and append. The three misses the diagnostic found each appear as a trap slide, and there are five checks, followed by a scaffolded build-it-yourself Dream Team Roster project. It was built for the Pre-COSMOS Day 3 hour.

Subject: Python · 75 slides · code lesson

Open the interactive version of this deck · Homework for this lesson

What this lesson covers

The lesson, slide by slide

1. What you will be able to do

Objectives

By the end of this hour you will be able to:

1. Grab any item out of a list using its index - counting from 0.
2. Slice a list to pull out exactly the items you want, and predict how many you get.
3. Join two lists with + and repeat a list with *.
4. Beat all three list questions from the diagnostic.
5. Build a Dream Team Roster program on your own.

2. What survived from Data Types & Conversion?

Warm-up

Discussion prompt

Before we open Lists: Operations & Slicing: without looking back, what was the main idea of Data Types & Conversion, and what could you do by the end of it that you could not do before?

Hint: One sentence for the idea, one for the skill. If the second one is blank, that is the part to revisit.

Answer:

In-depth (40 slides): int/float/str/bool and type(); floats are approximate; // and %; input() always returns text; int()/float()/str() and f-strings; round() vs int() truncation; bool() truthiness; two build-it calculators; five checks. The three classic misconceptions are trap slides.

3. Why lists matter at camp

Concept

Sensor readings, color channels, and motor commands almost always travel in lists - a robot is basically a machine that reads lists and writes lists.

Robot thingWhat it looks like
5 distance readings[34, 36, 35, 90, 88]
One pixel's color[255, 120, 0]
Motor speeds[50, 50] (left, right)

The diagnostic had three list questions - by the end of today you will be able to answer all three without guessing.

4. Fill in: What it looks like for Why lists matter at camp

Comparison

Comparison matrix

From Why lists matter at camp: refill the What it looks like column from what you know. The rest of the table is as it appeared.

Robot thingWhat it looks like
5 distance readings[34, 36, 35, 90, 88]
One pixel's color[255, 120, 0]
Motor speeds[50, 50] (left, right)

5. What a list is

Concept

list — An ordered collection of values, written between square brackets and separated by commas.

[10, 20, 30, 40] is a list of four numbers. ["Bolt", "Ace"] is a list of two strings. Order matters - the list remembers exactly where everything sits.

6. Break it if you can: What a list is

Counterexample

Discussion prompt

[10, 20, 30, 40] is a list of four numbers. ["Bolt", "Ace"] is a list of two strings. Order matters - the list remembers exactly where everything sits.

That is stated as though it always holds. Do one of two things: produce a case where it fails, or say precisely what rules such a case out. "It just does" is not on the menu.

Hint: Hunt at the extremes first — zero, one, negative, empty, equal. If every extreme survives, the reason they survive is the proof.

7. Your first list

Worked example

squad = ["Bolt", "Ace", "Nova", "Rex", "Zap", "Pixel"]
print(squad)
print(len(squad))

Build the list with square brackets

Why: Six names, separated by commas, all stored in one variable called squad - one box holding six things in order.

Ask len() how many items it holds

Why: len() counts the items - it is the same len() that counted characters in a string.

linewhat prints
print(squad)['Bolt', 'Ace', 'Nova', 'Rex', 'Zap', 'Pixel']
print(len(squad))6

8. Lockers numbered from 0

Intuition

Picture the squad in a row of lockers. Python paints a number on every locker - but the painter starts at 0, not 1.

Figure (svg): Six lockers labeled Bolt, Ace, Nova, Rex, Zap, Pixel with index numbers 0 through 5 painted underneath

Six heroes, six lockers - numbered 0 to 5.

So in a list of 6 items the indexes run 0 to 5. There is no locker 6 - asking for it is an error.

9. By analogy: Lockers numbered from 0

Analogy

Discussion prompt

Explain Lockers numbered from 0 by analogy to something with no Python in it at all — a queue, a recipe, a map, a bank balance, whatever fits. Then say where your analogy breaks.

Hint: An analogy that never breaks is not an analogy, it is the same idea wearing a hat. Find the seam — that is the part that is actually new.

Answer:

Picture the squad in a row of lockers. Python paints a number on every locker - but the painter starts at 0, not 1.

10. Indexing: grab one item

Concept

index — An item's position number in a list, starting from 0. Written in square brackets after the list's name: squad[0].

squad[0] is the first item. squad[1] is the second. The number in brackets is the locker number, not the count.

11. Teach it back: Indexing: grab one item

Explain it

Discussion prompt

Explain Indexing: grab one item to a student a year behind you. No notation, no jargon they have not met — and it still has to be true.

Hint: If your explanation needs a symbol they have never seen, you are describing the notation rather than the idea.

Answer:

squad[0] is the first item. squad[1] is the second. The number in brackets is the locker number, not the count.

12. What has to happen first: Grabbing heroes by index

Ranking

Put in order

Put the moves of Grabbing heroes by index into the order they have to happen.

  1. Read squad[0]
  2. Read squad[2]
  3. Read squad[5]

Why: These are the moves of the worked example in the order it makes them, and each one is set up by the one before it. Count lockers 0, 1, 2 - that lands on Nova, the THIRD hero.

13. Grabbing heroes by index

Worked example

squad = ["Bolt", "Ace", "Nova", "Rex", "Zap", "Pixel"]
print(squad[0])
print(squad[2])
print(squad[5])

Read squad[0]

Why: Index 0 is the first locker - Bolt.

expressionlockervalue
squad[0]0Bolt

Read squad[2]

Why: Count lockers 0, 1, 2 - that lands on Nova, the THIRD hero.

expressionlockervalue
squad[0]0Bolt
squad[2]2Nova

Read squad[5]

Why: 5 is the last valid locker in a 6-item list - Pixel.

expressionlockervalue
squad[0]0Bolt
squad[2]2Nova
squad[5]5Pixel

14. What each one costs: Grabbing heroes by index

Trade off

Comparison matrix

From Grabbing heroes by index: every row here is a choice with a cost. Fill the locker column, then say which row you would actually pick and what you give up for it.

expressionlockervalue
squad[0]0Bolt
squad[2]2Nova

15. Something is wrong here: squad[1] is not the first hero

Anomaly

Predict first

A student writes this, and it looks reasonable:

"I want the first hero, so I write squad[1]."

It is wrong. Say what breaks — and say it before you turn the page.

Correct: Feels right - first item, number 1.

"The painter started at 0, so the first locker is squad[0]."

Why: Feels right - first item, number 1. But Python hands back Ace, the second hero.

16. Trap: squad[1] is not the first hero

Trap

The trap

"I want the first hero, so I write squad[1]."

Read squad[1] expecting Bolt

Why: Feels right - first item, number 1. But Python hands back Ace, the second hero.

The fix

"The painter started at 0, so the first locker is squad[0]."

Read squad[0] to get Bolt

Why: Index = how many steps FROM the start. The first item is zero steps away.

17. Negative indexes: counting from the back

Concept

Python also numbers lockers from the back: squad[-1] is the last item, squad[-2] the second-to-last.

Handy when you do not know how long the list is - the newest sensor reading is always readings[-1].

18. The last hero, no counting needed

Worked example

squad = ["Bolt", "Ace", "Nova", "Rex", "Zap", "Pixel"]
print(squad[-1])
print(squad[-2])

Read squad[-1]

Why: -1 means one step back from the end - Pixel.

Read squad[-2]

Why: -2 is two steps back - Zap.

expressionvalue
squad[-1]Pixel
squad[-2]Zap

19. Draw the shape of it: The last hero, no counting needed

Blank canvas

Draw it

Draw what The last hero, no counting needed just did — the shape of it, not the line-by-line working. One picture, labels only where you need them. Then check it against the steps: anything you could not draw is a step you followed rather than understood.

20. Going past the end is an error

Concept

A 6-item list has lockers 0-5. Asking for squad[6] crashes with IndexError: list index out of range.

Rule of thumb: the biggest valid index is always len(list) - 1.

21. Slicing: grab a chunk

Concept

slice — list[start:end] - a NEW list containing the items from index start up to, but NOT including, index end.

nums[1:3] says: start at locker 1, stop before locker 3. The end index is where you stop - it never comes along.

22. Picture it first: Slices cut between the lockers

Picture it

Figure (svg): Four boxes holding 10, 20, 30, 40 with cut positions 0 to 4 marked between them; dashed cuts at positions 1 and 3 keep the boxes holding 20 and 30

Cut at 1, cut at 3 - keep what is between the cuts.

Discussion prompt

Read the picture before the words. What is this showing, and what is the one thing it is built to make obvious? Commit to an answer, then read on.

Hint: Name the parts, then say what changes between them — and if nothing changes, say what is being held still.

Answer:

Do not point at lockers - point at the gaps between them. A slice makes two cuts and keeps what is in the middle.

23. Slices cut between the lockers

Intuition

Do not point at lockers - point at the gaps between them. A slice makes two cuts and keeps what is in the middle.

Figure (svg): Four boxes holding 10, 20, 30, 40 with cut positions 0 to 4 marked between them; dashed cuts at positions 1 and 3 keep the boxes holding 20 and 30

Cut at 1, cut at 3 - keep what is between the cuts.

Bonus: the number of items you get is just end minus start. [1:3] gives 3 - 1 = 2 items.

24. Predict the next row: Tracing nums[1:3]

Pattern

Predict first

The table runs: print(nums[0]) | 10 | single item at locker 0 · print(nums[1:3]) | [20, 30] | indexes 1 and 2 - end excluded

In Tracing nums[1:3], given the rows so far: what is the next one — the row where line is items in the slice?

Correct: items in the slice | 2 | 3 - 1 = 2

linewhat printswhy
print(nums[0])10single item at locker 0
print(nums[1:3])[20, 30]indexes 1 and 2 - end excluded
items in the slice23 - 1 = 2

Why: The relationship between the columns, not the individual numbers, is what generates the next row. Plain index - locker 0 holds 10.

25. Tracing nums[1:3]

Worked example

nums = [10, 20, 30, 40]
print(nums[0])
print(nums[1:3])

Read nums[0]

Why: Plain index - locker 0 holds 10. No brackets in the output: one item, not a list.

index0123
value10203040

Slice nums[1:3]

Why: Start at index 1 (the 20), stop BEFORE index 3 (the 40). Items 1 and 2 come along - the 40 stays behind.

linewhat printswhy
print(nums[0])10single item at locker 0
print(nums[1:3])[20, 30]indexes 1 and 2 - end excluded
items in the slice23 - 1 = 2

26. Inspect it line by line: Tracing nums[1:3]

Error analysis

Annotate

Walk the callouts on Tracing nums[1:3]. Each one is a place this is easy to get subtly wrong.

  • Plain index - locker 0 holds 10. No brackets in the output: one item, not a list.
  • Start at index 1 (the 20), stop BEFORE index 3 (the 40). Items 1 and 2 come along - the 40 stays behind.

27. Something is wrong here: the end index is not included

Anomaly

Predict first

A student writes this, and it looks reasonable:

"nums[1:3] means items 1 through 3, so I get [20, 30, 40] - three items."

It is wrong. Say what breaks — and say it before you turn the page.

Correct: Reads like a range of lockers from 1 to 3 - but Python stops at the second cut, and 40 never makes it in.

"nums[1:3] means start at 1, stop BEFORE 3 - so I get [20, 30] - two items."

Why: Reads like a range of lockers from 1 to 3 - but Python stops at the second cut, and 40 never makes it in.

28. Trap: the end index is not included

Trap

The trap

"nums[1:3] means items 1 through 3, so I get [20, 30, 40] - three items."

Expect indexes 1, 2 AND 3

Why: Reads like a range of lockers from 1 to 3 - but Python stops at the second cut, and 40 never makes it in.

The fix

"nums[1:3] means start at 1, stop BEFORE 3 - so I get [20, 30] - two items."

Take indexes 1 and 2 only

Why: The end number is the stop sign, not a passenger. Count check: 3 - 1 = 2 items.

29. Break it on purpose: the end index is not included

Break the constraint

Discussion prompt

The rule this trap just fixed:

The end number is the stop sign, not a passenger. Count check: 3 - 1 = 2 items.

Now break it on purpose. Build a case that violates it and follow the consequences until something visibly fails. Where does the failure first show up — and would you have noticed it if you had not been looking?

Hint: The dangerous rules are the ones whose violation still produces an answer. If yours fails loudly, try to find one that fails quietly.

Answer:

Reads like a range of lockers from 1 to 3 - but Python stops at the second cut, and 40 never makes it in.

30. Slice shortcuts: leave a side blank

Concept

Leave out the start and Python uses the very beginning: squad[:3]. Leave out the end and it runs to the very end: squad[3:].

squad[:] (both blank) copies the whole list. Read [:3] out loud as "the first three."

31. Shortcuts in action

Worked example

squad = ["Bolt", "Ace", "Nova", "Rex", "Zap", "Pixel"]
print(squad[:3])
print(squad[3:])

Slice squad[:3]

Why: Blank start = from the beginning. Stop before 3: indexes 0, 1, 2 - the first three.

Slice squad[3:]

Why: Start at 3, blank end = all the way to the last item: indexes 3, 4, 5.

expressionresult
squad[:3]['Bolt', 'Ace', 'Nova']
squad[3:]['Rex', 'Zap', 'Pixel']
togetherthe whole squad, split in two

32. Draw the shape of it: Shortcuts in action

Blank canvas

Draw it

Draw what Shortcuts in action just did — the shape of it, not the line-by-line working. One picture, labels only where you need them. Then check it against the steps: anything you could not draw is a step you followed rather than understood.

33. + joins two lists

Concept

concatenation — Gluing two lists end-to-end with + to make one NEW, longer list.

[1, 2] + [3] is [1, 2, 3]. Same + you used on strings: "ab" + "c" glued text, this glues lists. It never does math on what is inside.

34. Restore the missing line: Merging two squads

Fill the middle

Fill in the blanks

From Merging two squads — one line has had its right-hand side removed. Put it back.

squad = ["Bolt", "Ace", "Nova", "Rex", "Zap", "Pixel"]
squad2 = ["Turbo", "Glitch"]
team = squad + squad2
print(team)
print(len(team))

Why: team is what everything below it consumes, so the wrong expression here fails later and somewhere else. All six originals first, then the two recruits, in order - a brand-new 8-item list.

35. Merging two squads

Worked example

squad = ["Bolt", "Ace", "Nova", "Rex", "Zap", "Pixel"]
squad2 = ["Turbo", "Glitch"]
team = squad + squad2
print(team)
print(len(team))

Glue squad and squad2 with +

Why: All six originals first, then the two recruits, in order - a brand-new 8-item list.

Count the merged team

Why: 6 + 2 = 8. Joining lists adds their LENGTHS, never the items inside.

linewhat prints
print(team)['Bolt', 'Ace', 'Nova', 'Rex', 'Zap', 'Pixel', 'Turbo', 'Glitch']
print(len(team))8

36. Inspect it line by line: Merging two squads

Error analysis

Annotate

Walk the callouts on Merging two squads. Each one is a place this is easy to get subtly wrong.

  • All six originals first, then the two recruits, in order - a brand-new 8-item list.
  • 6 + 2 = 8. Joining lists adds their LENGTHS, never the items inside.

37. Something is wrong here: [1, 2] + [3] joins - it does not add

Anomaly

Predict first

A student writes this, and it looks reasonable:

"Plus means add, so [1, 2] + [3] is 1 + 2 + 3 = 6."

It is wrong. Say what breaks — and say it before you turn the page.

Correct: That is what + does to plain numbers - but these are lists, and + never opens the boxes to do math.

"These are lists, so + glues them: [1, 2] + [3] is [1, 2, 3]."

Why: That is what + does to plain numbers - but these are lists, and + never opens the boxes to do math.

38. Trap: [1, 2] + [3] joins - it does not add

Trap

The trap

"Plus means add, so [1, 2] + [3] is 1 + 2 + 3 = 6."

Add up everything inside

Why: That is what + does to plain numbers - but these are lists, and + never opens the boxes to do math.

The fix

"These are lists, so + glues them: [1, 2] + [3] is [1, 2, 3]."

Join the lists end-to-end

Why: A 2-item list plus a 1-item list = a 3-item list. The items just line up; nothing gets added together.

39. * repeats a list

Concept

[0] * 3 is [0, 0, 0] - the list stamped out three times, not multiplication on the 0.

Robots use this to start clean: [0] * 8 makes eight zeroed-out sensor slots in one line.

40. Predict the next row: The chant builder

Pattern

Predict first

The table runs: ["Go!"] * 3 | ['Go!', 'Go!', 'Go!'] · [1, 2] * 2 | [1, 2, 1, 2]

In The chant builder, given the rows so far: what is the next one — the row where expression is **[0] * 3**?

Correct: [0] * 3 | [0, 0, 0]

expressionresult
["Go!"] * 3['Go!', 'Go!', 'Go!']
[1, 2] * 2[1, 2, 1, 2]
[0] * 3[0, 0, 0]

Why: The relationship between the columns, not the individual numbers, is what generates the next row. One copy, two copies, three copies, glued into a single list of 3 items.

41. The chant builder

Worked example

chant = ["Go!"] * 3
print(chant)
print([1, 2] * 2)

Stamp ["Go!"] three times

Why: One copy, two copies, three copies, glued into a single list of 3 items.

Stamp [1, 2] twice

Why: The WHOLE pattern repeats: 1, 2, then 1, 2 again. No number ever gets doubled.

expressionresult
["Go!"] * 3['Go!', 'Go!', 'Go!']
[1, 2] * 2[1, 2, 1, 2]
[0] * 3[0, 0, 0]

42. Fill in: result for The chant builder

Comparison

Comparison matrix

From The chant builder: refill the result column from what you know. The rest of the table is as it appeared.

expressionresult
["Go!"] * 3['Go!', 'Go!', 'Go!']
[1, 2] * 2[1, 2, 1, 2]
[0] * 3[0, 0, 0]

43. Something is wrong here: [0] * 3 stamps - it does not multiply

Anomaly

Predict first

A student writes this, and it looks reasonable:

"Times 3 multiplies, so [0] * 3 is [0]... and [1, 2] * 2 must be [2, 4]."

It is wrong. Say what breaks — and say it before you turn the page.

Correct: Feels like math class - but * on a list never touches the numbers inside the brackets.

"* copies the whole list: [0] * 3 is [0, 0, 0] and [1, 2] * 2 is [1, 2, 1, 2]."

Why: Feels like math class - but * on a list never touches the numbers inside the brackets.

44. Trap: [0] * 3 stamps - it does not multiply

Trap

The trap

"Times 3 multiplies, so [0] * 3 is [0]... and [1, 2] * 2 must be [2, 4]."

Multiply each number inside

Why: Feels like math class - but * on a list never touches the numbers inside the brackets.

The fix

"* copies the whole list: [0] * 3 is [0, 0, 0] and [1, 2] * 2 is [1, 2, 1, 2]."

Repeat the whole list that many times

Why: It is a rubber stamp: 3 stamps of [0] leave three 0s on the page. The length multiplies, the values never change.

45. Which of these survive contact with Lists: Operations & Slicing?

Two truths and a lie

Sort into buckets

Some of these hold up and some are the exact mistakes this lesson is built to prevent. Sort them.

Holds up
Sensor readings, color channels, and motor commands almost always travel in lists - a robot is basically a machine that reads lists and writes lists.; [10, 20, 30, 40] is a list of four numbers. ["Bolt", "Ace"] is a list of two strings. Order matters - the list remembers exactly where everything sits.; Picture the squad in a row of lockers. Python paints a number on every locker - but the painter starts at 0, not 1.
Breaks
"I want the first hero, so I write squad[1]."; "nums[1:3] means items 1 through 3, so I get [20, 30, 40] - three items."
sound
These are stated as this lesson states them — each one survives the edge cases Lists: Operations & Slicing puts it through.
flawed
Each of these is lifted from a trap in this deck: reasonable-sounding, and wrong in a way that only shows up once you rely on it.

46. Lists can change: swap an item

Concept

Assign straight into a locker: squad[1] = "Comet" throws out whoever was at index 1 and puts Comet there.

This is a big difference from strings - you cannot do "team"[0] = "T", but you CAN rewrite a list.

47. Teach it back: Lists can change: swap an item

Explain it

Discussion prompt

Explain Lists can change: swap an item to a student a year behind you. No notation, no jargon they have not met — and it still has to be true.

Hint: If your explanation needs a symbol they have never seen, you are describing the notation rather than the idea.

Answer:

Assign straight into a locker: squad[1] = "Comet" throws out whoever was at index 1 and puts Comet there.

48. Restore the missing line: Trading Ace for Comet

Fill the middle

Fill in the blanks

From Trading Ace for Comet — one line has had its right-hand side removed. Put it back.

squad = ["Bolt", "Ace", "Nova", "Rex", "Zap", "Pixel"]
squad[1] = "Comet"
print(squad)

Why: squad[1] is what everything below it consumes, so the wrong expression here fails later and somewhere else. Index 1 held Ace; the assignment replaces just that one slot, everyone else stays put.

49. Trading Ace for Comet

Worked example

squad = ["Bolt", "Ace", "Nova", "Rex", "Zap", "Pixel"]
squad[1] = "Comet"
print(squad)

Assign Comet into locker 1

Why: Index 1 held Ace; the assignment replaces just that one slot, everyone else stays put.

012345
beforeBoltAceNovaRexZapPixel
afterBoltCometNovaRexZapPixel

50. append: grow the team by one

Concept

append — list.append(item) adds one item to the END of the list, in place - the list itself gets longer.

+ builds a new list; append stretches the same list. For adding one recruit at a time, append is the move.

51. Term to definition: Lists: Operations & Slicing

Matching

Match the pairs

Match each term to the definition this lesson gave it — not the one you would guess from the word.

  • t1. list
  • t2. index
  • t3. slice
  • t4. concatenation
  • t5. append
  • d1. An ordered collection of values, written between square brackets and separated by commas.
  • d2. An item's position number in a list, starting from 0. Written in square brackets after the list's name: squad[0].
  • d3. list[start:end] - a NEW list containing the items from index start up to, but NOT including, index end.
  • d4. Gluing two lists end-to-end with + to make one NEW, longer list.
  • d5. list.append(item) adds one item to the END of the list, in place - the list itself gets longer.

Why: These are the working definitions of list, index, slice, concatenation, append as Lists: Operations & Slicing uses them. Pairing them correctly is the test of whether you could state each one with the slide switched off.

52. Recruiting Vortex

Worked example

squad = ["Bolt", "Comet", "Nova", "Rex", "Zap", "Pixel"]
squad.append("Vortex")
print(squad)
print(len(squad))

Append Vortex to the squad

Why: Vortex lands in a brand-new locker 6 at the end; nobody else moves.

linewhat prints
print(squad)['Bolt', 'Comet', 'Nova', 'Rex', 'Zap', 'Pixel', 'Vortex']
print(len(squad))7

53. The list toolbox

Pattern

Everything from this hour fits on one card:

#you wantyou writeremember
1one itemsquad[i]first item is squad[0]
2the last itemsquad[-1]negatives count from the back
3a chunksquad[start:end]end is EXCLUDED; count = end - start
4the first threesquad[:3]blank start = from the beginning
5everything aftersquad[3:]blank end = to the very end
6join two listsa + bglues, never adds the insides
7repeat a lista * nstamps the whole list n times
8swap an itemsquad[i] = newrewrites one locker
9add one at the endsquad.append(x)same list, one longer

54. Where this shows up: Lists: Operations & Slicing

Real world

Discussion prompt

Outside this lesson: where does Lists: Operations & Slicing actually turn up? Name one concrete situation — a job, a piece of software someone ships, a decision somebody has to make — and say which part of The list toolbox is doing the work in it.

Hint: Vague is the failure mode here. "Engineering" is not a situation; "deciding whether this build is fast enough to ship" is.

Answer:

An in-depth session of 41 slides. It covers indexing from 0, negative indexes, and slicing with the end excluded, along with the slice shortcuts, joining with + and repeating with *, changing items, and append. The three misses the diagnostic found each appear as a trap slide, and there are five checks, followed by a scaffolded build-it-yourself Dream Team Roster project. It was built for the Pre-COSMOS Day 3 hour.

55. Check yourself: the first hero

Check

squad is ["Bolt", "Ace", "Nova", "Rex", "Zap", "Pixel"]. Decide before you click.

Check your understanding

What does print(squad[1]) show?

  • A. Bolt
  • B. Ace (correct)
  • C. Nova
  • D. an IndexError

Answer: B

Why: Indexes start at 0, so locker 0 holds Bolt and locker 1 holds Ace - squad[1] is the SECOND hero.

Why A tempts people
Treats 1 as the first position - but Python starts counting at 0, so the first hero is squad[0].
Why C tempts people
Counted two steps in (lockers 1 and 2) instead of reading locker 1 directly.
Why D tempts people
1 is a perfectly valid index here - errors only happen past the end, like squad[6].

56. Check yourself: the slice

Check

nums is [10, 20, 30, 40]. This exact question was on the diagnostic.

Check your understanding

What does print(nums[1:3]) show?

  • A. [20, 30, 40]
  • B. [10, 20, 30]
  • C. [20, 30] (correct)
  • D. [30]

Answer: C

Why: Start at index 1 (the 20), stop BEFORE index 3 - so only indexes 1 and 2 come along. Count check: 3 - 1 = 2 items.

Why A tempts people
Included the end index 3 - but the end is the stop sign, and the 40 never makes it in.
Why B tempts people
Started counting positions at 1 instead of 0, taking the 'first through third' items.
Why D tempts people
Took only the item at the end index minus... this keeps one item, but a [1:3] slice spans two lockers.

57. Check yourself: plus

Check

Another one straight off the diagnostic. What kind of values are these?

Check your understanding

What does print([1, 2] + [3]) show?

  • A. 6
  • B. [1, 2, 3] (correct)
  • C. [4, 5]
  • D. an error

Answer: B

Why: + between two lists glues them end-to-end into one new list: [1, 2, 3]. It never does arithmetic on the items inside.

Why A tempts people
Added the numbers inside (1+2+3) - but + on lists joins the boxes, it never opens them.
Why C tempts people
Added 3 to each item - that elementwise math is something + on plain lists never does.
Why D tempts people
Joining two lists with + is completely legal - the error comes from mixing types, like [1, 2] + 3 without brackets.

58. Check yourself: times

Check

Last of the three diagnostic questions. Stamp or multiply?

Check your understanding

What does print([0] * 3) show?

  • A. [0]
  • B. 0
  • C. [0, 0, 0] (correct)
  • D. [3]

Answer: C

Why: * repeats the whole list: three stamps of [0] make [0, 0, 0]. The length multiplies; the values inside never change.

Why A tempts people
Multiplied the inside: 0 x 3 = 0, list unchanged - but * never touches the items, it copies the list.
Why B tempts people
Did plain number math 0 x 3 - but one side is a list, so the result is a longer list, not a number.
Why D tempts people
Multiplied 0... wait, replaced the item with 3 - * never rewrites items, it only repeats the pattern.

59. Rule out three: Check yourself: how many items?

Elimination

Eliminate the wrong options

team has 8 items. How many items are in team[2:5]?

3 of these 4 are wrong. Strike them one at a time, and say what rules each one out before you strike the next. The survivor is the answer.

  • A. 4
  • B. 3
  • C. 2
  • D. 5

Survives elimination: B

Why: A slice holds end minus start items: 5 - 2 = 3 (indexes 2, 3, and 4 - the 5 is excluded).

60. Check yourself: how many items?

Check

No list needed - just the counting rule.

Check your understanding

team has 8 items. How many items are in team[2:5]?

  • A. 4
  • B. 3 (correct)
  • C. 2
  • D. 5

Answer: B

Why: A slice holds end minus start items: 5 - 2 = 3 (indexes 2, 3, and 4 - the 5 is excluded).

Why A tempts people
Counted 2 through 5 inclusive (2, 3, 4, 5) - but the end index never comes along.
Why C tempts people
Subtracted an extra one, as if BOTH ends were excluded - the start index IS included.
Why D tempts people
Read the end index as the count - the slice stops at 5, it does not contain 5 items.

61. PROJECT: Dream Team Roster

Concept

Now you build, on your own. Mission: a roster program for your dream team of robots or heroes.

#requirementtool you'll use
1a squad list with SIX names[ ] and commas
2print the starting lineup - first three - with ONE slice[:3]
3merge a second squad onto the team+
4stretch: captain, bench, and a chant[0], [3:], *

Rules of the build: type everything yourself, run after every line, and when something surprises you - read the error, do not erase it. Hints are one beat away if you get stuck.

62. By analogy: PROJECT: Dream Team Roster

Analogy

Discussion prompt

Explain PROJECT: Dream Team Roster by analogy to something with no Python in it at all — a queue, a recipe, a map, a bank balance, whatever fits. Then say where your analogy breaks.

Hint: An analogy that never breaks is not an analogy, it is the same idea wearing a hat. Find the seam — that is the part that is actually new.

Answer:

Rules of the build: type everything yourself, run after every line, and when something surprises you - read the error, do not erase it. Hints are one beat away if you get stuck.

63. Build it 1: draft your squad - your turn

Worked example

Your turn (about 5 minutes): make a list called squad with six names you like, then prove it works by printing the list and its length. Don't peek ahead until it runs.

Hint: six strings in quotes, commas between, square brackets around. len(squad) should print 6.

squad = ["Bolt", "Ace", "Nova", "Rex", "Zap", "Pixel"]
print(squad)
print(len(squad))
you should see
the six names in brackets['Bolt', 'Ace', 'Nova', 'Rex', 'Zap', 'Pixel']
the count6

64. Build it 2: the starting lineup - your turn

Worked example

Your turn: print the starting lineup - the first three heroes - using exactly one slice. Before you run it, say out loud how many names will print.

Hint: "the first three" is a slice with a blank start. Where does it stop - and is that index included?

print("Starters:", squad[0:3])
expressionresult
squad[0:3]['Bolt', 'Ace', 'Nova']
squad[:3]same thing - blank start means 0
items printed3 - 0 = 3

65. Build it 3: merge the recruits - your turn

Worked example

Your turn: make a second list squad2 with two new recruits, merge it onto your squad with + into a variable called team, and print the full team and its new length.

Hint: + makes a NEW list - catch it in a variable or it vanishes. Predict the new length before you print it.

squad2 = ["Turbo", "Glitch"]
team = squad + squad2
print("Full team:", team)
print(len(team))
checkexpected
recruits at the END of the printout..., 'Pixel', 'Turbo', 'Glitch']
len(team)8 - lengths add, items never do

66. What each one costs: Build it 3: merge the recruits - your turn

Trade off

Comparison matrix

From Build it 3: merge the recruits - your turn: every row here is a choice with a cost. Fill the expected column, then say which row you would actually pick and what you give up for it.

checkexpected
recruits at the END of the printout..., 'Pixel', 'Turbo', 'Glitch']
len(team)8 - lengths add, items never do

67. Build it 4 (stretch): captain, bench, chant

Worked example

Stretch goals, your turn: print the captain (the very first team member), the bench (everyone from index 3 on), and a chant that says Go! three times - using *, not three print calls.

Hint: captain is a single index. Bench is a slice with a blank end. The chant is a 1-item list stamped 3 times.

print("Captain:", team[0])
print("Bench:", team[3:])
print(["Go!"] * 3)
linewhat prints
team[0]Bolt
team[3:]['Rex', 'Zap', 'Pixel', 'Turbo', 'Glitch']
["Go!"] * 3['Go!', 'Go!', 'Go!']

68. Fill in: what prints for Build it 4 (stretch): captain, bench, chant

Comparison

Comparison matrix

From Build it 4 (stretch): captain, bench, chant: refill the what prints column from what you know. The rest of the table is as it appeared.

linewhat prints
team[0]Bolt
team[3:]['Rex', 'Zap', 'Pixel', 'Turbo', 'Glitch']
["Go!"] * 3['Go!', 'Go!', 'Go!']

69. Guess the shape of the answer: The whole roster program

Estimation

Predict first

If yours prints your own six heroes in the same shape - you built it. Different names, same superpowers.

Commit before you compute: what does The whole roster program come out to? A rough magnitude and the right form is enough — the point is to have something concrete to be wrong about.

Correct: Read the program top to bottom

Why: A prediction you can defend turns the computation into a check rather than a leap of faith — and an answer that contradicts it is caught on the spot. Draft the squad, slice the starters, merge the recruits, then index, slice, and stamp - every tool from the hour in nine lines.

70. The whole roster program

Worked example

squad = ["Bolt", "Ace", "Nova", "Rex", "Zap", "Pixel"]
print("Starters:", squad[0:3])

squad2 = ["Turbo", "Glitch"]
team = squad + squad2
print("Full team:", team)

print("Captain:", team[0])
print("Bench:", team[3:])
print(["Go!"] * 3)

Read the program top to bottom

Why: Draft the squad, slice the starters, merge the recruits, then index, slice, and stamp - every tool from the hour in nine lines.

lineoutput
Starters:['Bolt', 'Ace', 'Nova']
Full team:['Bolt', 'Ace', 'Nova', 'Rex', 'Zap', 'Pixel', 'Turbo', 'Glitch']
Captain:Bolt
Bench:['Rex', 'Zap', 'Pixel', 'Turbo', 'Glitch']
chant['Go!', 'Go!', 'Go!']

If yours prints your own six heroes in the same shape - you built it. Different names, same superpowers.

71. Work backwards from the answer: The whole roster program

Reverse engineer

Discussion prompt

Work backwards. The example finished here:

Read the program top to bottom

What was it asked to do, and what must it have been given? Reconstruct the problem from its answer.

Hint: Every quantity in the result had to enter somewhere. Account for each one.

Answer:

If yours prints your own six heroes in the same shape - you built it. Different names, same superpowers.

72. Show it off

Concept

Last 5 minutes: run your roster and explain it line by line - what [:3] grabs and why the end index stays out, why + made the team longer but never did math, and what * 3 stamped out.

If you can explain those three, you just aced the exact three questions the diagnostic said were impossible.

73. Break it if you can: Show it off

Counterexample

Discussion prompt

Last 5 minutes: run your roster and explain it line by line - what [:3] grabs and why the end index stays out, why + made the team longer but never did math, and what * 3 stamped out.

That is stated as though it always holds. Do one of two things: produce a case where it fails, or say precisely what rules such a case out. "It just does" is not on the menu.

Hint: Hunt at the extremes first — zero, one, negative, empty, equal. If every extreme survives, the reason they survive is the proof.

Answer:

If you can explain those three, you just aced the exact three questions the diagnostic said were impossible.

74. Connect it up: Lists: Operations & Slicing

Connect it up

Draw it

One page, no notation unless you need it: draw how these connect — The list toolbox · Why lists matter at camp · What a list is · Lockers numbered from 0 · Indexing: grab one item. Put an arrow wherever one of them is what makes another possible, and label the arrow with why.

75. What you can do now

Recap

You can now grab any item with its index (starting at 0), slice exactly the chunk you want (end excluded - count is end minus start), join lists with +, repeat them with *, rewrite a locker, and grow a team with append. And you built a whole roster program with your own hands.

the three diagnostic trapsthe truth
[1, 2] + [3][1, 2, 3] - glue, not math
nums[1:3]items 1 and 2 only - end excluded
[0] * 3[0, 0, 0] - a stamp, not multiplication

Sources

  1. Python 3 docs - Sequence Types (list)
  2. Python 3 tutorial - An Informal Introduction (lists)
  3. Think Python 2e, Ch. 10 (Lists) — Allen B. Downey, Green Tea Press, 2015

Want this taught 1-on-1? Alexander tutors Python — $55/session, free consultation.

Book on Wyzant · Text (657) 465-8108