Python Intermediate Quiz
Comprehensions, dicts, exceptions and the reference semantics that surprise people — worked through real code rather than definitions.
Start the quizSample questions from this quiz
A preview of the style and depth. Try each one, then reveal the answer — or skip straight to the timed quiz.
1. What does this print?
nums = [1, 2, 3, 4]
print([n * 2 for n in nums if n % 2 == 0])- [4, 8, 0, 0]
- [2, 4, 6, 8]
- [2, 6]
- [4, 8]
Show answer
Answer: [4, 8]. The `if` filters first, keeping 2 and 4, then the expression doubles them. Reading order is: for, then if, then the expression at the front. A conditional *expression* would go before the `for` instead: `[n*2 if n%2==0 else n for n in nums]`.
2. What does this print?
a = [1, 2]
b = a
b.append(3)
print(a, len(a))- [3] 1
- [1, 2] 2
- [1, 2, 3] 2
- [1, 2, 3] 3
Show answer
Answer: [1, 2, 3] 3. `b = a` binds a second name to the same list object — it does not copy. Mutating through either name is visible from both. Use `a[:]`, `list(a)` or `copy.copy(a)` for an independent shallow copy.
3. This should collect the squares of even numbers but raises a TypeError. Which line is wrong?
1 result = []
2 for n in range(10):
3 if n % 2 == 0:
4 result = result.append(n ** 2)
5 print(result)- Line 3 — the modulo check is inverted
- Line 2 — range should be list(range(10))
- Line 4 — append returns None, so result becomes None after the first pass
- Line 1 — result should be a tuple
Show answer
Answer: Line 4 — append returns None, so result becomes None after the first pass. Methods that mutate in place — `append`, `sort`, `extend`, `update` — return None by convention, precisely so you cannot chain them and mistake them for copies. Just call `result.append(...)` without assigning.
Frequently asked questions
How many questions are in this Python quiz?+
30 questions, with a 40-minute time limit. Every question includes a written explanation of the correct answer.
Is this Python quiz free?+
Yes. Every quiz on CodexQuizz is free and needs no account. You only enter a name if you choose to post your score to the leaderboard.
What level is the intermediate quiz aimed at?+
Comprehensions, dicts, exceptions and the reference semantics that surprise people — worked through real code rather than definitions.
Can I use this to prepare for a Python interview?+
Yes. The questions cover the topics that come up in Python technical screens, and the explanations are written so that a wrong answer still teaches you the underlying concept.
Ready to test your Python?
30 questions, 40 minutes. Free, no sign-up.
Start now