Module 3 · Sorting lists: bubble sort · Lesson 2 of 2
Sorting a list
Here's the good news after all that: you'll almost never write a sort by hand. Python has fast, battle-tested sorting built in, and it comes in two flavours that are easy to mix up — so let's pin down the difference.
sorted(nums) is a function that returns a new sorted list and leaves your original exactly as it was. nums.sort() is a method that reorders the list in place — it changes nums itself and returns nothing. Reach for sorted() when you need to keep the original, and .sort() when you don't.
Both sort smallest-to-largest by default. Pass reverse=True to flip it, and note that strings sort alphabetically, numbers numerically — you just can't mix the two types in one list. Under the hood Python uses an algorithm far cleverer (and faster) than bubble sort, but the bubble sort you just watched is exactly the kind of thinking that makes it tick.
And that's a wrap on lists — from a row of boxes to sorting them in a single line. You now have variables, decisions, loops and collections: the core of practical Python.