Level 1
0 / 100 XP

Math in Python

Python can do math. Not just simple addition — any calculation you'd reach for a calculator to do, Python handles in a single line.

The seven operators

Python
print(10 + 3) # 13 — addition print(10 - 3) # 7 — subtraction print(10 * 3) # 30 — multiplication print(10 / 3) # 3.3333... — division print(10 // 3) # 3 — floor division print(10 % 3) # 1 — remainder print(10 ** 3) # 1000 — exponent

Most of these are familiar. The last three are worth a closer look.

Floor division //

Regular division gives you the exact result. Floor division throws away everything after the decimal and gives you a whole number:

Python
print(10 / 3) # 3.3333333333333335 print(10 // 3) # 3

Think of it as "how many times does 3 fit completely into 10?"

Remainder %

The % operator gives you what's left over after floor division:

Python
print(10 % 3) # 1

10 divided by 3 is 3 remainder 1. That leftover 1 is what % returns. This comes up more than you'd expect — checking if a number is even, splitting things into groups, working with time.

Exponent **

Two stars means "to the power of":

Python
print(2 ** 8) # 256 print(3 ** 3) # 27 print(10 ** 6) # 1000000

Order of operations

Python follows the same rules as regular math — exponents first, then multiplication and division, then addition and subtraction:

Python
print(2 + 3 * 4) # 14, not 20 print((2 + 3) * 4) # 20

When in doubt, use parentheses. They make your intent clear and your code easier to read.

Key takeaways

  • Python has seven arithmetic operators — +, -, *, /, //, %, **
  • // gives you a whole number result; / always gives a float
  • % gives you the remainder — it comes up constantly in real code
  • Python follows standard order of operations; use parentheses to be explicit