Already know some Python? Answer these 13 questions, about 7 minutes. Each topic you get fully right is marked Known on the Python roadmap , so you can skip it. Each one you miss is listed for you to study.
Don’t guess. Pick “I don’t know yet” when you’re not sure: a lucky guess would mark a topic Known that you still need to learn.
The 13 topics it checks Classes: inheritance, MRO, super() and dunder methods @property, class methods and static methods Dataclasses Iterators and generators Closures Decorators, with arguments and functools.wraps Context managers collections, itertools and functools Type hints is vs ==, shallow vs deep copy Logging pytest basics pandas basics: DataFrames, filtering, groupby and merge Question 1 of 13 · Predict the output What does this print? class A:
def hi(self):
return "A"
class B(A):
def hi(self):
return "B" + super().hi()
class C(A):
def hi(self):
return "C" + super().hi()
class D(B, C):
pass
print(D().hi())
BCA BA BACA CBA I don’t know yet
Question 2 of 13 · Multiple choice Code all over the app writes item.price = x. You need to reject negative prices. What keeps every caller unchanged? Add a set_price() method and ask everyone to use it. Make price a @property with a setter that checks the value. Make price a @staticmethod. Check the price in __repr__. I don’t know yet
Question 3 of 13 · Spot the bug Why does Python refuse to create this class? from dataclasses import dataclass
@dataclass
class Cart:
owner: str
items: list = []
owner needs a default value. Dataclasses can’t hold lists. A list default would be shared by every Cart; use field(default_factory=list). The decorator needs brackets. I don’t know yet
Question 4 of 13 · Predict the output What does this print? nums = (n * n for n in range(4))
print(sum(nums), sum(nums))
14 14 0 14 An error 14 0 I don’t know yet
Question 5 of 13 · Predict the output What does this print? fns = []
for i in range(3):
fns.append(lambda: i)
print([f() for f in fns])
[0, 1, 2] [0, 0, 0] [2, 2, 2] An error I don’t know yet
Question 6 of 13 · Spot the bug After adding @timed, every decorated function returns None. Why? def timed(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = time.perf_counter()
func(*args, **kwargs)
print(time.perf_counter() - start)
return wrapper
wrapper calls func but never returns its result. functools.wraps hides the result. timed must return wrapper(). *args can’t pass keyword arguments. I don’t know yet
Question 7 of 13 · Multiple choice In a @contextmanager function, where must the clean-up code go so it runs even when the with block raises? Straight after yield. Before yield. In __init__. In a finally block around the yield. I don’t know yet
Question 8 of 13 · Multiple choice You need the 3 most common words in a list of a million words. What is shortest and fast? sorted(set(words), key=words.count)[-3:] collections.Counter(words).most_common(3) itertools.groupby(words) max(words) three times I don’t know yet
Question 9 of 13 · Predict the output What happens when this runs? def double(x: int) -> int:
return x * 2
print(double("ab"))
It prints abab TypeError: x must be int It prints 0 A SyntaxError I don’t know yet
Question 10 of 13 · Predict the output What does this print? a = [[0], [0]]
b = a.copy()
b[0].append(1)
print(a, a == b, a is b)
[[0], [0]] False False [[0, 1], [0]] True True [[0, 1], [0]] True False [[0], [0]] True False I don’t know yet
Question 11 of 13 · Multiple choice Which line belongs in a library module? logging.basicConfig(level=logging.DEBUG) print("paid", user) logging.info(f"paid {user}") log = logging.getLogger(__name__), then log.info("paid %s", user) I don’t know yet
Question 12 of 13 · Spot the bug This test passes even when parse("") raises nothing. Why? def test_empty():
try:
parse("")
assert False, "should raise"
except Exception:
pass
assert False never runs. AssertionError is an Exception, so the except swallows the failure. Tests can’t use try. pass should be return. I don’t know yet
Question 13 of 13 · Multiple choice A pandas report loops with iterrows over 3 million rows and takes 20 minutes. What is the fix for total amount per region? df.groupby("region")["amount"].sum() Use apply instead of iterrows. Loop over regions and filter the table for each one. Convert the DataFrame to a list first. I don’t know yet
See my results Answered 0 of 13.