6.5.1 Arithmetic operators
Arithmetic Operators in Programs
You already know what arithmetic operators do from Topic 1. The focus here is writing programs that use them correctly — choosing the right operator for the task, combining them in expressions, and understanding the results in Python's type system.
| Operator | Operation | Python | C# | VB.NET |
|---|---|---|---|---|
| Addition | Sum | + | + | + |
| Subtraction | Difference | - | - | - |
| Multiplication | Product | * | * | * |
| Division | Real result | / | / (int ÷ int = int) | / |
| Integer division | Floor quotient | // | / (int operands) | |
| Modulus | Remainder | % | % | Mod |
| Exponentiation | Power | ** | Math.Pow() | ^ |
Note: in Python, / always gives a real (float) result even for two integers. Use // for integer division.
Worked Example: Bill Calculator
subtotal = float(input("Subtotal (£): "))
VAT_RATE = 0.20
vat = subtotal * VAT_RATE # multiplication
total = subtotal + vat # addition
saving = total - subtotal # subtraction (same as vat here)
per_head = total / 4 # division — real result
whole = total // 1 # integer division — floor
pence = round((total % 1) * 100) # modulus — pence remainder
tip = subtotal ** 0.5 # exponentiation — square root trick
print("VAT: £" + str(round(vat, 2)))
print("Total: £" + str(round(total, 2)))
print("Per head: £" + str(round(per_head, 2)))
print("Pence: " + str(pence) + "p")
print("Tip idea: £" + str(round(tip, 2)))
double subtotal = double.Parse(Console.ReadLine());
const double VAT_RATE = 0.20;
double vat = subtotal * VAT_RATE;
double total = subtotal + vat;
double perHead = total / 4;
int whole = (int)(total);
int pence = (int)Math.Round((total % 1) * 100);
double tip = Math.Pow(subtotal, 0.5);
Console.WriteLine("VAT: £" + Math.Round(vat, 2));
Console.WriteLine("Total: £" + Math.Round(total, 2));
Console.WriteLine("Per head: £" + Math.Round(perHead, 2));
Console.WriteLine("Pence: " + pence + "p");
Console.WriteLine("Tip idea: £" + Math.Round(tip, 2));
Dim subtotal As Double = Double.Parse(Console.ReadLine())
Const VAT_RATE As Double = 0.20
Dim vat As Double = subtotal * VAT_RATE
Dim total As Double = subtotal + vat
Dim perHead As Double = total / 4
Dim whole As Integer = CInt(Int(total))
Dim pence As Integer = CInt(Math.Round((total Mod 1) * 100))
Dim tip As Double = subtotal ^ 0.5
Console.WriteLine("VAT: £" & Math.Round(vat, 2))
Console.WriteLine("Total: £" & Math.Round(total, 2))
Console.WriteLine("Per head: £" & Math.Round(perHead, 2))
Console.WriteLine("Pence: " & pence & "p")
Console.WriteLine("Tip idea: £" & Math.Round(tip, 2))
The // and % Pair in Practice
Integer division and modulus are complementary — together they decompose a value into quotient and remainder. Common uses: converting seconds to minutes/seconds, checking even/odd, cycling through a fixed range.
# Python — time conversion using // and %
total_seconds = int(input("Enter seconds: "))
minutes = total_seconds // 60
seconds = total_seconds % 60
print(str(minutes) + "m " + str(seconds) + "s")
# Even/odd check
n = int(input("Enter a number: "))
if n % 2 == 0:
print(n, "is even")
else:
print(n, "is odd")
# Cycling 0-4 using modulus
for i in range(10):
print(i % 5, end=" ") # 0 1 2 3 4 0 1 2 3 4
Key Takeaways
- Python
/always returns a float; use//for integer (floor) division. %gives the remainder — useful for even/odd, time conversion, cycling.**(Python) /^(VB.NET) /Math.Pow()(C#) for exponentiation.- Use named constants (
VAT_RATE) rather than magic numbers in calculations.