Variables and data types in Python — exercises with solutions and cheat sheet
You’ve seen the theory and practised with real code. Now it’s time for the moment of truth: solving exercises on your own. In this article you’ll find intermediate-level exercises on variables and data types in Python, a final challenge exercise that mixes everything from the topic, and a complete cheat sheet to keep handy. The method is simple: try to solve it, use the hint if you get stuck, and compare with the commented solution at the end.
You can use pythontutor.com to visualise the execution step by step if something isn’t clicking.
Before starting, review the theory or the practice article if you need to refresh anything.
Table of Contents
How to use this article
Don’t jump straight to the solutions. The process is: read the exercise → try to solve it in VS Code → if you’ve been stuck for more than 10 minutes, read the hint → compare your solution with the one at the end. You learn twice as much this way as just reading the answer directly.
Variables and data types in Python exercises — Exercise 1: The shopping receipt
Write a program that asks the user for three products with their prices and calculates the total purchase, the VAT (21%) and the final price including VAT.
The output should look like this:
Product 1: Milk Price: 1.20 Product 2: Bread Price: 0.90 Product 3: Coffee Price: 3.50 --- Receipt --- Milk: 1.2 € Bread: 0.9 € Coffee: 3.5 € Subtotal: 5.6 € VAT (21%): 1.18 € Total: 6.78 €
💡 Hint: Use str variables for the product names and float for the prices. VAT is calculated by multiplying the subtotal by 0.21. Use round() to control the decimal places and f-strings for the receipt layout.
Variables and data types in Python exercises — Exercise 2: Temperature converter
Write a program that asks for a temperature in degrees Celsius and converts it to Fahrenheit and Kelvin. The formulas are:
Fahrenheit = (Celsius × 9/5) + 32 Kelvin = Celsius + 273.15
The output should look like this:
Temperature in Celsius: 100 100 °C is equivalent to: → 212.0 °F → 373.15 K
💡 Hint: One single float variable for the Celsius temperature is enough. Calculate each conversion in a separate variable to keep the code readable. Use \n for the blank line and → as a special character inside the print.
Variables and data types in Python exercises — Exercise 3: Bill splitter
Write a program to split a restaurant bill between several people including tip. Ask for the total amount, the tip percentage and the number of people, and calculate how much each person pays.
Bill amount: 85.50 Tip percentage (%): 10 Number of people: 4 --- Bill split --- Subtotal: 85.5 € Tip (10%): 8.55 € Total with tip: 94.05 € Each person pays: 23.51 €
💡 Hint: Use float for the amount, int for the number of people and float for the percentage. Divide the total including tip by the number of people at the end. Only round to 2 decimal places at the final result.
Variables and data types in Python exercises — Challenge exercise: Complete student profile
This exercise mixes everything from the topic. Write a program that asks for a student’s details and generates a complete profile with statistics.
Data to ask for: full name, age, degree, university, and four subject grades with their names.
The output should include: all student data, the average grade rounded to 2 decimal places, the highest and lowest grade using max() and min(), whether they passed (average ≥ 5), and estimated credits (multiply passed subjects × 6).
=============================
STUDENT PROFILE
=============================
Name: Sergio Medina
Age: 20 years
Degree: GCID
University: ULPGC
-----------------------------
Subject 1 — FP1: 7.5
Subject 2 — Statistics: 8.0
Subject 3 — Maths: 6.5
Subject 4 — Databases: 4.0
-----------------------------
Average grade: 6.5
Highest grade: 8.0
Lowest grade: 4.0
Passed?: True
Estimated credits: 18
=============================
💡 Hints:
- Use
strfor name, degree and university,intfor age,floatfor all grades max(grade1, grade2, grade3, grade4)andmin()work with separate variables- To count passed subjects you need to compare each grade with 5.0 using
bool— in Python you can add booleans becauseTrueequals 1 andFalseequals 0 - The repeated
=and-characters in the profile are made with"=" * 29— multiplying a string by a number
Commented solutions
Solution Exercise 1:
# Shopping receipt
p1_name = input("Product 1: ")
p1_price = float(input("Price: "))
p2_name = input("Product 2: ")
p2_price = float(input("Price: "))
p3_name = input("Product 3: ")
p3_price = float(input("Price: "))
subtotal = p1_price + p2_price + p3_price
vat = round(subtotal * 0.21, 2)
total = round(subtotal + vat, 2)
print(f"\n--- Receipt ---")
print(f"{p1_name}: {p1_price} €")
print(f"{p2_name}: {p2_price} €")
print(f"{p3_name}: {p3_price} €")
print(f"Subtotal: {round(subtotal, 2)} €")
print(f"VAT (21%): {vat} €")
print(f"Total: {total} €")
Solution Exercise 2:
# Temperature converter
celsius = float(input("Temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
kelvin = celsius + 273.15
print(f"\n{celsius} °C is equivalent to:")
print(f"→ {fahrenheit} °F")
print(f"→ {kelvin} K")
Solution Exercise 3:
# Bill splitter
bill = float(input("Bill amount: "))
tip_pct = float(input("Tip percentage (%): "))
people = int(input("Number of people: "))
tip = round(bill * tip_pct / 100, 2)
total = round(bill + tip, 2)
per_person = round(total / people, 2)
print(f"\n--- Bill split ---")
print(f"Subtotal: {bill} €")
print(f"Tip ({int(tip_pct)}%): {tip} €")
print(f"Total with tip: {total} €")
print(f"Each person pays: {per_person} €")
Solution Challenge exercise:
# Complete student profile
name = input("Full name: ")
age = int(input("Age: "))
degree = input("Degree: ")
university = input("University: ")
s1_name = input("Subject 1 name: ")
s1_grade = float(input(f"Grade for {s1_name}: "))
s2_name = input("Subject 2 name: ")
s2_grade = float(input(f"Grade for {s2_name}: "))
s3_name = input("Subject 3 name: ")
s3_grade = float(input(f"Grade for {s3_name}: "))
s4_name = input("Subject 4 name: ")
s4_grade = float(input(f"Grade for {s4_name}: "))
average = round((s1_grade + s2_grade + s3_grade + s4_grade) / 4, 2)
highest = max(s1_grade, s2_grade, s3_grade, s4_grade)
lowest = min(s1_grade, s2_grade, s3_grade, s4_grade)
passed = average >= 5.0
passed_count = (s1_grade >= 5) + (s2_grade >= 5) + (s3_grade >= 5) + (s4_grade >= 5)
credits = passed_count * 6
print("=" * 29)
print(" STUDENT PROFILE")
print("=" * 29)
print(f"Name: {name}")
print(f"Age: {age} years")
print(f"Degree: {degree}")
print(f"University: {university}")
print("-" * 29)
print(f"{s1_name}: {s1_grade}")
print(f"{s2_name}: {s2_grade}")
print(f"{s3_name}: {s3_grade}")
print(f"{s4_name}: {s4_grade}")
print("-" * 29)
print(f"Average grade: {average}")
print(f"Highest grade: {highest}")
print(f"Lowest grade: {lowest}")
print(f"Passed?: {passed}")
print(f"Estimated credits: {credits}")
print("=" * 29)
Cheat sheet — Variables and data types in Python
# ============================================
# CHEAT SHEET — Variables and data types
# Sergio Learns · sergiolearns.com
# ============================================
# BASIC TYPES
x = 5 # int — integer number
x = 5.0 # float — decimal number
x = "hello" # str — text (single or double quotes)
x = True # bool — True or False
# CHECK THE TYPE
type(x) # returns the type of x
# TYPE CONVERSION
int("5") # → 5
float("5.0") # → 5.0
str(5) # → "5"
bool(0) # → False (0 is False, any other number is True)
# INPUT — ALWAYS RETURNS TEXT
name = input("Name: ") # str
age = int(input("Age: ")) # int
price = float(input("Price: ")) # float
# NUMERIC OPERATORS
5 + 3 # addition → 8
5 - 3 # subtraction → 2
5 * 3 # multiplication → 15
5 / 3 # division → 1.666...
5 // 3 # floor division → 1
5 % 3 # modulo (remainder) → 2
5 ** 3 # power → 125
# ROUNDING
round(7.1666, 2) # → 7.17
# F-STRINGS
name = "Sergio"
age = 20
print(f"Hello, {name}. You are {age} years old.")
# STRING OPERATIONS
"hello" + " world" # concatenate → "hello world"
"ha" * 3 # repeat → "hahaha"
"=" * 20 # separator → "===================="
# COMMON TRAPS
"5" + "5" # → "55" NOT 10
5 / 2 # → 2.5 NOT 2 (use // for integer division)
int(7.9) # → 7 truncates, does NOT round
