Repaso
Español
PythonCorePY-37

What does this print?

nums = [3, 1, 2]
result = nums.sort()
print(result)

Options

  1. [1, 2, 3]
  2. None (Correct option)
  3. [3, 1, 2]
  4. 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.

Key terms

Practice this question in Repaso