📦 Chapter 3: Variables & Data Types

Test your understanding of Python's building blocks!

🎯 Multiple Choice Questions

1. Which of the following is the best analogy for a variable in Python?

  • A math equation
  • A labeled box that stores information
  • A computer program
  • A type of calculator
Correct! Variables are like labeled boxes - you put information inside and use the label (variable name) to access it later.

2. What data type would be best for storing a product price like $15.50?

  • int (integer)
  • float
  • str (string)
  • bool (boolean)
Correct! Float is perfect for prices because it can handle decimal points. Integers can't store fractional values.

3. Which variable name follows Python's naming rules?

  • 2nd_product
  • product name
  • product_name
  • product-name
Correct! Variable names can't start with numbers, can't have spaces, and can't use hyphens. Underscores are perfect for separating words.

4. What will this code output?

x = 10 y = 3 result = x * y print(result)
  • 13
  • 30
  • 103
  • Error
Correct! The * operator multiplies numbers: 10 × 3 = 30.

5. What does the # symbol do in Python code?

  • Performs division
  • Creates a comment that Python ignores
  • Starts a new variable
  • Prints text to the screen
Correct! The # symbol creates comments - notes for humans that Python completely ignores when running the code.
🔍 Code Analysis

6. What will happen when this code runs?

message = "Your total is: $" total = 25.00 final_message = message + total print(final_message)
  • Prints: Your total is: $25.0
  • Prints: Your total is: $25.00
  • Causes a TypeError
  • Prints: 25.00
Correct! This causes a TypeError because you can't concatenate (join) a string with a number directly. You need to convert the number to a string first with str().

7. How would you fix the code from question 6?

  • final_message = message - total
  • final_message = message + str(total)
  • final_message = str(message) + total
  • final_message = message * total
Correct! str(total) converts the number to a string, allowing it to be concatenated with the text message.

8. What data type will the variable 'result' have after this code?

first_name = "Alice" last_name = "Johnson" result = first_name + " " + last_name
  • int
  • float
  • str
  • bool
Correct! When you concatenate strings with +, the result is always a string. result will contain "Alice Johnson".
✏️ Fill in the Blanks

9. Complete Alex's sales calculation:

# Price per hour for design work hourly_rate = # Hours worked on project hours_worked = # Calculate total total = hourly_rate hours_worked

Fill in: A reasonable hourly rate, hours worked, and the math operator to multiply them.

Sample answers: hourly_rate could be 75.00, hours_worked could be 4.5, and the operator should be * for multiplication.
✅ True or False

10. Variable names in Python can start with a number.

  • True
  • False
Correct! Variable names cannot start with a number in Python. They must start with a letter or underscore.

11. Python automatically determines the data type of a variable based on its value.

  • True
  • False
Correct! Python uses dynamic typing - it automatically figures out whether a value is an int, float, string, etc.

12. The following code will work without errors:

is_available = True print("Product available: " + is_available)
  • True
  • False
Correct! This will cause a TypeError. You need to convert the boolean to a string: str(is_available).

🎉 Quiz Complete!

Your Score: 0/12

Keep practicing!