Test your knowledge of Python edge cases!
In each of the 12 snippets below, one or more values is missing (marked with ???
). In each case, answer Yes or No: is it possible to replace the ???
with an expression using basic, built-in Python types (see Details below) that will make the snippet into exactly what you would see if you entered the text into the interpreter?
>>> x, y = ???
>>> x + y == y + x
False
Yes. This snippet is possible.
>>> x, y = [0], [1]
>>> x + y == y + x
False
>>> x = ???
>>> x < x
True
No. This snippet is impossible.
Now, technically, type("", (), {"__lt__": lambda a, b: True})()
would make Example question 2 work. But don't worry. This quiz is not about trick questions, and I'm not looking for "trick" answers like that. The missing values in this quiz are limited to the built-in types bool
, int
, list
, tuple
, dict
, set
, frozenset
, str
, and NoneType
, and values of these types.
In particular, the following are out of scope, and are not valid as missing values:
float
s, includingnan
andinf
- unicode (Strings, if they appear, only have ascii characters.)
- the
type
keyword - user-defined classes
lambda
s- anything using
globals
orlocals
- anything using
import
- anything that changes value just by inspecting it or iterating over it (iterators and generators)
- expressions with side effects (e.g.
eval
), including anything that redefines built-ins (obviously) bytes
arrays,buffer
s,range
s,memoryview
s, and dictionary views
Again, these are not trick questions. For snippets whose answers are Yes, this is possible, the missing values will clearly be in scope.
Assume the latest Python version (currently 3.5) if it matters. It shouldn't, though.
Make sure to write down your answers (Yes or No) before looking at the answers, so you're not tempted to cheat.
For a real challenge, try taking the quiz without using a Python interpreter to check your answers.
Good luck!
>>> x, y = ???
>>> min(x, y) == min(y, x)
False
>>> x = ???
>>> len(set(list(x))) == len(list(set(x)))
False
>>> x, s = ???
>>> s.add(x)
>>> type(x) in map(type, s)
False
>>> x, y = ???
>>> x < y and all(a >= b for a, b in zip(x, y))
True
>>> x, y = ???
>>> sum(0 * x, y) == y
False
>>> x = ???
>>> min(x) == min(*x)
False
>>> x, y, z = ???
>>> x * (y * z) == (x * y) * z
False
>>> x, y = ???
>>> y > max(x) and y in x
True
>>> x, y = ???
>>> any(x) and not any(x + y)
True
>>> x, y = ???
>>> x.count(y) <= len(x)
False
>>> x = ???
>>> all(filter(None, x))
False
>>> x, a, b, c = ???
>>> max(x) < max(x[a:b:c])
True
All done? Make sure you have your answers written down and check out the answers here.