PYTHON CORE · LESSON 39

Sorting

Function sorted · about 10 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Put the numbers in order.

sorted takes a list and returns a new list, where the items go in order.

nums = [3, 1, 2]
print(sorted(nums))

✦ It will be [1, 2, 3].

STEP 2

The old list remains

sorted does not change the original list. If desired, the result can be saved under a new name.

nums = [5, 2]
ordered = sorted(nums)
print(ordered)

✦ ordered stores [2, 5].

IN-CLASS PRACTICE

Type the code yourself

Dial the list and display a sorted copy.

nums = [4, 2, 3]
print(sorted(nums))
Open the editor for class work
HOMEWORK

Solve it yourself

Create nums from 3, 1, 2 in this order. Introduce a new sorted list through sorted.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework