ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
print(sorted(['beta', 'Alpha', 'alpha', 'Beta'], key=str.lower))
- A. ['Alpha', 'beta', 'alpha', 'Beta']
- B. ['Beta', 'beta', 'Alpha', 'alpha']
- C. ['Alpha', 'alpha', 'beta', 'Beta']
- D. ['beta', 'Alpha', 'Beta', 'alpha']
Correct answer: C. ['Alpha', 'alpha', 'beta', 'Beta']
Sorting by the lowercase key groups alpha and beta together. sorted is stable, so Alpha stays before alpha, and beta stays before Beta.
More Python Programming questions
- What is printed? python x = 0.1 + 0.7 print(x, x 10, int(x 10), round(x 10))
- What is printed? python print(int('3' 3) + int('4' 2) 2, '3' 3 + '4' 2 2)
- What is printed? python print(len('%s' % 1.0), len('%g' % 1.0), len(repr(1.0)), str(1e5), '%g' % 1e5, '%g' % 1e6)
- What is printed? python def f(s): return s[::-1] == s print(f('abba'), f('ab' 2), f(''), f('a'))
- What is printed? python print(True + True, str(True) + '1', str(None), str([None]), repr('None'))
- What is printed? python print(sorted([0.5, -0.0, 0.0, -1]))