Skip to main content

How to Add, Subtract, Multiply and Divide Numbers in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Python does arithmetic with the same symbols you'd use on paper, apart from * for multiply and / for divide. Two things surprise newcomers: dividing always gives a decimal number, and multiplication happens before addition. This guide shows all four operations on Python 3.15.2.

Syntax​

Syntax
<a> + <b>
<a> - <b>
<a> * <b>
<a> / <b>
PartWhat it means
<a>, <b>The numbers, or variables that hold numbers
+Adds <a> and <b>
-Subtracts <b> from <a>
*Multiplies <a> by <b>
/Divides <a> by <b>. The result is always a float, a number with a decimal point

The Python documentation describes the results as the "sum of x and y", "difference of x and y", "product of x and y" and "quotient of x and y". The language reference adds: "Division of integers yields a float, while floor division of integers results in an integer".

The Example​

The numbers 7 and 2 go through each of the four operations. Then two calculations with three numbers show the order Python works in: 2 + 3 * 4, and the same with brackets, (2 + 3) * 4.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

arithmetic.py
print(7 + 2)
print(7 - 2)
print(7 * 2)
print(7 / 2)
print(2 + 3 * 4)
print((2 + 3) * 4)

The arithmetic.py script in a macOS-style code window: it prints 7 + 2, 7 - 2, 7 * 2, 7 / 2, then 2 + 3 * 4 and (2 + 3) * 4.

Step 2: Run It​

Terminal
uv run python arithmetic.py
Output
9
5
14
3.5
14
20

The terminal after uv run python arithmetic.py: 9, 5, 14, 3.5, 14 and 20, one per line.

The first three are whole numbers. 7 / 2 gave 3.5, because division keeps the fraction. 2 + 3 * 4 gave 14, not 20: Python multiplies before it adds, as in maths. The brackets in (2 + 3) * 4 make the addition happen first, which gave 20. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting / to give a whole number6 / 2 gave 3.0, a floatUse // when you want a whole number: 7 // 2 gave 3
Dividing by zeroZeroDivisionError: division by zeroCheck that the number you divide by isn't 0 first
Adding a number to text that looks like a number"7" + 2 raised TypeError: can only concatenate str (not "int") to strTurn the text into a number first: int("7") + 2 gave 9

FAQ​

What does // do?​

It's floor division: it divides and rounds the result down to a whole number. 7 // 2 gave 3. The documentation says the result "is always rounded towards minus infinity".

In what order does Python calculate?​

Brackets first, then *, /, // and %, then + and -. Operators at the same level work from left to right. The operator precedence table lists every level.

Conclusion​

+, - and * turned 7 and 2 into 9, 5 and 14, and / gave 3.5. 2 + 3 * 4 gave 14 because multiplication goes first, and brackets changed it to 20.

Additional Resources​