Rocket is proud of his selection sort. Then Nova whispers that Python has its own sorting tools.
"So I did all that for nothing?" Rocket says.
Raven shakes her head. "What do you notice now when you sort cards by hand?"
"I know what is happening inside," Rocket says. "I can count the work."
Nova twirls. "Exactly! Here is a hint. One tool makes a new list, and one changes the old list."
ids = [27, 3, 15, 8] print(sorted(ids)) print(ids) ids.sort() print(ids) print(sorted(ids, reverse=True))
Output: [3, 8, 15, 27] [27, 3, 15, 8] [3, 8, 15, 27] [27, 15, 8, 3]
| Statement | True or false? |
|---|---|
| sorted() changes the original list. | ? |
| .sort() changes the original list. | ? |
| sorted(ids, reverse=True) puts the biggest number first. | ? |
Words sort in alphabet order. But every capital letter comes before every small letter.
print(sorted(["Iris", "Ember", "Halo"])) print(sorted(["glow", "Halo", "ember"]))
Output: ['Ember', 'Halo', 'Iris'] ['Halo', 'ember', 'glow']
names = ["Pip", "ash", "Juno"] print(sorted(names)) nums = [5, 1, 4] nums.sort(reverse=True) print(nums) print(sorted([3, 3, 1]))
| Statement | True or false? |
|---|---|
| sorted([3, 3, 1]) keeps both 3s. | ? |
| nums.sort() makes a new list and keeps the old one. | ? |
| reverse=True works with sorted() and .sort(). | ? |
| "Juno" sorts before "Pip". | ? |
Great job! Now you can sort by hand, in your own code, or with one Python word. Tomorrow we sort star records and then search them.