Answer key · 12 questions

Python Variables and Types Exercises With Answers

All 12 questions for Python Variables and Types in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Variables and Types, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

Which value is a float?

  1. 3.0correct
  2. True
  3. "3.0"
  4. 3

Answer: 3.0

Why: A float is a number with a decimal point, even when the decimal part is zero.

02 · Multiple choice

What does type(True).__name__ return?

  1. boolean
  2. str
  3. boolcorrect
  4. int

Answer: bool

Why: True and False are values of the built-in bool type.

03 · Multiple choice

Which variable name is valid in Python?

  1. order_totalcorrect
  2. class
  3. order-total
  4. 2total

Answer: order_total

Why: A variable name may contain letters, digits, and underscores, but cannot start with a digit or be a keyword.

04 · Multiple choice

What appears on the screen?

price = 4
quantity = 3
print(price * quantity)
  1. 12.0
  2. 7
  3. 43
  4. 12correct

Answer: 12

Why: price * quantity multiplies 4 by 3.

05 · Fill in the blank

Complete the expression so total is price multiplied by quantity.

total = price ____ quantity

Answer: *

Why: The asterisk is Python's multiplication operator.

06 · Fill in the blank

Complete the assignment to store the boolean value true.

is_member = ____

Answer: True

Why: Python's boolean true value starts with a capital T.

07 · Fill in the blank

Complete the attribute so the expression evaluates to int.

print(type(42).____)

Answer: __name__

Why: type(value).__name__ returns the type name as a string.

08 · Fill in the blank

Complete the format spec so the price shows two decimal places.

price = 4.5
print(f"{price:____}")

Answer: .2f

Why: The f-string format spec .2f displays two digits after the decimal point.

09 · Predict output

What is printed?

x = 5
y = 3
print(x + y)

Answer: 8

Why: 5 + 3 is 8.

10 · Predict output

What is the output?

print(type(3.0).__name__)

Answer: float

Why: 3.0 is a float.

11 · Predict output

What does this code print?

print(int("12") + 3)

Answer: 15

Why: int("12") converts the text to 12, then adds 3.

12 · Predict output

What does the program output?

print(10 / 4)

Answer: 2.5

Why: True division always returns a float in Python 3.

Try the questions first

Open the practice page for Python Variables and Types and answer the same 12 questions with instant feedback.

Practice this lesson