ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
print(sorted(['10', '9', '1.5']), sorted(['10', '9', '1.5'], key=float))
  1. A. ['10', '9', '1.5'] ['1.5', '9', '10']
  2. B. ['1.5', '9', '10'] ['1.5', '10', '9']
  3. C. ['1.5', '10', '9'] ['1.5', '9', '10']
  4. D. ['1.5', '9', '10'] ['10', '9', '1.5']

Correct answer: C. ['1.5', '10', '9'] ['1.5', '9', '10']

Plain sorting compares strings character by character, so '1.5' < '10' < '9'. Sorting with key=float compares numeric values, giving 1.5, 9, 10.

More Python Programming questions

Exams that ask this topic