Python Programming Quiz Test Your Code Skills
True / False
True / False
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, intersectiona & b, differencea - b, symmetric diffa ^ 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
Nonesentinel. - *args and **kwargs: accept variable positional and keyword arguments. Use for forwarding:
g(*args, **kwargs). - Nonlocal vs global:
nonlocaltargets nearest enclosing scope,globaltargets module scope.
Exceptions and context managers
- Raise with context:
raise NewError() from errpreserves the cause chain. - Finally runs:
finallyexecutes even afterreturnor 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:
@dataclassreduces 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.
Looking for more? Browse QuizWiz Technology & IT collection or explore the full compliance and training quizzes on QuizWiz.