Python operators practice — 3 real programs in VS Code
In the previous article we covered Python operators theory. Now it’s time to write real code. In this article we build three programs from scratch in VS Code using all the operators you learned: a unit converter, a discount calculator and a full calculator with a menu. Each program goes from simpler to more complete, just like the exercises in class.
Before starting make sure you have VS Code configured. Create a new folder called operators_practice and inside it create a separate file for each program.
Practising Python operators with real code is the best way to consolidate them. I also recommend using pythontutor.com to visualise step by step what’s happening.
Python operators practice — Program 1: Unit converter
Create the file converter.py. This program converts between the most commonly used units:
# Unit converter
print("=== UNIT CONVERTER ===\n")
# Input
value = float(input("Enter the value to convert: "))
print("\n--- Distance ---")
km_to_miles = round(value * 0.621371, 4)
miles_to_km = round(value * 1.60934, 4)
print(f"{value} km → {km_to_miles} miles")
print(f"{value} miles → {miles_to_km} km")
print("\n--- Weight ---")
kg_to_lbs = round(value * 2.20462, 4)
lbs_to_kg = round(value * 0.453592, 4)
print(f"{value} kg → {kg_to_lbs} lbs")
print(f"{value} lbs → {lbs_to_kg} kg")
print("\n--- Temperature ---")
celsius_to_fahrenheit = round((value * 9/5) + 32, 2)
fahrenheit_to_celsius = round((value - 32) * 5/9, 2)
print(f"{value} °C → {celsius_to_fahrenheit} °F")
print(f"{value} °F → {fahrenheit_to_celsius} °C")
Output with value 100:
=== UNIT CONVERTER === --- Distance --- 100 km → 62.1371 miles 100 miles → 160.934 km --- Weight --- 100 kg → 220.462 lbs 100 lbs → 45.3592 kg --- Temperature --- 100 °C → 212.0 °F 100 °F → 37.78 °C
Notice what we’re using here: * to multiply the conversion factors, / for temperature, + and - also in temperature, round() to control decimal places, and f-strings to display everything cleanly. Every arithmetic operator in action.
Python operators practice — Program 2: Discount calculator
Create the file discounts.py. Very useful for understanding how the % operator works in a real context, here it really is a mathematical percentage, not the modulo operator:
# Discount and price calculator
print("=== DISCOUNT CALCULATOR ===\n")
product = input("Product name: ")
original_price = float(input("Original price (€): "))
discount_pct = float(input("Discount (%): "))
# Calculations
discount_euros = round(original_price * discount_pct / 100, 2)
final_price = round(original_price - discount_euros, 2)
vat = round(final_price * 0.21, 2)
price_with_vat = round(final_price + vat, 2)
total_savings = round(original_price - final_price, 2)
# Detailed result
print(f"\n--- {product} breakdown ---")
print(f"Original price: {original_price} €")
print(f"Discount ({int(discount_pct)}%): -{discount_euros} €")
print(f"Price after discount: {final_price} €")
print(f"VAT (21%): +{vat} €")
print(f"Final price with VAT: {price_with_vat} €")
print(f"\nTotal savings: {total_savings} €")
Output:
=== DISCOUNT CALCULATOR === Product name: Laptop Original price (€): 899 Discount (%): 15 --- Laptop breakdown --- Original price: 899.0 € Discount (15%): -134.85 € Price after discount: 764.15 € VAT (21%): +160.47 € Final price with VAT: 924.62 € Total savings: 134.85 €
Here we use * and / to calculate percentages, - and + to apply them, and round() at each step to avoid accumulated decimal errors. Notice that VAT is applied to the discounted price, not the original, that’s the correct calculation.
Python operators practice — Program 3: Full calculator with menu
This is the most ambitious one. Create the file calculator.py. It uses the % operator as modulo to detect exact divisions, and // for floor division:
# Full calculator with menu
print("=== PYTHON CALCULATOR ===")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Floor division")
print("6. Modulo (remainder)")
print("7. Power")
option = int(input("\nChoose an option (1-7): "))
a = float(input("First number: "))
b = float(input("Second number: "))
print("\n--- Result ---")
if option == 1:
result = a + b
print(f"{a} + {b} = {result}")
elif option == 2:
result = a - b
print(f"{a} - {b} = {result}")
elif option == 3:
result = a * b
print(f"{a} × {b} = {result}")
elif option == 4:
if b != 0:
result = round(a / b, 4)
print(f"{a} / {b} = {result}")
else:
print("Error: cannot divide by zero")
elif option == 5:
if b != 0:
result = int(a // b)
remainder = int(a % b)
print(f"{int(a)} // {int(b)} = {result} (remainder: {remainder})")
else:
print("Error: cannot divide by zero")
elif option == 6:
if b != 0:
result = int(a % b)
print(f"{int(a)} % {int(b)} = {result}")
if result == 0:
print(f"→ {int(a)} is divisible by {int(b)}")
else:
print(f"→ {int(a)} is NOT divisible by {int(b)}")
else:
print("Error: cannot divide by zero")
elif option == 7:
result = a ** b
print(f"{a} ^ {b} = {result}")
else:
print("Invalid option")
Output with option 5 (floor division):
=== PYTHON CALCULATOR === 1. Addition ... Choose an option (1-7): 5 First number: 17 Second number: 5 --- Result --- 17 // 5 = 3 (remainder: 2)
This program already uses if/elif/else structures that we’ll cover in depth in the next topic, but the logic is intuitive. The important thing here is to see how each operator produces a different result with the same numbers, especially options 5 and 6 which show // and % working together.
The % trick you’ll use most
Of all the operators, % is the one that confuses the most and gets used the most in FP1 exercises. Here are the three most common patterns:
# Is it even or odd? number = 7 print(number % 2 == 0) # → False (odd) print(number % 2 == 1) # → True (odd confirmed) # Is it divisible by N? print(15 % 3 == 0) # → True (15 is divisible by 3) print(16 % 3 == 0) # → False (16 is not divisible by 3) # Extract the digits of a number number = 1234 units = number % 10 # → 4 tens = (number // 10) % 10 # → 3 hundreds = (number // 100) % 10 # → 2 thousands = (number // 1000) # → 1
That last example — extracting digits with % and // combined, is one of those patterns that appears in exams more often than you’d expect. It’s worth understanding it properly.
Visualise it with Python Tutor
Paste the unit converter into pythontutor.com (select Python 3) and step through it line by line. Watch how each variable gets created in memory with its type and value — especially useful for seeing why round((value * 9/5) + 32, 2) evaluates in a specific order, and how Python handles the intermediate results before rounding.
Summary and next step
In this article you practised Python operators with three real programs. The key concepts were * and / for conversions and percentages, // for floor division, % for remainders and divisibility, and how to combine operators with round() and f-strings for clean results.
In the next article in this series you’ll find proposed exercises with solutions to practice on your own.

One Comment