Python Quiz
Thirty questions covering the whole course. Choose an answer for each, then press Check answers at the bottom to see your score and an explanation for every question.
1. How does Python mark which lines belong to an if block?
Indentation is part of Python's syntax (lesson 03).
2. What is the naming convention for variables and functions in Python?
PEP 8 uses snake_case for variables and functions (lessons 04 and 54).
3. What does 7 / 2 return?
/ always returns a float; use // for floor division (lessons 06 and 12).
4. What does "5" + 3 do?
Python doesn't implicitly convert between strings and numbers (lesson 05).
5. What does int("3.5") do?
Convert via float first: int(float("3.5")) (lesson 07).
6. What is "Python"[-1]?
Negative indexes count from the end (lesson 08).
7. What does f"{3.14159:.2f}" produce?
.2f means two decimal places (lesson 10).
8. Which value is truthy?
Any non-empty string is truthy (lesson 11).
9. What's the correct way to check for None?
None is a singleton; compare with is (lesson 11).
10. What does 1 < x < 10 mean in Python?
Python supports chained comparisons (lesson 12).
11. What does input() always return?
Convert with int() or float() when you need a number (lesson 13).
12. In a match statement, what does a bare name like case x: do?
Bare names capture; use literals or dotted names to compare (lesson 15).
13. What does list(range(2, 8, 2)) produce?
The stop value is never included (lesson 17).
14. When does a loop's else block run?
Think of it as "no break" (lesson 18).
15. What does nums.sort() return?
Use sorted(nums) for a new list (lesson 20).
16. What does [n * 2 for n in range(4) if n % 2 == 0] produce?
Keeps 0 and 2, then doubles them (lesson 21).
17. How do you write a tuple with one item?
The comma makes the tuple (lesson 22).
18. Which can be used as a dictionary key?
Keys must be hashable (immutable) (lessons 22–24).
19. What does {} create?
Use set() for an empty set (lesson 23).
20. What happens with def add(item, basket=[])?
Defaults are evaluated once; use None instead (lesson 29).
21. Which keyword lets an inner function modify a variable of its enclosing function?
nonlocal refers to the enclosing function's scope (lesson 30).
22. What does a function containing yield return when called?
The body runs lazily as values are requested (lesson 35).
23. What does @functools.wraps(fn) do in a decorator?
It keeps the decorated function's identity (lesson 34).
24. What is self in a method?
obj.method() passes obj as self (lesson 36).
25. Which special method controls how an object looks in lists and the shell?
__repr__ is the developer-facing representation (lessons 36 and 38).
26. What does @dataclass generate automatically?
Dataclasses remove boilerplate (lesson 40).
27. When does a finally block run?
Use it for cleanup (lesson 41).
28. What does open("data.txt", "w") do if the file exists?
"w" truncates; use "a" to append or "x" to refuse overwriting (lesson 43).
29. What is the purpose of a virtual environment?
One environment per project avoids version clashes (lesson 47).
30. Which module should you use to generate a secure password token?
random is predictable by design (lesson 48).
Missed a few? Each explanation names the lesson to revisit. When you're ready, try the exercises.