Numbers and Arithmetic
Python is a calculator that never gets tired
Big Idea: Python can do math for you. You type a sum, and Python gives the answer straight away. In this lesson you will learn the math operators (the symbols like + and *), the order Python does them in, and three special ones: //, % and **.
By the end you will be able to:
- add, subtract, multiply and divide with Python,
- use
//for whole division and%for the remainder, - use
**for powers, - work out the answer using the correct order of operations.
1. The four basic operators
Python uses simple symbols for math. They are called operators:
+adds-subtracts*multiplies (a star, not the letter x)/divides
We use print() to show the answer on the screen. Try these:
print(5 + 3) # 8
print(10 - 4) # 6
print(4 * 6) # 24
print(20 / 4) # 5.0
The line after # is a comment — a note for people that Python ignores. Here it shows the expected output.
2. Division always gives a decimal
Watch out for one surprise. In Python, the / operator always gives a decimal number (called a float), even when it divides evenly:
print(9 / 3) # 3.0 (division always gives a decimal)
print(7 / 2) # 3.5
print(10 / 5) # 2.0
So 10 / 5 is 2.0, not 2. The little .0 tells you it is a decimal. This is normal in Python.