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
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:
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:
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":
Order of operations
Python follows the same rules as regular math — exponents first, then multiplication and division, then addition and subtraction:
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
No comments yet. Add the first comment to start the discussion.