Beginner Python tutorial – 05 – Basic Arithmetics

Video

When we code we use every day mathematical operations. They are easy to learn but we need to really understand them in order to avoid errors that we may overlook. Let’s go!

Add

An easy one:

print(16 + 4)

That will return 20. Keep in mind that when we add two (or more) values, every element has to be a number:

print(16 + 'b')
print(16 + '4')

Both lines will throw an error. The first one, because we can’t add ‘b’ to 16, and the second one because we can’t add ‘4’ to 16. When we surround a number with quotes, we are saying “This is a string of text”, so we can’t add them. Unless we convert that ‘4’ into a number. Like this:

print(16 + int('4'))

Subtract

Like add, another easy one:

print(16 - 4)

We should get a 12 here. Same rules apply to subtract and every other operation. Only between numbers, unless we convert them into a number with int()

Multiply

print(16 * 4)

I don’t need to explain how this works, right?

Divide

print(16 / 4)

Another easy one. This returns what you expect, 4.0. Wasn’t that what you expected? Divisions returns us the result of the division with decimals:

print(18 / 4)

This returns 4.5. But what if you don’t want decimals and you just want the whole number? If we have 38 elements, we want to know how many boxes of 12 we want, not how many elements are left over.

Floor division

print(16 // 4)

This returns 4. No decimals, just the whole number. You can check how any extra decimals are ignored:

print(18 // 4)

Remember the example of the 38 elements and the boxes of 12 to store them? What if we just want the remainder of that division, 6?

Modulus

print(10 % 3)

This will return 1. We can divide 10 by 3, 3 times. Only 1 is left.

Exponent

Sometimes we want to multiply the same number a certain number of times. Use ** to calculate power operations :

print(2 ** 3)

That’s the equivalent of 2 * 2 * 2. 2 Multiplied by itself 3 times.

Precedence

Some operations have preference when they have to be calculated. 3 + 1 * 6 will return 9, not 24, as first the multiplication is calculated before the add operation. Let’s see the order of operations:

() >> ** >> * / //  % >> + -

But as always, to learn something is better to practice instead of just read. So yes, you guessed. Time for…

Challenge time

I’m going to be optimistic this time and I will assume that you know how to add and multiply on your own, so let’s see if we understood how Python calculates mathematical operations and which operations will take precedence.

Here I will present you with a few operations. While they are simple, you have to guess what is the result of that operation.

The point of this challenge is not that you have to add or subtract numbers, but to understand the order that Python uses to calculate operations.

So, read every line and try to predict the first operation. Take you time, you’re not in a hurry, and think how every operation is calculated. Then print the first line and see if you guess was right. If not, think how it was calculated.

Do the same with each operation and have fun!

print(6 + 5 * 3)
print((6 + 5) * 3)

print(10 % 3 + 3)

print(5 ** 3 % 10)

print(13 // 7 + 6)
print(13 // (7 + 6))
print(13 // (7 + 7))

If it is still something you don’t fully grasp, think in basic operations like these ones on your own and solve them before printing. This is very important to understand while coding if you don’t want nasty surprises. Especially if you are calculating taxes!

The REPL with my code: https://repl.it/@DavidMM1707/05-BasicArithmetics

Video version: https://www.youtube.com/watch?v=C34OAmXPROE

Find me on Twitter at: https://twitter.com/DavidMM1707

My GitHub: https://github.com/david1707

Learn more about basic operations: https://www.tutorialspoint.com/python/python_basic_operators.htm

Sixth lesson: 06 – FizzBuzz challenge