Sorted Function
Predict Output Medium
Question
What does this code print?
Code
nums = [3, 1, 2]
print(sorted(nums, reverse=True))
Hint
sorted with reverse=True sorts descending.
Explanation
sorted([3, 1, 2], reverse=True) sorts in descending order: [3, 2, 1]. sorted returns a new list without modifying the original. For custom sorting, use key: sorted(items, key=lambda x: x["age"]).
Related Resources
Related Lessons
- Lists
Learn Python lists.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.