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?

2. What is the naming convention for variables and functions in Python?

3. What does 7 / 2 return?

4. What does "5" + 3 do?

5. What does int("3.5") do?

6. What is "Python"[-1]?

7. What does f"{3.14159:.2f}" produce?

8. Which value is truthy?

9. What's the correct way to check for None?

10. What does 1 < x < 10 mean in Python?

11. What does input() always return?

12. In a match statement, what does a bare name like case x: do?

13. What does list(range(2, 8, 2)) produce?

14. When does a loop's else block run?

15. What does nums.sort() return?

16. What does [n * 2 for n in range(4) if n % 2 == 0] produce?

17. How do you write a tuple with one item?

18. Which can be used as a dictionary key?

19. What does {} create?

20. What happens with def add(item, basket=[])?

21. Which keyword lets an inner function modify a variable of its enclosing function?

22. What does a function containing yield return when called?

23. What does @functools.wraps(fn) do in a decorator?

24. What is self in a method?

25. Which special method controls how an object looks in lists and the shell?

26. What does @dataclass generate automatically?

27. When does a finally block run?

28. What does open("data.txt", "w") do if the file exists?

29. What is the purpose of a virtual environment?

30. Which module should you use to generate a secure password token?

Missed a few? Each explanation names the lesson to revisit. When you're ready, try the exercises.