Arithmetic

Let’s look at some basic math:

>>> 2 + 2
4

Python has numbers and arithmetic, just like every other programming language.

You can add integers. You can add floating point numbers–programmer speak for decimal numbers.

>>> 2 + 2
4
>>> 1.4 + 2.25
3.65

You can subtract, multiply, and divide numbers.

>>> 4 - 2
2
>>> 2 * 3
6
>>> 4 / 2
2.0
>>> 3 / 2
1.5

If you want your integers to round-down you can use a double slash:

>>> 3 // 2
1

We can use double asterisks to raise a number to a power. For example, 2 to the 10th power is 1024:

>>> 2 ** 10
1024

Time to Play! Math 👾

Open up your terminal window, type python3 and play! See what you can do with numbers. Try out some of the code below and see if you can guess what’s going on.

>>> round(1.5)
>>> round(2.5)
>>> 4 % 3
>>> 10 % 5