After b = a where a is a list, which statement is true?
Assignment binds a name; use a.copy() or a[:] for a copy.
Tricky Python Output Questions · Exercise 1 of 4
Ten multiple-choice questions on the rules behind tricky Python outputs: references, defaults, late binding, rounding, floor division, truthiness and evaluation order. Pick an option to check your answer instantly.
After b = a where a is a list, which statement is true?
Assignment binds a name; use a.copy() or a[:] for a copy.
Why does def f(x, acc=[]) keep data between calls?
Use acc=None and create a new list inside the function.
When does a lambda created inside a loop read the loop variable?
So lambdas created in a loop usually all see the final value.
What are round(1.5) and round(2.5)?
Halves go to the even neighbour.
What are -7 // 2 and int(-7 / 2)?
// floors toward minus infinity; int() truncates toward zero.
What is the value of "" or [] or 0?
or returns the first truthy operand, or the last operand if all are falsy.
What does print([3, 1, 2].sort()) print?
sort() works in place and returns None; sorted() returns a new list.
How many keys does {1: "a", 1.0: "b", True: "c"} have?
The three keys are equal and hash the same, so later values overwrite earlier ones.
Which expression is True?
An empty list is falsy, but it is never equal to False or None.
A generator expression g is passed to sum() twice. What does the second call return?
Generators produce their values only once.
Timed online tests with instant scores and explanations.