Python Programming Quiz Test Your Code Skills - claymation artwork

Python Programming Quiz Test Your Code Skills

8 – 16 Questions 6 min
This Python quiz focuses on intermediate language mechanics that affect correctness and maintainability, including data model behavior, iteration patterns, exceptions, scopes, and common standard-library usage. Expect scenarios that mirror production code reviews such as fixing subtle bugs, choosing the right data structure, and reading tricky expressions. Backend developers, data engineers, automation engineers, and QA engineers benefit most.
1What does this print? ```python print(2 ** 3) ```
2In Python, indentation is part of the syntax and determines code blocks.

True / False

3What exception is raised by this code? ```python user = {"name": "Ada"} print(user["age"]) ```
4What is `list(range(3))`?
5Which list method adds a single item to the end of a list?
6A config loader returns a tuple that might include extra fields: ```python value = ("api.example.com", 443, "https") ``` You only need `host` and `port`, and you want the assignment to still work even if more fields are added later. Which assignment is most robust?
7You are validating user input: ```python text = user_provided n = int(text) ``` You want to handle non-numeric input, but you do not want to hide unrelated bugs. What should you catch?
8A `finally` block is skipped if you `return` from inside the `try` block.

True / False

9You iterate over a dictionary to produce a report and you rely on the key order staying consistent. In Python 3.7 and later, which statement is accurate?
10You write this error handling code: ```python try: risky() except Exception: handle_any() except ValueError: handle_value() ``` What happens when Python reads this?
11A teammate asks you to speed up a pure function that is called repeatedly with the same arguments. Which standard library decorator is designed for memoization?
12You build a list of callbacks in a loop: ```python funcs = [] for i in range(3): funcs.append(lambda: i) print([f() for f in funcs]) ``` It prints `[2, 2, 2]`, but you wanted `[0, 1, 2]`. Which fix works?

Frequent Python programming errors that break otherwise “working” code

1) Confusing identity with equality

Mistake: using is to compare numbers, strings, or lists. It may “work” for small integers due to interning, then fail later.
Fix: use == for value comparison. Reserve is for None and identity checks.

2) Mutable default arguments

Mistake: def f(x, cache={}): ... keeps one dict across calls.
Fix: default to None and create inside: cache = {} if cache is None else cache.

3) Late binding in closures inside loops

Mistake: building callbacks in a loop that all reference the final loop variable.
Fix: bind as a default: lambda i=i: i, or use functools.partial.

4) Swallowing exceptions

Mistake: except Exception: pass hides real failures and creates corrupted state.
Fix: catch specific exceptions, log context, and re-raise when appropriate.

5) Modifying a list while iterating

Mistake: removing items from a list during a for loop skips elements.
Fix: iterate over a copy, or build a filtered list with a comprehension.

6) Assuming copies are independent

Mistake: new = old is aliasing, and list(old) is a shallow copy that still shares nested objects.
Fix: use copy.deepcopy when nested structures must be isolated.

7) Shadowing built-ins

Mistake: naming variables list, dict, id, or sum, then later calling the built-in fails.
Fix: use names like items, mapping, total.

Python intermediate syntax and behavior quick reference (printable)

Tip: You can print this section or save the page as a PDF for a desk reference.

Data structures and operations

  • Dict update/merge: d |= other (3.9+), or {**d, **other} (new dict).
  • Set algebra: union a | b, intersection a & b, difference a - b, symmetric diff a ^ b.
  • Sorting: sorted(items, key=..., reverse=True). Use a key function, avoid custom comparators.
  • Slicing: s[start:stop:step]. Negative step reverses direction, s[::-1] is a common idiom.

Iteration patterns

  • Enumerate: for i, x in enumerate(xs, start=0):
  • Zip: for a, b in zip(as_, bs_, strict=False): (strict available in newer Python 3).
  • Comprehension vs generator: [f(x) for x in xs] builds a list now, (f(x) for x in xs) is lazy.
  • Unpacking: a, *mid, b = seq. Keyword unpacking: f(**kwargs).

