Practice / py

Python practice

Every Python exercise, built from a topic you can open beside it.

139 exercises runs offline
py Quiz new In def route(path, /, version, *parts, strict=True, **metadata), which classification is … Collect, unpack, and forward variable arguments without hiding a function's contract or accepting misspelled options. intermediate 3 min · from *args and **kwargs py Quiz new What happens when capture is defined as def capture(**metadata) and called as capture(**{… Collect, unpack, and forward variable arguments without hiding a function's contract or accepting misspelled options. advanced 3 min · from *args and **kwargs py Predict the output new What does this program print? Collect, unpack, and forward variable arguments without hiding a function's contract or accepting misspelled options. beginner 2 min · from *args and **kwargs py Review AI code new Review a generated retry wrapper Collect, unpack, and forward variable arguments without hiding a function's contract or accepting misspelled options. advanced 10 min · from *args and **kwargs py Spot the bug new Spot the bug in connection options Collect, unpack, and forward variable arguments without hiding a function's contract or accepting misspelled options. intermediate 4 min · from *args and **kwargs py Predict the output new In what order does this program print its four lines? Organize I/O with coroutines, tasks, and structured concurrency while handling timeouts, cancellation, blocking calls, and concurrency limits. beginner 2 min · from asyncio py Review AI code new Review a generated async URL fetcher Organize I/O with coroutines, tasks, and structured concurrency while handling timeouts, cancellation, blocking calls, and concurrency limits. advanced 6 min · from asyncio py Spot the bug new Spot the bug in the async job batch Organize I/O with coroutines, tasks, and structured concurrency while handling timeouts, cancellation, blocking calls, and concurrency limits. intermediate 4 min · from asyncio py Quiz new What normally happens when one child in an active TaskGroup raises ValueError? Organize I/O with coroutines, tasks, and structured concurrency while handling timeouts, cancellation, blocking calls, and concurrency limits. intermediate 1 min · from asyncio py Quiz new Where should code catch the TimeoutError produced by asyncio.timeout()? Organize I/O with coroutines, tasks, and structured concurrency while handling timeouts, cancellation, blocking calls, and concurrency limits. intermediate 1 min · from asyncio py Quiz new What does [n * n for n in range(6) if n % 2] produce? Quiz intermediate 1 min · from checkpoint-1 py Quiz new Which pair is produced by (7 // 3, 7 % 3)? Quiz beginner 1 min · from checkpoint-1 py Fill in new Enter the Python expression that creates an empty set Fill in intermediate 1 min · from checkpoint-1 py Quiz new When does an else clause attached to a for loop run? Quiz intermediate 1 min · from checkpoint-1 py Quiz new After a = [1], b = a, and b.append(2), what is a? Quiz beginner 1 min · from checkpoint-1 py Fill in new In range(2, 5), enter the first excluded stop value Fill in beginner 1 min · from checkpoint-1 py Quiz new What is bool("False")? Quiz beginner 1 min · from checkpoint-1 py Fill in new Name the exception raised by {[]}, where a list is used as a set element Fill in advanced 1 min · from checkpoint-1 py Quiz new A function appends to a default parameter items=[]. What happens across calls that omit i… Quiz beginner 1 min · from checkpoint-2 py Fill in new Which keyword lets an inner function rebind a name in its nearest enclosing function scop… Fill in intermediate 1 min · from checkpoint-2 py Fill in new Name the exception raised when next() advances an exhausted iterator Fill in advanced 1 min · from checkpoint-2 py Quiz new What does calling a generator function do before next() or iteration begins? Quiz advanced 1 min · from checkpoint-2 py Quiz new What does [f() for f in [lambda: i for i in range(3)]] produce? Quiz intermediate 1 min · from checkpoint-2 py Quiz new For def render(source, /, *, compact=False), which call is valid? Quiz beginner 1 min · from checkpoint-2 py Fill in new Enter the four-letter name for Python’s Local, Enclosing, Global, Built-in lookup order Fill in intermediate 1 min · from checkpoint-2 py Quiz new Inside def collect(*values, **labels), which container types are values and labels? Quiz intermediate 1 min · from checkpoint-2 py Quiz new May a method decorated with @abstractmethod contain a concrete implementation? Quiz advanced 1 min · from checkpoint-3 py Fill in new Name the exception raised when a subclass still has an unimplemented abstract method and … Fill in advanced 1 min · from checkpoint-3 py Quiz new Two instances initially read the class attribute status = "new". What does first.status =… Quiz beginner 1 min · from checkpoint-3 py Quiz new For an ordinary instance method, what does Python do when code calls account.deposit(10)? Quiz beginner 1 min · from checkpoint-3 py Fill in new Name the magic method that defines an object’s developer-oriented representation Fill in intermediate 1 min · from checkpoint-3 py Quiz new A frozen dataclass has a field values: list[int]. Which statement is correct? Quiz intermediate 1 min · from checkpoint-3 py Fill in new Which dataclasses field() argument creates a fresh list for every instance? Fill in intermediate 1 min · from checkpoint-3 py Quiz new In cooperative multiple inheritance, which implementation does zero-argument super() sear… Quiz intermediate 1 min · from checkpoint-3 py Quiz new In with manager as resource, where does resource come from? Quiz beginner 1 min · from checkpoint-4 py Fill in new Which boolean value returned by __exit__ suppresses an exception from the with block? Fill in beginner 1 min · from checkpoint-4 py Quiz new A logger accepts DEBUG and above, but its only handler is set to WARNING. What happens to… Quiz advanced 1 min · from checkpoint-4 py Quiz new Which statement correctly distinguishes json.dumps() from json.dump()? Quiz intermediate 1 min · from checkpoint-4 py Quiz new What does pytest do if code inside with pytest.raises(ValueError) completes normally? Quiz advanced 1 min · from checkpoint-4 py Fill in new By default, what SystemExit status does argparse use for an invalid value such as --count… Fill in intermediate 1 min · from checkpoint-4 py Quiz new What does Path("reports") / "daily.json" do? Quiz beginner 1 min · from checkpoint-4 py Quiz new What happens when Path("notes.txt").write_text("new") targets an existing file? Quiz intermediate 1 min · from checkpoint-4 py Fill in new Which typing construct captures a callable’s complete parameter signature so a decorator … Fill in intermediate 1 min · from checkpoint-5 py Quiz new What does calling an async def function return before it is awaited? Quiz intermediate 1 min · from checkpoint-5 py Quiz new Which statement about concurrent.futures is correct? Quiz advanced 1 min · from checkpoint-5 py Quiz new What happens by default when a function annotated def total(count: int) is called with "3… Quiz beginner 1 min · from checkpoint-5 py Fill in new Name the threading primitive commonly used to make a multi-step shared-state update mutua… Fill in advanced 1 min · from checkpoint-5 py Fill in new Name the typing construct that describes structural subtyping without requiring explicit … Fill in beginner 1 min · from checkpoint-5 py Quiz new What normally happens when one child in an active asyncio.TaskGroup raises ValueError? Quiz intermediate 1 min · from checkpoint-5 py Quiz new On a standard GIL-enabled CPython build, which default choice best matches the workload? Quiz intermediate 1 min · from checkpoint-5 py Quiz new What does a Python closure capture for a free variable? Closures let functions retain access to enclosing bindings; understand cells, nonlocal, and late binding to keep state and callbacks correct. intermediate 1 min · from Closures py Quiz new An inner function executes count += 1, but count is bound only in the enclosing function.… Closures let functions retain access to enclosing bindings; understand cells, nonlocal, and late binding to keep state and callbacks correct. intermediate 1 min · from Closures py Predict the output new What does this program print? Closures let functions retain access to enclosing bindings; understand cells, nonlocal, and late binding to keep state and callbacks correct. beginner 2 min · from Closures py Review AI code new Review generated retry handlers Closures let functions retain access to enclosing bindings; understand cells, nonlocal, and late binding to keep state and callbacks correct. intermediate 8 min · from Closures py Quiz new What must a context manager do if __enter__() fails after acquiring its first subresource? Context managers bind resource acquisition and required cleanup to a with block, including exception flow, generator wrappers, dynamic stacks, and async release. intermediate 3 min · from Context managers py Quiz new Why should dynamic acquisition call stack.enter_context() immediately for each resource? Context managers bind resource acquisition and required cleanup to a with block, including exception flow, generator wrappers, dynamic stacks, and async release. intermediate 3 min · from Context managers py Predict the output new What does this program print? Context managers bind resource acquisition and required cleanup to a with block, including exception flow, generator wrappers, dynamic stacks, and async release. intermediate 3 min · from Context managers py Review AI code new Review a generated batch publisher Context managers bind resource acquisition and required cleanup to a with block, including exception flow, generator wrappers, dynamic stacks, and async release. advanced 12 min · from Context managers py Spot the bug new Spot the bug in the report context manager Context managers bind resource acquisition and required cleanup to a with block, including exception flow, generator wrappers, dynamic stacks, and async release. intermediate 5 min · from Context managers py Quiz new What is the value of result after result = [] or "fallback"? Use branches, iteration, loop control, and structural pattern matching precisely while avoiding nontermination and misleading truth tests. beginner 1 min · from Control flow py Quiz new Inside match status, what does case READY: normally mean? Use branches, iteration, loop control, and structural pattern matching precisely while avoiding nontermination and misleading truth tests. intermediate 1 min · from Control flow py Predict the output new What does this program print? Use branches, iteration, loop control, and structural pattern matching precisely while avoiding nontermination and misleading truth tests. beginner 2 min · from Control flow py Review AI code new Review a generated command dispatcher Use branches, iteration, loop control, and structural pattern matching precisely while avoiding nontermination and misleading truth tests. advanced 6 min · from Control flow py Spot the bug new Spot the bug in archived-record removal Use branches, iteration, loop control, and structural pattern matching precisely while avoiding nontermination and misleading truth tests. beginner 4 min · from Control flow py Quiz new A function will only add and remove keys in a dictionary; it will not mutate any values. … How Python copies object graphs, preserves shared references, handles cycles, and supports field replacement. beginner 1 min · from Shallow and deep copy py Predict the output new What does this program print? How Python copies object graphs, preserves shared references, handles cycles, and supports field replacement. beginner 2 min · from Shallow and deep copy py Quiz new After copy.replace(record, status="sent"), what should you assume about an unchanged muta… How Python copies object graphs, preserves shared references, handles cycles, and supports field replacement. intermediate 1 min · from Shallow and deep copy py Review AI code new Review generated work-order copier How Python copies object graphs, preserves shared references, handles cycles, and supports field replacement. advanced 6 min · from Shallow and deep copy py Spot the bug new Spot the bug in the deep-copy hook How Python copies object graphs, preserves shared references, handles cycles, and supports field replacement. intermediate 4 min · from Shallow and deep copy py Quiz new Why should a transparent decorator for async def usually return an async def wrapper? Decorators replace function or class bindings at definition time; preserve contracts across wrapping, stacking, metadata, typing, and async code. intermediate 1 min · from Decorators py Quiz new For @retry_on(ConnectionError, attempts=3), what must retry_on(...) return? Decorators replace function or class bindings at definition time; preserve contracts across wrapping, stacking, metadata, typing, and async code. intermediate 1 min · from Decorators py Predict the output new What does this program print? Decorators replace function or class bindings at definition time; preserve contracts across wrapping, stacking, metadata, typing, and async code. intermediate 2 min · from Decorators py Review AI code new Review a generated payment decorator Decorators replace function or class bindings at definition time; preserve contracts across wrapping, stacking, metadata, typing, and async code. advanced 8 min · from Decorators py Spot the bug new Spot the bug in the decorator wrapper Decorators replace function or class bindings at definition time; preserve contracts across wrapping, stacking, metadata, typing, and async code. beginner 4 min · from Decorators py Quiz new What does functools.wraps() guarantee for a wrapper? Decorators replace function or class bindings at definition time; preserve contracts across wrapping, stacking, metadata, typing, and async code. beginner 1 min · from Decorators py Quiz new Which signature makes order_id positional-only and timeout keyword-only? Functions bind inputs to parameters, run code in a local scope, and return results; clear call conventions turn that behavior into a stable interface. beginner 2 min · from Functions py Predict the output new What two lines does this program print? Functions bind inputs to parameters, run code in a local scope, and return results; clear call conventions turn that behavior into a stable interface. beginner 2 min · from Functions py Predict the output new What does this program print? Functions bind inputs to parameters, run code in a local scope, and return results; clear call conventions turn that behavior into a stable interface. beginner 2 min · from Functions py Review AI code new Review generated order pricer Functions bind inputs to parameters, run code in a local scope, and return results; clear call conventions turn that behavior into a stable interface. advanced 12 min · from Functions py Spot the bug new Spot the bug in the mutable default Functions bind inputs to parameters, run code in a local scope, and return results; clear call conventions turn that behavior into a stable interface. beginner 3 min · from Functions py Predict the output new What does this program print? Use functools to bind call arguments, preserve wrapper metadata, reduce iterables, and adapt comparison protocols without breaking interface contracts. beginner 2 min · from functools py Predict the output new What are the two output lines? Use functools to bind call arguments, preserve wrapper metadata, reduce iterables, and adapt comparison protocols without breaking interface contracts. intermediate 2 min · from functools py Review AI code new Review a generated audited total Use functools to bind call arguments, preserve wrapper metadata, reduce iterables, and adapt comparison protocols without breaking interface contracts. advanced 8 min · from functools py Spot the bug new Spot the bug in the comparator Use functools to bind call arguments, preserve wrapper metadata, reduce iterables, and adapt comparison protocols without breaking interface contracts. intermediate 4 min · from functools py Quiz new Which guarantee does functools.wraps() provide? Use functools to bind call arguments, preserve wrapper metadata, reduce iterables, and adapt comparison protocols without breaking interface contracts. beginner 1 min · from functools py Quiz new Which statement correctly distinguishes an iterable from an iterator? Understand Python's iteration protocol, lazy execution, yield, generator delegation, and resource cleanup to build predictable single-pass data flows. beginner 1 min · from Generators and iterators py Predict the output new Which lines appear, and in what order? Understand Python's iteration protocol, lazy execution, yield, generator delegation, and resource cleanup to build predictable single-pass data flows. beginner 2 min · from Generators and iterators py Predict the output new What does this program print? Understand Python's iteration protocol, lazy execution, yield, generator delegation, and resource cleanup to build predictable single-pass data flows. beginner 2 min · from Generators and iterators py Review AI code new Review a generated lazy order reader Understand Python's iteration protocol, lazy execution, yield, generator delegation, and resource cleanup to build predictable single-pass data flows. advanced 8 min · from Generators and iterators py Spot the bug new Spot the bug in the file-line generator Understand Python's iteration protocol, lazy execution, yield, generator delegation, and resource cleanup to build predictable single-pass data flows. intermediate 4 min · from Generators and iterators py Quiz new What happens when StopIteration escapes directly from a generator function body in Python… Understand Python's iteration protocol, lazy execution, yield, generator delegation, and resource cleanup to build predictable single-pass data flows. intermediate 1 min · from Generators and iterators py Quiz new After values = [parsed for raw in rows if (parsed := parse(raw)) is not None] succeeds, w… List comprehensions express iterable transformation and filtering in one expression; learn clause order, scope, evaluation, and when a loop is clearer. intermediate 1 min · from List comprehensions py Quiz new Which loop has the same order as [(team, member) for team in teams for member in team.mem… List comprehensions express iterable transformation and filtering in one expression; learn clause order, scope, evaluation, and when a loop is clearer. beginner 1 min · from List comprehensions py Predict the output new What does this program print? List comprehensions express iterable transformation and filtering in one expression; learn clause order, scope, evaluation, and when a loop is clearer. beginner 2 min · from List comprehensions py Review AI code new Review generated order export List comprehensions express iterable transformation and filtering in one expression; learn clause order, scope, evaluation, and when a loop is clearer. advanced 6 min · from List comprehensions py Spot the bug new Spot the bug in reading normalization List comprehensions express iterable transformation and filtering in one expression; learn clause order, scope, evaluation, and when a loop is clearer. intermediate 4 min · from List comprehensions py Quiz new Which statement about lru_cache keys is correct? Cache repeatable calls with functools while handling keys, eviction, invalidation, duplicate concurrent work, and object lifetimes correctly. intermediate 1 min · from lru_cache function caching py Predict the output new How many times does this program print compute? Cache repeatable calls with functools while handling keys, eviction, invalidation, duplicate concurrent work, and object lifetimes correctly. beginner 2 min · from lru_cache function caching py Review AI code new Review a generated invoice cache Cache repeatable calls with functools while handling keys, eviction, invalidation, duplicate concurrent work, and object lifetimes correctly. advanced 8 min · from lru_cache function caching py Spot the bug new Spot the bug in the LRU cache Cache repeatable calls with functools while handling keys, eviction, invalidation, duplicate concurrent work, and object lifetimes correctly. beginner 4 min · from lru_cache function caching py Quiz new What does lru_cache thread safety guarantee for two concurrent calls with the same cold k… Cache repeatable calls with functools while handling keys, eviction, invalidation, duplicate concurrent work, and object lifetimes correctly. intermediate 1 min · from lru_cache function caching py Quiz new On Python 3.14, when should multi-input map use strict=True? Transform, select, and reduce Python iterables while handling lazy evaluation, empty inputs, and mismatched input lengths correctly. intermediate 1 min · from map, filter and reduce py Predict the output new What does this program print? Transform, select, and reduce Python iterables while handling lazy evaluation, empty inputs, and mismatched input lengths correctly. beginner 2 min · from map, filter and reduce py Predict the output new What value does this left-to-right reduction print? Transform, select, and reduce Python iterables while handling lazy evaluation, empty inputs, and mismatched input lengths correctly. intermediate 2 min · from map, filter and reduce py Review AI code new Review a generated payment summary Transform, select, and reduce Python iterables while handling lazy evaluation, empty inputs, and mismatched input lengths correctly. advanced 8 min · from map, filter and reduce py Spot the bug new Spot the bug in sensor filtering Transform, select, and reduce Python iterables while handling lazy evaluation, empty inputs, and mismatched input lengths correctly. beginner 4 min · from map, filter and reduce py Quiz new What does configured_port or 8000 return when configured_port is 0? Understand how Python binds names to objects and builds programs from control flow, functions, exceptions, and modules. beginner 2 min · from Python fundamentals py Quiz new Which try suite best handles conversion of one user-supplied quantity? Understand how Python binds names to objects and builds programs from control flow, functions, exceptions, and modules. beginner 2 min · from Python fundamentals py Predict the output new What does this program print? Understand how Python binds names to objects and builds programs from control flow, functions, exceptions, and modules. beginner 3 min · from Python fundamentals py Review AI code new Review generated order total Understand how Python binds names to objects and builds programs from control flow, functions, exceptions, and modules. intermediate 12 min · from Python fundamentals py Spot the bug new Spot the bug in the mutable default Understand how Python binds names to objects and builds programs from control flow, functions, exceptions, and modules. beginner 4 min · from Python fundamentals py Quiz new Which construct binds a name in the current code block? Scope controls where names are visible, while namespaces hold bindings; learn LEGB, global, nonlocal, and Python's special scope boundaries. intermediate 1 min · from Scope and namespaces py Quiz new In a Python 3.14 function, what happens after locals()["count"] = 3? Scope controls where names are visible, while namespaces hold bindings; learn LEGB, global, nonlocal, and Python's special scope boundaries. advanced 1 min · from Scope and namespaces py Predict the output new What does this program print? Scope controls where names are visible, while namespaces hold bindings; learn LEGB, global, nonlocal, and Python's special scope boundaries. beginner 2 min · from Scope and namespaces py Predict the output new What does this program print? Scope controls where names are visible, while namespaces hold bindings; learn LEGB, global, nonlocal, and Python's special scope boundaries. intermediate 2 min · from Scope and namespaces py Review AI code new Review generated tenant pricing handlers Scope controls where names are visible, while namespaces hold bindings; learn LEGB, global, nonlocal, and Python's special scope boundaries. advanced 8 min · from Scope and namespaces py Spot the bug new Spot the bug in the invoice renderer Scope controls where names are visible, while namespaces hold bindings; learn LEGB, global, nonlocal, and Python's special scope boundaries. intermediate 4 min · from Scope and namespaces py Quiz new Which expression accepts a list on the right without converting it first? Model unique members, membership, and set algebra with hashable values while avoiding unstable order, input mutation, and equality surprises. beginner 1 min · from Sets py Predict the output new What does this program print? Model unique members, membership, and set algebra with hashable values while avoiding unstable order, input mutation, and equality surprises. beginner 2 min · from Sets py Quiz new A cleanup operation may run repeatedly, and an already-absent member is valid. Which oper… Model unique members, membership, and set algebra with hashable values while avoiding unstable order, input mutation, and equality surprises. beginner 1 min · from Sets py Review AI code new Review generated permission plan Model unique members, membership, and set algebra with hashable values while avoiding unstable order, input mutation, and equality surprises. advanced 6 min · from Sets py Spot the bug new Spot the bug in customer ID export Model unique members, membership, and set algebra with hashable values while avoiding unstable order, input mutation, and equality surprises. intermediate 4 min · from Sets py Quiz new What happens when two registered, unrelated ABCs both recognize an argument class and nei… singledispatch selects an implementation by the first argument's runtime type; use registration, inheritance, ABCs, and method dispatch safely. advanced 1 min · from Single-dispatch generic functions py Quiz new How should singledispatchmethod and classmethod be stacked on the generic method? singledispatch selects an implementation by the first argument's runtime type; use registration, inheritance, ABCs, and method dispatch safely. intermediate 1 min · from Single-dispatch generic functions py Predict the output new What does this program print? singledispatch selects an implementation by the first argument's runtime type; use registration, inheritance, ABCs, and method dispatch safely. beginner 2 min · from Single-dispatch generic functions py Review AI code new Review a generated plugin encoder singledispatch selects an implementation by the first argument's runtime type; use registration, inheritance, ABCs, and method dispatch safely. advanced 8 min · from Single-dispatch generic functions py Spot the bug new Spot the bug in singledispatch routing singledispatch selects an implementation by the first argument's runtime type; use registration, inheritance, ABCs, and method dispatch safely. beginner 4 min · from Single-dispatch generic functions py Quiz new What does @convert.register on a value: int | float implementation register in Python 3.1… singledispatch selects an implementation by the first argument's runtime type; use registration, inheritance, ABCs, and method dispatch safely. intermediate 1 min · from Single-dispatch generic functions py Quiz new Which expression wraps the string "Paris" in a one-item tuple without iterating over its … Use tuples for fixed-shape data, with precise rules for packing, unpacking, shallow immutability, hashing, and named records. beginner 1 min · from Tuples py Predict the output new What does this program print? Use tuples for fixed-shape data, with precise rules for packing, unpacking, shallow immutability, hashing, and named records. beginner 2 min · from Tuples py Review AI code new Review generated reading index Use tuples for fixed-shape data, with precise rules for packing, unpacking, shallow immutability, hashing, and named records. advanced 6 min · from Tuples py Spot the bug new Spot the bug in the tuple key Use tuples for fixed-shape data, with precise rules for packing, unpacking, shallow immutability, hashing, and named records. beginner 4 min · from Tuples py Quiz new After first, *middle, last = (10, 20, 30, 40), what are middle and its type? Use tuples for fixed-shape data, with precise rules for packing, unpacking, shallow immutability, hashing, and named records. beginner 1 min · from Tuples py Quiz new Which statement about dynamic typing and annotations is accurate? Learn how Python names bind to objects, then handle built-in types, truth values, conversion, aliases, mutability, and annotations correctly. beginner 2 min · from Variables and data types py Quiz new What is the safest description of int() at an input boundary? Learn how Python names bind to objects, then handle built-in types, truth values, conversion, aliases, mutability, and annotations correctly. beginner 2 min · from Variables and data types py Predict the output new What does this program print? Learn how Python names bind to objects, then handle built-in types, truth values, conversion, aliases, mutability, and annotations correctly. beginner 3 min · from Variables and data types py Predict the output new What does this program print? Learn how Python names bind to objects, then handle built-in types, truth values, conversion, aliases, mutability, and annotations correctly. beginner 3 min · from Variables and data types py Review AI code new Review generated order total Learn how Python names bind to objects, then handle built-in types, truth values, conversion, aliases, mutability, and annotations correctly. intermediate 12 min · from Variables and data types py Spot the bug new Spot the bug in the cents parser Learn how Python names bind to objects, then handle built-in types, truth values, conversion, aliases, mutability, and annotations correctly. intermediate 5 min · from Variables and data types