Skip to main content
ThePythonBook/Assessment

Advanced Python Assessment

Test your mastery of advanced Python concepts including iterators, generators, decorators, context managers, closures, and more.

Progress
1040 XP0/12
#1Countdown Iterator
Write Code

Write a class Countdown that acts as an iterator counting down from a given start value to 1 (inclusive).

  • Countdown(n) should accept a positive integer.
  • Iterating over the object should yield values from n down to 1.
  • After all values are exhausted the iterator should raise StopIteration.
  • Loading editor...
    #2Fibonacci Generator
    Write Code

    Write a generator function fibonacci(n) that yields the first n Fibonacci numbers.

    The sequence starts with 0 and 1: 0, 1, 1, 2, 3, 5, 8, ...

    Loading editor...
    #3Call-Counter Decorator
    Write Code

    Write a decorator count_calls that tracks how many times a function has been called.

  • The decorated function should have a .call_count attribute that starts at 0.
  • Each call to the function increments .call_count by 1.
  • The function should still return its normal result.
  • Loading editor...
    #4Retry Decorator
    Write Code

    Write a decorator factory retry(max_attempts) that retries a function up to max_attempts times if it raises an exception.

  • If the function succeeds, return its result immediately.
  • If all attempts fail, re-raise the last exception.
  • Print "Attempt {n} failed" for each failed attempt (1-indexed).
  • Loading editor...
    #5Fix the Broken Generator Pipeline
    Fix the Bug

    The code below is supposed to create a generator pipeline that:

    1. Generates numbers 1-10

    2. Filters to keep only even numbers

    3. Squares each number

    But it produces wrong results. Find and fix the bug.

    Loading editor...
    #6Fix the Broken Decorator
    Fix the Bug

    This decorator is supposed to double the return value of any function, but it doesn't work. Find and fix the bug.

    Loading editor...
    #7Fix the Context Manager
    Fix the Bug

    This context manager is meant to track whether we are inside the managed block. It sets tracker.active to True on entry and False on exit. But the exit never runs. Find and fix the bug.

    Loading editor...
    #8Closure Variable Capture
    Predict Output

    What does this code print? Think carefully about how closures capture variables.

    Loading editor...
    #9Walrus Operator in Comprehension
    Predict Output

    What does this code print? Pay attention to the walrus operator (:=) and how it assigns and returns a value simultaneously.

    Loading editor...
    #10Extended Unpacking
    Predict Output

    What does this code print? Think about how starred unpacking works with nested structures.

    Loading editor...
    #11Refactor to Generator Expression
    Refactor

    Refactor this code to use a single generator expression passed directly to sum() instead of the explicit loop. The result should be the same: the sum of squares of even numbers from 1 to 20.

    Your solution must:

  • Use a single sum() call with a generator expression inside
  • Produce the same final printed result
  • Be no more than 2 lines (assignment + print)
  • Loading editor...
    #12Refactor with Dictionary Unpacking
    Refactor

    Refactor the merge_configs function to use dictionary unpacking (** operator) instead of the manual loop. The function should merge defaults with overrides, where overrides take precedence.

    Your refactored function body should be a single return statement using {**dict1, **dict2} syntax.

    Loading editor...