Functions and scope

  • Default args evaluated once: avoid mutable defaults, prefer None sentinel.
  • *args and **kwargs: accept variable positional and keyword arguments. Use for forwarding: g(*args, **kwargs).
  • Nonlocal vs global: nonlocal targets nearest enclosing scope, global targets module scope.

Exceptions and context managers

  • Raise with context: raise NewError() from err preserves the cause chain.
  • Finally runs: finally executes even after return or an exception.
  • Context manager pattern: with open(path) as f: ensures cleanup on success and failure.

String formatting and typing

  • f-strings: f"{value=}" is great for debugging.
  • Type hints: def parse(line: str) -> dict[str, int]: helps tools catch mismatches early.
  • Dataclasses: @dataclass reduces boilerplate for simple record types.

Worked example: parse a log file, handle errors, and summarize with Python

Scenario: You get a text log where each line is "<status> <endpoint>", like "200 /health" or "500 /api/orders". You need a summary report and you must not crash on bad lines.

Step 1: Model a parsed record

Use a small dataclass so downstream code is readable and typed.

from dataclasses import dataclass

@dataclass(frozen=True)
class Hit:
    status: int
    endpoint: str

Step 2: Parse with validation and precise exceptions

Split once, convert status to int, and raise ValueError for malformed input. Catch only what you expect when consuming the parser.

def parse_line(line: str) -> Hit:
    parts = line.strip().split(maxsplit=1)
    if len(parts) != 2:
        raise ValueError("missing fields")
    status_s, endpoint = parts
    status = int(status_s)
    return Hit(status=status, endpoint=endpoint)

Step 3: Stream the file with a context manager

Iterate line by line so memory use stays flat on large files. Maintain counters in dicts keyed by status and endpoint.

from collections import Counter

status_counts = Counter()
endpoint_5xx = Counter()

with open("app.log", "rt", encoding="utf-8") as f:
    for raw in f:
        try:
            hit = parse_line(raw)
        except (ValueError, UnicodeDecodeError):
            continue
        status_counts[hit.status] += 1
        if 500 <= hit.status <= 599:
            endpoint_5xx[hit.endpoint] += 1

Step 4: Produce a deterministic report

Sort by count, then by name to stabilize output. This checks your understanding of key functions and tuple sorting.

top_5xx = sorted(endpoint_5xx.items(), key=lambda kv: (-kv[1], kv[0]))

Python programming quiz FAQ for intermediate learners

What Python version behavior should I assume while answering?

Assume modern Python 3 behavior, including f-strings, guaranteed dict insertion order, and the common iteration and exception semantics used in current production code. If two answers look close, favor the one that matches the language reference rules over an implementation quirk.

Why does is sometimes “work” for strings or small integers, and why is it still wrong?

is checks object identity, not value. CPython may reuse objects for small integers and some strings, so two equal values can point to the same object sometimes. That behavior is not a reliable contract for your code. Use == for value comparisons, and use is None for sentinel checks.

Generators vs list comprehensions: what should I look for in quiz questions?

List comprehensions allocate a full list immediately, which affects memory and evaluation timing. Generator expressions are lazy and produce items on demand, which changes when exceptions occur and when side effects run. Watch for questions that mention large inputs, streaming files, or “first match” logic, where laziness matters.

What is the fastest way to improve if I miss questions on copies, mutation, and defaults?

Practice by writing tiny examples that print object ids and mutate nested structures. Compare new = old, old.copy(), slicing like old[:], and copy.deepcopy. Then rewrite one function that uses a mutable default argument to use a None sentinel. If you also work across languages, Check JavaScript Skills From Basics to Advanced helps reinforce reference vs value mental models.

Do Python skills here transfer to SQL-heavy or data engineering work?

Yes. Many data tasks mix Python control flow, iteration, and error handling with SQL results processing. If your misses cluster around grouping, sorting, and aggregation patterns, pairing this with Test SQL Query Writing and Database Skills helps you choose where to aggregate and how to validate data at boundaries.