Python Coding Exercise #06: Arithmetics
Part of the series Python Practice Exercise — part 6
Before you start: Know what a variable is · Know print() and f-strings
The question
This program already contains two integer variables,
xandy:
x = 27
y = 15
Please complete the program so that it also prints out the following:
27 + 15 = 42
27 - 15 = 12
27 * 15 = 405
27 / 15 = 1.8
The program should work correctly even if the values of the variables are changed. That is, if the first two lines are replaced with this:
x = 4
y = 9
the program should print out the following:
4 + 9 = 13
4 - 9 = -5
4 * 9 = 36
4 / 9 = 0.4444444444444444
My Solution
x = 27
y = 15
add = x + y
subtract = x - y
multiplication = x * y
division = x / y
print(f"{x} + {y} = {add}")
print(f"{x} - {y} = {subtract}")
print(f"{x} * {y} = {multiplication}")
print(f"{x} / {y} = {division}")
Output:
27 + 15 = 42
27 - 15 = 12
27 * 15 = 405
27 / 15 = 1.8
Change the first two lines to x = 4 and y = 9, and the same program prints:
4 + 9 = 13
4 - 9 = -5
4 * 9 = 36
4 / 9 = 0.4444444444444444
How My Solution Works
1. Store each result in a variable
The four calculations use Python’s arithmetic operators:
| Operator | Meaning | With x = 27, y = 15 |
|---|---|---|
+ | addition | 42 |
- | subtraction | 12 |
* | multiplication | 405 |
/ | division | 1.8 |
Each result is stored in a variable with a descriptive name (add, subtract, multiplication, division), which makes the print lines easy to read.
2. Use variables, not typed-out numbers
The most important part of this exercise is the sentence “the program should work correctly even if the values of the variables are changed.”
It would be easy to pass the first example by typing the answers directly:
print("27 + 15 = 42")
That prints the right text for 27 and 15, but it is wrong the moment the values change. Instead, the f-string (see Exercise #04) uses {x} and {y} to show the current numbers, and the result variable to show the calculated answer. Because everything comes from the variables, changing x and y at the top changes the whole output.
3. Division always gives a float
Look at the division line. Even though x and y are both integers, / always returns a float, a number with a decimal point:
print(27 / 15) # 1.8
print(10 / 5) # 2.0, not 2
print(4 / 9) # 0.4444444444444444
That is why the last sample output shows a long decimal, and why dividing evenly still gives 2.0. The other three operators keep integers as integers, so 27 + 15 is 42, not 42.0. (There’s more about this in Data Types in Python.)
University Solution: calculate inside the f-string
An alternative is to calculate the expressions directly inside the curly braces of an f-string:
x = 27
y = 15
print(f"{x} + {y} = {x+y}")
print(f"{x} - {y} = {x-y}")
print(f"{x} * {y} = {x*y}")
print(f"{x} / {y} = {x/y}")
This is shorter, and it produces exactly the same output. The University Solution does it this way for the first three lines. For the last line it uses the comma style of print():
print(x, "/", y, "=", (x / y))
That works here because print() puts a space between comma-separated values, and the sample output wants a space on each side of / and =. In Exercise #05 those automatic spaces were the problem. Here they happen to be exactly what we need. It’s still worth knowing which one you’re using, since the f-string version never adds a space you didn’t type.
Which version is better? Storing results in variables is clearer when you want to reuse a result later. Calculating inside the f-string is quicker for a one-off printout. Both are correct.
Try it yourself
Change the values and check that the output stays right, including the awkward ones:
x = 5
y = 0
Now the division line fails, because you can’t divide by zero:
ZeroDivisionError: division by zero
Python stops with an error. The exercise doesn’t ask you to handle this, but it’s a good reminder that a program that works for 27 and 15 isn’t automatically safe for every pair of numbers.
If you’re curious about other operators, // gives the whole-number part of a division, and % gives the remainder:
print(27 // 15) # 1
print(27 % 15) # 12
Key takeaways
- Python’s basic arithmetic operators are
+,-,*and/. - Use variables inside your
print()so the output adapts when the values change. Never type the answers in by hand. /always returns a float, even when the division is exact (10 / 5is2.0).- An f-string can hold a calculation, such as
{x+y}, not just a variable name. - Commas in
print()add spaces automatically. f-strings add only the spaces you type. - Dividing by zero raises a
ZeroDivisionError.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.