Learn Without Walls
← Back to Module 2

Practice Problems: Variables & Data Types

15 problems to reinforce your learning • Try each one before checking the solution!

Problem 1 Easy

Create variables for a person's first name, last name, and age. Print all three values on separate lines.

Use three separate assignment statements and three print() calls.

first_name = "Alice"
last_name = "Johnson"
age = 22

print(first_name)
print(last_name)
print(age)
Alice Johnson 22

Problem 2 Easy

Calculate the area of a rectangle with a width of 8 and a height of 5. Store the result in a variable called area and print it.

Area of a rectangle = width * height

width = 8
height = 5
area = width * height
print(area)
40

Problem 3 Easy

Create a variable called price with the value 49.99 and a variable called quantity with the value 3. Calculate and print the total cost.

Total cost = price * quantity

price = 49.99
quantity = 3
total = price * quantity
print(total)
149.97

Problem 4 Easy

What is the output of each expression? Write your answer first, then check.

print(15 // 4)
print(15 % 4)
print(2 ** 4)
print(10 / 3)
3 3 16 3.3333333333333335

15 // 4 = 3 (integer division), 15 % 4 = 3 (remainder), 2 ** 4 = 16 (exponent), 10 / 3 = 3.333... (float division).

Problem 5 Easy

Create a variable with the value "Python" and print its length using len().

language = "Python"
print(len(language))
6

Problem 6 Medium

Given 500 seconds, convert it to minutes and remaining seconds. Print the result like: "8 minutes and 20 seconds".

Use // to get whole minutes and % to get remaining seconds. Use str() to convert numbers for concatenation.

total_seconds = 500
minutes = total_seconds // 60
seconds = total_seconds % 60
print(str(minutes) + " minutes and " + str(seconds) + " seconds")
8 minutes and 20 seconds

Problem 7 Medium

Convert 72 degrees Fahrenheit to Celsius using the formula: C = (F - 32) * 5/9. Store the result and print it rounded to 2 decimal places.

Use parentheses to ensure the subtraction happens before multiplication. Use round(value, 2) for rounding.

fahrenheit = 72
celsius = (fahrenheit - 32) * 5 / 9
print(round(celsius, 2))
22.22

Problem 8 Medium

Create a string that says: She said "It's amazing!" and print it. (The output must include both single and double quotes.)

You can use escape characters, or use triple quotes.

# Option 1: escape the double quotes
message = "She said \"It's amazing!\""
print(message)

# Option 2: use triple quotes
message = """She said "It's amazing!\""""
print(message)
She said "It's amazing!"

Problem 9 Medium

Predict what type() will return for each value, then verify:

type(42)
type(42.0)
type("42")
type(True)
type(10 / 5)
print(type(42))      # <class 'int'>
print(type(42.0))    # <class 'float'>
print(type("42"))    # <class 'str'>
print(type(True))    # <class 'bool'>
print(type(10 / 5))  # <class 'float'> -- division always returns float!

Problem 10 Medium

A student scored 85, 92, 78, and 95 on four exams. Calculate the average and print it rounded to 1 decimal place.

Average = sum of scores / number of scores. Use round(value, 1).

score1 = 85
score2 = 92
score3 = 78
score4 = 95

average = (score1 + score2 + score3 + score4) / 4
print(round(average, 1))
87.5

Problem 11 Medium

Swap the values of two variables a = 10 and b = 20 so that a becomes 20 and b becomes 10. Print both before and after.

Python allows simultaneous assignment: a, b = b, a. Or you can use a temporary variable.

a = 10
b = 20
print("Before: a =", a, "b =", b)

# Python's elegant way to swap
a, b = b, a
print("After: a =", a, "b =", b)
Before: a = 10 b = 20 After: a = 20 b = 10

Problem 12 Challenge

Convert the string "3.75" to an integer. What steps do you need?

You cannot convert "3.75" directly to int. You need an intermediate step. Convert to float first.

text = "3.75"

# This would cause an error: int("3.75")
# Step 1: convert string to float
# Step 2: convert float to int

result = int(float(text))
print(result)
3

Problem 13 Challenge

Given a 4-digit number stored in a variable (e.g., 1234), extract each digit using integer division and modulo. Print each digit on its own line.

To get the last digit, use % 10. To remove the last digit, use // 10. The thousands digit is number // 1000.

number = 1234

thousands = number // 1000
hundreds = (number // 100) % 10
tens = (number // 10) % 10
ones = number % 10

print(thousands)
print(hundreds)
print(tens)
print(ones)
1 2 3 4

Problem 14 Challenge

Create variables for a product name, price, and quantity. Build a receipt string using concatenation that looks like:
Item: Widget | Qty: 5 | Total: $49.95

Remember to convert numbers to strings using str() before concatenating with +.

product = "Widget"
price = 9.99
quantity = 5
total = price * quantity

receipt = "Item: " + product + " | Qty: " + str(quantity) + " | Total: $" + str(total)
print(receipt)
Item: Widget | Qty: 5 | Total: $49.95

Problem 15 Challenge

Predict the output of each bool() call, then verify. Which are True and which are False?

print(bool(0))
print(bool(""))
print(bool("0"))
print(bool(" "))
print(bool(-1))
print(bool(0.0001))
print(bool(None))
False False True True True True False

bool(0) = False (zero), bool("") = False (empty string), bool("0") = True (non-empty string -- the character "0" is still content!), bool(" ") = True (a space is a character), bool(-1) = True (non-zero), bool(0.0001) = True (non-zero), bool(None) = False.