What does this print?
nums = [3, 1, 2]
result = nums.sort()
print(result)Options
- [1, 2, 3]
- None (Correct option)
- [3, 1, 2]
- TypeError
Answer
None
Why
nums.sort() sorts the list itself (in place) and returns None. That is a Python convention for methods that modify an object.
So result is None, while nums is now [1, 2, 3].
If you want a NEW sorted list and keep the original, use sorted(nums).
Remember
list.sort() → None. sorted(list) → new list.