python · level 2 · unit 2 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
hands back a tidy copy of your list.
sorted(scores) gives you a new list in order: smallest first for numbers, A to Z for words. The original list is left exactly as it was.
Add reverse=True for order, biggest first, which is how a leaderboard puts the winner on top. reverse=True is a : you name the parameter and give it a value.
Words are sorted letter by letter, and every capital comes before every small letter. A list also has a .sort() method, which sorts the list itself instead of making a copy.
scores = [42, 97, 63, 85]
print(sorted(scores))
print(sorted(scores, reverse=True))
print(scores)
names = ["Zoe", "amy", "Bea"]
print(sorted(names)) Because capitals come first, "Zoe" sorts before "amy".
guess first
A copy or the real thing? What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
queue = ["Omar", "Lily", "Ben"]
tidy = sorted(queue)
print(queue)
queue.sort()
print(queue)
print(tidy == queue)
your turn
Your turn. Build a leaderboard.
Type five scores into the list. Then fill in the keyword argument that puts the biggest score first.
fix it
The playlist turned into None. Can you fix it?
.sort()sorts the list itself. What does it hand back?.sort()returnsNone, and line 2 stores thatNoneinsongs, throwing the list away.- Change line 2 to songs = sorted(songs), then run.
nailed it
you put everything in order. :D
sorted()hands back a new list in ascending order.reverse=Truegives descending order, biggest first..sort()sorts the list itself and returnsNone.
new words in your collection
- sorted
- ascending
- descending
- keyword argument
This lesson isn’t saved. A free account remembers every lesson you finish.