PythonMastery

Basic arithmetic in Python

Add, subtract, multiply and divide two numbers in Python, plus floor division (//), remainder (%) and powers (**), with the output of each.

Python has seven arithmetic operators. Four of them work as on a calculator; the other three are worth learning early.

The program

python
a = 15
b = 4

print("a + b  =", a + b)
print("a - b  =", a - b)
print("a * b  =", a * b)
print("a / b  =", a / b)
print("a // b =", a // b)
print("a % b  =", a % b)
print("a ** b =", a ** b)
output
a + b  = 19
a - b  = 11
a * b  = 60
a / b  = 3.75
a // b = 3
a % b  = 3
a ** b = 50625

How it works

// and % are used together a lot. For example, 135 minutes is 135 // 60 hours and 135 % 60 minutes: 2 hours and 15 minutes.

Learn the idea: Operators & Expressions, Numbers in Depth: Integers, Floats, and the math Module