Make It Do Math
Learn how to use +, -, *, /, and print() with numbers.
We'll cover the following...
Last time, we said “Hello” to our computer. This time, let’s make Python do some number crunching!
Python is amazing at math; unlike us, it never forgets or makes mistakes. So let’s put it to work.
Start calculating
Action: Click the “Run” button and see what happens.
Python did the math for us—just like that. Note that it didn’t print 4 + 7, it printed the result.
Your turn: Experiment with numbers
Here are some operations that Python can handle:
Arithmetic Operator | Operation | Example |
| Addition | 10 + 5 results in 15 |
| Subtraction | 10 - 5 results in 5 |
| Multiplication | 10 * 5 results in 50 |
| Division | 10 / 5 results in 2 |
| Modulus | 10 % 5 results in 0 |
Note: The modulus operator (
%) gives us the remainder after division.
For example:
print(10 % 3)
This prints 1 because 10 divided by 3 is 3 with a remainder of 1.
Try changing the numbers and operations to see what happens.
Let’s write code to perform multiplication:
Let’s write code to perform division:
Try:
Different numbers
Different operations
A mix of big and small numbers
Every time we click “Run,” Python gives us an instant answer.
What’s happening here?
We use Python like a calculator, and it’s doing exactly what we say:
+adds-subtracts*multiplies/divides
Just like in math class, but a lot more fun (and forgiving).
And yes, Python follows the normal math rules, like parentheses:
Can you guess the output of the example below before you click the “Run” button?
Good job. Keep it up!