What does `list.sort()` return?
Answer & explanation
Answer: None; it sorts the list in place
`list.sort()` mutates the list and returns `None`; `sorted(iterable)` returns a new list.
Test Python collections, slicing, functions, iterators, and language fundamentals.
A random set of up to 10 questions from Python. Your answers stay on this page.
Open a topic above to explore the complete question bank.
Answer: None; it sorts the list in place
`list.sort()` mutates the list and returns `None`; `sorted(iterable)` returns a new list.
Answer: is
`is` tests object identity. `==` tests equality according to the objects’ equality behavior.
def add(value, items=[]):
items.append(value)
return items
add(1)
add(2)
print(add(3))Answer: [1, 2, 3]
The default list is created once when the function is defined, so values appended by earlier calls remain in it.
Answer: else
`else` runs if no exception occurred. `finally` runs regardless of success or failure.
Answer: Produces a value and suspends the generator
`yield` produces a value while preserving generator state for the next iteration.
for name ___ names:
print(name)Answer: in
`for name in names` iterates over the elements of the list.