Basic Math Operations
Perform arithmetic operations and understand operator precedence. Read the lesson first, then move through the exercises in order.
After reading
Practice Arena
Begin with the first exercise, then continue step by step through the module.
Start with Simple Addition & SubtractionStudy Material
Read the full lesson
Python as a calculator
Python can do math directly in your code. That means you can use it like a calculator, but with the extra power of variables, formulas, and program logic.
This module teaches the operators that appear in beginner Python again and again.
The four basic operations
The first operators are the ones you already know from school.
pythonprint(5 + 3) print(10 - 4) print(6 * 7) print(8 / 2)
These mean:
+addition-subtraction*multiplication/division
They work with variables too.
pythonwidth = 8 height = 5 area = width * height print(area)
Division gives a float
A detail that surprises many beginners is that / returns a float.
pythonprint(10 / 2)
The result is 5.0, not 5.
Python does this because regular division is treated as decimal division.
Floor division with //
If you want the whole-number result of a division, use //.
pythonprint(14 // 4)
This gives 3.
Floor division is useful when you want complete groups, positions, or counts without the decimal part.
The remainder with %
The % operator gives the remainder after division.
pythonprint(14 % 4)
This gives 2.
The modulo operator is very useful. A classic example is checking whether a number is even or odd.
pythonprint(8 % 2) print(7 % 2)
If the remainder is 0, the number is even.
Powers with **
Python uses ** for powers.
pythonprint(2 ** 3) print(5 ** 2)
These mean 2 to the power of 3 and 5 squared.
This operator appears often in beginner math formulas.
Square roots and powers
A square root can be written as a power of 0.5.
pythonprint(9 ** 0.5)
This gives 3.0.
You do not need advanced math here. The important idea is that Python can handle more than just simple addition and subtraction.
Using variables in math
Math becomes much more useful when the values come from variables.
pythonprice = 12 quantity = 4 total = price * quantity print(total)
This makes formulas easier to read and easier to change.
Operator precedence
Python does not always calculate from left to right. It follows rules called operator precedence.
For example:
pythonprint(2 + 3 * 4)
The multiplication happens first, so the result is 14, not 20.
A simple rule is:
- parentheses first
- powers
- multiplication and division
- addition and subtraction
When parentheses help
Parentheses make your intention clear.
pythonprint((2 + 3) * 4)
Now the result is 20 because the addition happens first.
Even when Python would already do the correct thing, parentheses often make beginner code easier to understand.
Common math patterns in beginner code
These patterns appear again and again:
pythonaverage = (10 + 20 + 30) / 3 seconds = minutes * 60 bmi = weight / (height ** 2)
Once you understand the operators, these formulas become much easier to read.
Common mistakes to avoid
A few mistakes happen often in this module:
- using
/when you expected a whole number - forgetting that
%gives a remainder, not a percentage - forgetting parentheses in a formula
- mixing text and numbers in the same calculation
When a result looks strange, check the operator you used and the order of the calculation.
What this lesson should give you
After this lesson, you should understand how to:
- add, subtract, multiply, and divide in Python
- use
//,%, and** - read and write simple formulas
- use variables inside math expressions
- understand the order in which Python evaluates expressions
- use parentheses to make formulas clear
These ideas will support many later exercises, including calculators, converters, and data-processing tasks.
Interactive
Exercises for this topic
These exercises follow the exact order of the lesson. Move step-by-step from reading into coding.
Simple Addition & Subtraction
Complete the "Simple Addition & Subtraction" exercise.
Float vs Integer Division
Complete the "Float vs Integer Division" exercise.
Modulo Operator
Complete the "Modulo Operator" exercise.
Exponents & Roots
Complete the "Exponents & Roots" exercise.
Operator Precedence (PEMDAS)
Complete the "Operator Precedence (PEMDAS)" exercise.