Paul's Python CoursePPC

sorted() and .sort()

Not met yet

sorted(items) hands back a new list in order and leaves items as it was. items.sort() puts items itself in order and hands back None, so items = items.sort() loses the list. Both take reverse=True. Text sorts letter by letter, and capitals come before every lower-case letter.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks .sort() hands back the sorted list, so x = x.sort() keeps it.
  • Thinks .sort() makes a sorted copy somewhere and leaves the list itself as it was.
  • Thinks sorted(x) rearranges x itself.
  • Thinks sorting text ignores capital letters, rather than putting every capital before every lower-case letter.

Where it comes up