Pre-COSMOS Day 4, for Cluster 10: Robot Inventors, covered in depth. It grows a list with append(), orders it with sort() and sort(reverse=True), and counts with len(), building to the big idea that in-place methods return None - so nums = nums.sort() wipes your data. It then builds the High-Score Board project, with a top-three stretch. Every snippet is runnable, and the outputs and error messages came verbatim from CPython 3.12.
Subject: Python · 69 slides · code lesson
Open the interactive version of this deck · Homework for this lesson
Objectives
As your robot runs, you pile up readings in a list. To manage that list you need the methods that change it in place. By the end you can:
1. Use append() to add one reading to the end of a list, in place.
2. Use sort() to order a list - and sort(reverse=True) for high-to-low.
3. Use len() to count how many items a list holds.
4. Explain why nums = nums.sort() throws your data away.
5. Build a High-Score Board: append scores, sort them, and print a ranked list.
Warm-up
Discussion prompt
Before we open List Methods: append, sort, len: without looking back, what was the main idea of for Loops, 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:
That session covers what a for loop does, range() and the off-by-one trap, and the accumulator pattern. Every snippet is runnable, and every trace table came from real execution.
Concept
A list holds many values in order, inside square brackets: [3, 1, 2]. Each item has a position, and the list can grow while your program runs.
list — An ordered, changeable collection of values. As a robot takes readings, you append each one onto a list so they pile up in order.
Counterexample
Discussion prompt
A list holds many values in order, inside square brackets: [3, 1, 2]. Each item has a position, and the list can grow while your program runs.
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.
Concept
Some list tools change the list itself - they add to it or rearrange it without making a new copy. That is called changing it in place.
in place (mutate) — To change the original list directly instead of building a new one. append() and sort() both mutate the list in place - the same list, now different.
Definition probe
Sort into buckets
Every line below is part of the definition of list or of in place (mutate) — one or the other, never both. Put each where it belongs.
Intuition
Picture your list as a whiteboard. append() walks up and writes one more item on it. sort() erases and rewrites the items in order. The board is changed - you are not handed a new board.
That is why these tools don't need to hand anything back: the work already happened to your list. Hold onto that idea - it explains the biggest trap in this lesson.
Analogy
Discussion prompt
Explain Your list is a whiteboard, not a photocopy 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 your list as a whiteboard. append() walks up and writes one more item on it. sort() erases and rewrites the items in order. The board is changed - you are not handed a new board.
Concept
nums.append(x) sticks x onto the end of nums, making the list one longer. It changes nums in place.
Explain it
Discussion prompt
Explain append(): add one item to the end 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:
nums.append(x) sticks x onto the end of nums, making the list one longer. It changes nums in place.
Ranking
Put in order
Put the moves of append in action into the order they have to happen.
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. nums is now [3, 1, 2, 4]. It grew in place - same list, one longer.
Worked example
nums = [3, 1, 2]
nums.append(4)
print(nums)Line 1: start with three items
Why: nums is [3, 1, 2] - positions 0, 1, 2.
Line 2: append(4) adds 4 to the end
Why: nums is now [3, 1, 2, 4]. It grew in place - same list, one longer.
Trace the list
Why: Verified by execution: prints [3, 1, 2, 4].
| step | code | nums now | len |
|---|---|---|---|
| 1 | nums = [3, 1, 2] | [3, 1, 2] | 3 |
| 2 | nums.append(4) | [3, 1, 2, 4] | 4 |
| 3 | print(nums) | [3, 1, 2, 4] | 4 |
Comparison
Comparison matrix
From append in action: refill the code column from what you know. The rest of the table is as it appeared.
| step | code | nums now | len |
|---|---|---|---|
| 1 | nums = [3, 1, 2] | [3, 1, 2] | 3 |
| 2 | nums.append(4) | [3, 1, 2, 4] | 4 |
| 3 | print(nums) | [3, 1, 2, 4] | 4 |
Concept
append() always adds its argument as a single item. If you append a whole list, that list becomes one item inside your list - it does not merge in.
Anomaly
Predict first
A student writes this, and it looks reasonable:
Goal: add 3 and 4 onto [1, 2].
It is wrong. Say what breaks — and say it before you turn the page.
Correct: nums is [1, 2, [3, 4]] - length 3, not 4.
To add the numbers separately, join with + (or append each one).
Why: nums is [1, 2, [3, 4]] - length 3, not 4. The [3, 4] is a single nested item, not two new numbers.
Trap
Goal: add 3 and 4 onto [1, 2].
nums = [1, 2]
nums.append([3, 4])
print(nums)
print(len(nums))append put the whole list in as one item
Why: nums is [1, 2, [3, 4]] - length 3, not 4. The [3, 4] is a single nested item, not two new numbers.
| expression | value |
|---|---|
| nums | [1, 2, [3, 4]] |
| len(nums) | 3 |
To add the numbers separately, join with + (or append each one).
nums = [1, 2]
nums = nums + [3, 4]
print(nums)
print(len(nums))+ builds one flat list
Why: nums is [1, 2, 3, 4] - length 4. (Or call nums.append(3) then nums.append(4) to add them one at a time.)
| expression | value |
|---|---|
| nums | [1, 2, 3, 4] |
| len(nums) | 4 |
Trade off
Comparison matrix
From Trap: appending a list nests it: every row here is a choice with a cost. Fill the value column, then say which row you would actually pick and what you give up for it.
| expression | value |
|---|---|
| nums | [1, 2, [3, 4]] |
| len(nums) | 3 |
Concept
nums.sort() rearranges nums from smallest to largest, in place. The same list is reordered - you don't get a new one back.
Pattern
Predict first
The table runs: 1 | nums = [3, 1, 2] | [3, 1, 2] · 2 | nums.sort() | [1, 2, 3]
In Sorting low to high, given the rows so far: what is the next one — the row where step is 3?
Correct: 3 | print(nums) | [1, 2, 3]
| step | code | nums now |
|---|---|---|
| 1 | nums = [3, 1, 2] | [3, 1, 2] |
| 2 | nums.sort() | [1, 2, 3] |
| 3 | print(nums) | [1, 2, 3] |
Why: The relationship between the columns, not the individual numbers, is what generates the next row. Smallest to largest: the same list becomes [1, 2, 3].
Worked example
nums = [3, 1, 2]
nums.sort()
print(nums)Line 2: sort() reorders in place
Why: Smallest to largest: the same list becomes [1, 2, 3].
Trace the list
Why: Verified by execution: prints [1, 2, 3].
| step | code | nums now |
|---|---|---|
| 1 | nums = [3, 1, 2] | [3, 1, 2] |
| 2 | nums.sort() | [1, 2, 3] |
| 3 | print(nums) | [1, 2, 3] |
Error analysis
Annotate
Walk the callouts on Sorting low to high. Each one is a place this is easy to get subtly wrong.
Concept
For a high-score board you want the biggest first. nums.sort(reverse=True) orders the list from largest to smallest, still in place.
Fill the middle
Fill in the blanks
From Sorting high to low — one line has had its right-hand side removed. Put it back.
nums = [3, 1, 2]
nums.sort(reverse=True)
print(nums)
Why: nums is what everything below it consumes, so the wrong expression here fails later and somewhere else. Verified by execution: prints [3, 2, 1].
Worked example
nums = [3, 1, 2]
nums.sort(reverse=True)
print(nums)reverse=True flips the order to largest-first
Why: Verified by execution: prints [3, 2, 1].
| step | code | nums now |
|---|---|---|
| 1 | nums = [3, 1, 2] | [3, 1, 2] |
| 2 | nums.sort(reverse=True) | [3, 2, 1] |
| 3 | print(nums) | [3, 2, 1] |
Concept
sort() also orders strings - alphabetically, A to Z. So "apple" comes before "banana". Same method, same in-place behavior.
Worked example
words = ["banana", "apple", "cherry"]
words.sort()
print(words)sort() puts the words in alphabetical order
Why: Verified by execution: ['apple', 'banana', 'cherry'].
| step | words now |
|---|---|
| before sort() | ["banana", "apple", "cherry"] |
| after sort() | ["apple", "banana", "cherry"] |
Anomaly
Predict first
A student writes this, and it looks reasonable:
A list with numbers AND text in it.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: sort() works by checking which item is smaller.
Keep a list all one type. Sort numbers with numbers, words with words.
Why: sort() works by checking which item is smaller. It has no rule for "a" versus 1, so it stops with an error.
Trap
A list with numbers AND text in it.
mixed = [1, "a", 2]
mixed.sort()Python can't compare a str and an int
Why: sort() works by checking which item is smaller. It has no rule for "a" versus 1, so it stops with an error.
| expression | result |
|---|---|
| [1, "a", 2].sort() | TypeError: '<' not supported between instances of 'str' and 'int' |
Keep a list all one type. Sort numbers with numbers, words with words.
nums = [2, 1, 3]
nums.sort()
print(nums)All ints compare cleanly
Why: Verified by execution: [1, 2, 3]. A high-score board is all numbers, so you are safe.
| expression | value |
|---|---|
| nums | [1, 2, 3] |
Break the constraint
Discussion prompt
The rule this trap just fixed:
Verified by execution: [1, 2, 3]. A high-score board is all numbers, so you are safe.
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:
sort() works by checking which item is smaller. It has no rule for "a" versus 1, so it stops with an error.
Concept
len(nums) hands back the number of items in the list.
Unlike append and sort, len() does not change the list - it gives you a count you can store, print, or do math with.
Worked example
print(len([3, 1, 2, 4]))
print(len([]))
print(len("robot"))len counts the items
Why: A four-item list gives 4; an empty list gives 0.
len also counts the characters in a string
Why: Verified by execution: len("robot") is 5 - same idea, how many pieces are inside.
| expression | value | meaning |
|---|---|---|
| len([3, 1, 2, 4]) | 4 | four items |
| len([]) | 0 | empty list |
| len("robot") | 5 | five characters |
Error analysis
Annotate
Walk the callouts on len() in action. Each one is a place this is easy to get subtly wrong.
Concept
Methods that change the list in place - append, sort - hand back None, the "nothing" value, because the result already happened to your list.
len() is different: it computes a number and gives it back. So you write count = len(nums), but you never write nums = nums.sort().
Intuition
append() and sort() are do-something tools: they act on your list and return nothing (None). len() is a give-something tool: it returns a value for you to keep.
One question settles every case: did the tool change the list, or compute an answer about it? Change it - use it on its own line. Compute - capture what it returns.
Explain it
Discussion prompt
Explain "Do something" vs "give something" 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:
append() and sort() are do-something tools: they act on your list and return nothing (None). len() is a give-something tool: it returns a value for you to keep.
Fill the middle
Fill in the blanks
From Proof: append and sort return None — one line has had its right-hand side removed. Put it back.
nums = [3, 1, 2]
print(nums.append(4))
print(nums.sort())
print(len(nums))
Why: nums is what everything below it consumes, so the wrong expression here fails later and somewhere else. append and sort do their job on nums, but the call evaluates to None - so that is what prints.
Worked example
nums = [3, 1, 2]
print(nums.append(4))
print(nums.sort())
print(len(nums))Printing the calls themselves shows None
Why: append and sort do their job on nums, but the call evaluates to None - so that is what prints.
len prints a real number
Why: Verified by execution: None, None, 4. len returns a value; the other two return None.
| call | prints | why |
|---|---|---|
| nums.append(4) | None | changed the list in place |
| nums.sort() | None | reordered the list in place |
| len(nums) | 4 | returns a count |
Comparison
Comparison matrix
From Proof: append and sort return None: refill the why column from what you know. The rest of the table is as it appeared.
| call | prints | why |
|---|---|---|
| nums.append(4) | None | changed the list in place |
| nums.sort() | None | reordered the list in place |
| len(nums) | 4 | returns a count |
Anomaly
Predict first
A student writes this, and it looks reasonable:
You want nums sorted, so you "save" the result.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: Your sorted list is thrown away.
Call sort() on its own line. It already changes nums - don't assign it.
Why: Your sorted list is thrown away. nums is now None, not a list - your data is gone.
Trap
You want nums sorted, so you "save" the result.
nums = [3, 1, 2]
nums = nums.sort()
print(nums)sort() returned None, and you stored None into nums
Why: Your sorted list is thrown away. nums is now None, not a list - your data is gone.
| expression | value | type |
|---|---|---|
| nums | None | NoneType |
Call sort() on its own line. It already changes nums - don't assign it.
nums = [3, 1, 2]
nums.sort()
print(nums)Let it mutate in place - no assignment
Why: nums is [1, 2, 3]. Same rule for append: write nums.append(x) on its own line, never x = nums.append(...).
| expression | value | type |
|---|---|---|
| nums | [1, 2, 3] | list |
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.
[3, 1, 2]. Each item has a position, and the list can grow while your program runs.; Some list tools change the list itself - they add to it or rearrange it without making a new copy. That is called changing it in place.; nums.append(x) sticks x onto the end of nums, making the list one longer. It changes nums in place.[1, 2].; A list with numbers AND text in it.Concept
Two ways to grow a list, with a key difference. nums.append(x) changes nums in place (and returns None). nums + [x] builds a brand-new list and leaves nums alone - so you must store it.
Use append when you are piling items onto one list over time - like readings or scores.
Analogy
Discussion prompt
Explain append vs +: change one, or make a new one 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:
Two ways to grow a list, with a key difference. nums.append(x) changes nums in place (and returns None). nums + [x] builds a brand-new list and leaves nums alone - so you must store it.
Fill the middle
Fill in the blanks
From append vs + — one line has had its right-hand side removed. Put it back.
a = [1, 2]
a.append(3)
print(a)
b = [1, 2] + [3]
print(b)
Why: b is what everything below it consumes, so the wrong expression here fails later and somewhere else. Verified by execution: a.append(3) changes a in place; [1, 2] + [3] makes a new list stored in b.
Worked example
a = [1, 2]
a.append(3)
print(a)
b = [1, 2] + [3]
print(b)Both end up [1, 2, 3], but the mechanism differs
Why: Verified by execution: a.append(3) changes a in place; [1, 2] + [3] makes a new list stored in b. The original [1, 2] in the + version is untouched.
| line | what it does | result |
|---|---|---|
| a.append(3) | changes a in place | a is [1, 2, 3] |
| [1, 2] + [3] | builds a new list | b is [1, 2, 3] |
Concept
A slice copies part of a list. nums[:3] gives the first three items as a new list.
Combine it with a high-to-low sort and nums[:3] is your top three - exactly the stretch goal for the board.
Counterexample
Discussion prompt
A slice copies part of a list. nums[:3] gives the first three items as a new list.
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:
Combine it with a high-to-low sort and nums[:3] is your top three - exactly the stretch goal for the board.
Worked example
scores = [90, 80, 70, 60, 50]
print(scores[:3])scores[:3] copies the first three items
Why: Verified by execution: [90, 80, 70]. Because the list is already sorted high-to-low, those three are the podium.
| expression | value |
|---|---|
| scores[:3] | [90, 80, 70] |
| scores[:1] | [90] |
Pattern
1. Build with append on its own line
Why: scores.append(x) adds one item in place. Never store what append returns - it is None.
2. Order with sort on its own line
Why: scores.sort() is low-to-high; scores.sort(reverse=True) is high-to-low. Never store what sort returns - also None.
3. Count with len and KEEP the result
Why: n = len(scores). len gives back a value, so capture it.
4. Ask: change the list, or compute about it?
Why: Change it (append/sort) -> own line. Compute about it (len) -> capture the return value.
5. Keep a list one type
Why: All numbers, or all words - so sort() can compare the items without a TypeError.
Real world
Discussion prompt
Outside this lesson: where does List Methods: append, sort, len 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 in-place method recipe 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:
Pre-COSMOS Day 4 (Cluster 10: Robot Inventors), in depth. Growing a list with append(), ordering it with sort() and sort(reverse=True), counting with len(), and the big idea that in-place methods return None - so nums = nums.sort() wipes your data.
Ranking
Put in order
Put the moves of Build it (1/4): start empty, append five scores into the order they have to happen.
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. scores is [] - length 0. Every game will append onto it.
Worked example
The High-Score Board. Start with nothing and append each game's score.
scores = []
scores.append(50)
scores.append(90)
scores.append(70)
scores.append(60)
scores.append(80)
print(scores)
print(len(scores))Line 1: start from an empty list
Why: scores is [] - length 0. Every game will append onto it.
Lines 2-6: each append adds one score to the end
Why: The list grows by one item each time, in place - no new lists, no assignments.
Trace the list as it grows
Why: Verified by execution: prints [50, 90, 70, 60, 80] and 5.
| after | scores | len |
|---|---|---|
| start | [] | 0 |
| append(50) | [50] | 1 |
| append(90) | [50, 90] | 2 |
| append(70) | [50, 90, 70] | 3 |
| append(60) | [50, 90, 70, 60] | 4 |
| append(80) | [50, 90, 70, 60, 80] | 5 |
Cost model
Annotate
In Build it (1/4): start empty, append five scores, before reading the notes: mark where the time actually goes. Which line dominates?
Pattern
Predict first
The table runs: 1 | scores = [50, 90, 70, 60, 80] | [50, 90, 70, 60, 80] · 2 | scores.sort(reverse=True) | [90, 80, 70, 60, 50]
In Build it (2/4): sort the board high to low, given the rows so far: what is the next one — the row where step is 3?
Correct: 3 | print(scores) | [90, 80, 70, 60, 50]
| step | code | scores now |
|---|---|---|
| 1 | scores = [50, 90, 70, 60, 80] | [50, 90, 70, 60, 80] |
| 2 | scores.sort(reverse=True) | [90, 80, 70, 60, 50] |
| 3 | print(scores) | [90, 80, 70, 60, 50] |
Why: The relationship between the columns, not the individual numbers, is what generates the next row. Verified by execution: [90, 80, 70, 60, 50] - in place, on its own line.
Worked example
scores = [50, 90, 70, 60, 80]
scores.sort(reverse=True)
print(scores)sort(reverse=True) ranks the scores, best first
Why: Verified by execution: [90, 80, 70, 60, 50] - in place, on its own line.
| step | code | scores now |
|---|---|---|
| 1 | scores = [50, 90, 70, 60, 80] | [50, 90, 70, 60, 80] |
| 2 | scores.sort(reverse=True) | [90, 80, 70, 60, 50] |
| 3 | print(scores) | [90, 80, 70, 60, 50] |
Estimation
Predict first
Put it together: sort high-to-low, then announce how many games with len and the winner with scores[0].
Commit before you compute: what does Build it (3/4): the ranked board with len come out to? A rough magnitude and the right form is enough — the point is to have something concrete to be wrong about.
Correct: Sort first, then read from the ordered list
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. After sorting, scores[0] is the highest.
Worked example
Put it together: sort high-to-low, then announce how many games with len and the winner with scores[0].
scores = [50, 90, 70, 60, 80]
scores.sort(reverse=True)
print("Games played:", len(scores))
print("Top score:", scores[0])
print(scores)Sort first, then read from the ordered list
Why: After sorting, scores[0] is the highest. len(scores) counts the games. Verified by execution.
| line | prints |
|---|---|
| print("Games played:", len(scores)) | Games played: 5 |
| print("Top score:", scores[0]) | Top score: 90 |
| print(scores) | [90, 80, 70, 60, 50] |
Reverse engineer
Discussion prompt
Work backwards. The example finished here:
Sort first, then read from the ordered list
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:
Put it together: sort high-to-low, then announce how many games with len and the winner with scores[0].
Estimation
Predict first
Stretch goal: print only the podium. Sort high-to-low, then slice the first three.
Commit before you compute: what does Build it (4/4): stretch - top three only come out to? A rough magnitude and the right form is enough — the point is to have something concrete to be wrong about.
Correct: Slice after sorting to grab the best three
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. Verified by execution: scores[:3] is [90, 80, 70], printed as Podium: [90, 80, 70].
Worked example
Stretch goal: print only the podium. Sort high-to-low, then slice the first three.
scores = [50, 90, 70, 60, 80]
scores.sort(reverse=True)
top3 = scores[:3]
print("Podium:", top3)Slice after sorting to grab the best three
Why: Verified by execution: scores[:3] is [90, 80, 70], printed as Podium: [90, 80, 70].
| step | value |
|---|---|
| scores after sort | [90, 80, 70, 60, 50] |
| scores[:3] | [90, 80, 70] |
| printed | Podium: [90, 80, 70] |
Trade off
Comparison matrix
From Build it (4/4): stretch - top three only: every row here is a choice with a cost. Fill the value column, then say which row you would actually pick and what you give up for it.
| step | value |
|---|---|
| scores after sort | [90, 80, 70, 60, 50] |
| scores[:3] | [90, 80, 70] |
| printed | Podium: [90, 80, 70] |
Check
Trace the list, then choose.
nums = [5, 2]
nums.append(8)
print(nums)| expression | value |
|---|---|
| nums | ? |
Check your understanding
What does this print?
Answer: A
Why: append adds its argument as one item at the END of the list, in place. So nums becomes [5, 2, 8]. Verified by execution.
Check
Watch the assignment on line 2 carefully.
nums = [3, 1, 2]
nums = nums.sort()
print(nums)| step | nums |
|---|---|
| 1 | ? |
| 2 | ? |
Check your understanding
What does this print?
Answer: A
Why: sort() reorders the list in place and returns None. Storing that None into nums throws the list away, so nums is None and print shows None. Fix it by calling nums.sort() on its own line. Verified by execution.
Comparison
Comparison matrix
From Check yourself: the None trap: refill the nums column from what you know. The rest of the table is as it appeared.
| step | nums |
|---|---|
| 1 | ? |
| 2 | ? |
Check
Remember what append does with a list argument.
data = [1, 2, 3]
data.append([4, 5])
print(len(data))| expression | value |
|---|---|
| len(data) | ? |
Check your understanding
What does len(data) print?
Answer: A
Why: append adds the whole list [4, 5] as ONE item, so data is [1, 2, 3, [4, 5]] - four items. len counts top-level items, not the numbers inside the nested list. Verified by execution.
Check
Note the reverse=True.
nums = [4, 1, 7, 3]
nums.sort(reverse=True)
print(nums)| expression | value |
|---|---|
| nums | ? |
Check your understanding
What does this print?
Answer: A
Why: sort(reverse=True) orders the list from largest to smallest, in place: [7, 4, 3, 1]. Verified by execution.
Check
How many items end up in the list?
scores = []
scores.append(80)
scores.append(95)
scores.append(60)
print(len(scores))| after | len |
|---|---|
| all three appends | ? |
Check your understanding
What does len(scores) print?
Answer: A
Why: Three appends add three items to the empty list, so it holds [80, 95, 60] and len is 3. len counts how many items there are, not their total. Verified by execution.
Connect it up
Draw it
One page, no notation unless you need it: draw how these connect — The in-place method recipe · A list is an ordered collection · The big idea: changing a list "in place" · Your list is a whiteboard, not a photocopy · append(): add one item to the end. Put an arrow wherever one of them is what makes another possible, and label the arrow with why.
Recap
append() adds one item in place; sort() reorders in place; both return None, so call them on their own line. len() returns a count you can keep.
| You want | Write | Result |
|---|---|---|
| add a score | scores.append(90) | one item added, in place |
| order high-to-low | scores.sort(reverse=True) | list reordered, in place |
| count the games | n = len(scores) | n holds the number |
| WRONG: save sort's result | scores = scores.sort() | scores becomes None - data lost |
| top three | scores[:3] | first three after sorting |
The one rule to remember: if a method changes the list, use it on its own line; if it computes something (like len), keep what it returns. Now go build the high-score board.
Want this taught 1-on-1? Alexander tutors Python — $55/session, free consultation.