Practice mode Python

What is the output of the following Python code? a = ; b = a.sort(); print(a, b)

Correct answer B. [1, 2, 3] None

Explanation

list.sort() sorts the list in place and returns None, a convention Python uses for methods that mutate an object. So a becomes [1, 2, 3] while b is None. If you need a new sorted list and want to keep the original, use the built-in sorted(a) instead.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.