Python Quiz
A short Python quiz covering syntax, data types, and idioms.
Sample questions
EXAMPLE
# Python quiz
1) Which type is immutable?
a) list b) dict c) tuple d) set
2) Which is a list comprehension?
a) [x*2 for x in range(5)]
b) (x*2 for x in range(5))
c) {x*2 for x in range(5)}
d) all are valid expressions
3) What does enumerate() return?
a) (index, value) tuples
b) values only
c) indices only
d) keys only
4) f-strings let you:
a) format strings only at module load
b) embed expressions inside string literals - f'hello {name}'
c) replace print statements
d) work only with integers
5) The walrus operator := is for:
a) lambda functions
b) assignment inside an expression
c) type hints
d) destructuring
6) When you write 'with open(...) as f:' the with statement ensures:
a) the file opens
b) the file closes even if an exception is raised
c) the file is binary
d) you can read it
7) The Pythonic way to handle the case where a key may not exist:
a) try/except KeyError or dict.get(key, default)
b) hasattr
c) is None
d) defer to a global
8) Type hints in Python 3.10+:
a) are enforced at runtime
b) are documentation + static analysis only - not enforced unless you use a tool like mypy
c) only work for primitives
d) require dataclasses
Answers: 1c, 2a, 3a, 4b, 5b, 6b, 7a, 8b.
Why it matters
Python rewards reading code closely. Comprehensions, with-statements, dict.get, and f-strings are the daily idioms; type hints + mypy/pyright are the modern productivity bump. If you nail these eight you avoid the most common interview mistakes.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
# Quizzes appear at the end of each lesson — 3 questions each.
print('Test yourself when you finish a lesson!')
Try it Yourself »
Exercise
Per-lesson quiz length.
questions
A single digit.
Discussion
Loading…