Python Quiz
Test what you've learned across the whole Python series, from data types to classes and generators. Try each question before opening the answer, and use the links to revise any topic you miss.
Basics
1. What does this print?
print(type(3 / 2))
- A)
<class 'int'> - B)
<class 'float'> - C)
1 - D)
1.5
Show answer
B. The / operator always returns a float in Python 3, even for whole numbers (see Numbers & Arithmetic).
2. What does this print?
print(7 // 2, 7 % 2)
Show answer
3 1. // is floor division (whole-number part of the result) and % gives the remainder.
3. What is the value of "Kuza Ndoto"[1:4]?
- A)
"Kuz" - B)
"uza" - C)
"uza " - D)
"Kuza"
Show answer
B. Slicing starts at index 1 (u) and stops before index 4, giving "uza" (see Strings).
Control flow and data structures
4. What does this print?
for i in range(1, 10, 3):
print(i, end=" ")
Show answer
1 4 7. range(1, 10, 3) starts at 1 and adds 3 each time, stopping before 10.
5. Which of these types is immutable?
- A) list
- B) dict
- C) set
- D) tuple
Show answer
D. Tuples can't be changed after creation; lists, dictionaries and sets can (see Tuples).
6. What does this print?
x = [1, 2, 3] y = x y.append(4) print(x)
Show answer
[1, 2, 3, 4]. y = x doesn't copy the list; both names refer to the same list object.
7. What does len({1, 2, 2, 3, 3, 3}) return?
Show answer
3. A set keeps only unique values, so it contains {1, 2, 3} (see Sets).
8. What does this comprehension produce?
print([n * 2 for n in range(5) if n % 2 == 0])
Show answer
[0, 4, 8]. The even numbers 0, 2 and 4 pass the filter, and each is doubled (see Comprehensions).
Functions and errors
9. What does this print?
def area(width, height=2):
return width * height
print(area(3), area(3, 3))
Show answer
6 9. The first call uses the default height=2; the second overrides it with 3 (see Parameters & Arguments).
10. Which exception does int("12a") raise?
- A)
TypeError - B)
ValueError - C)
SyntaxError - D)
NameError
Show answer
B. The argument is the right type (a string) but its value isn't a valid integer (see Exceptions).
11. In a try statement, which block runs whether or not an exception happened?
Show answer
finally. else runs only when there was no exception; finally always runs, which makes it ideal for clean-up (see Try, Except & Finally).
Modules, classes and beyond
12. Which command creates a virtual environment called .venv?
- A)
pip install venv - B)
python -m venv .venv - C)
python venv create .venv - D)
pip freeze .venv
Show answer
B. venv is a standard library module, run with python -m (see pip & Virtual Environments).
13. What does this print?
class Animal:
def sound(self):
return "..."
class Cat(Animal):
def sound(self):
return "Meow"
class Lion(Cat):
pass
print(Lion().sound())
Show answer
Meow. Lion doesn't define sound(), so Python searches its parent Cat first and uses that version (see Inheritance).
14. What does this print?
def numbers():
yield 1
yield 2
g = numbers()
print(next(g) + next(g))
Show answer
3. Each next() resumes the generator until the next yield, giving 1 and then 2 (see Iterators & Generators).
15. What does this print?
import re print(re.findall(r"\d+", "a1b22c333"))
Show answer
['1', '22', '333']. \d+ matches runs of one or more digits, and findall returns them as strings in a list (see Regular Expressions).
How did you do?
- 13 to 15: excellent. Try the interview questions or start the Django series.
- 9 to 12: solid. Revisit the pages linked in the answers you missed.
- 8 or fewer: no problem. Work through the projects, such as the Quiz Game, then try again.
Key points
/gives a float;//and%give the quotient and remainder.- Assignment shares objects rather than copying them; tuples are immutable and sets hold unique values.
- Catch specific exceptions, and use
finallyfor clean-up that must always run. - Child classes inherit methods from their parents, and generators produce values one at a time.
Tip: use the ← and → keys to move between pages.
