Arithmetic Operators

In this step you will look at arithmetic operators. We'll go over the usual ones, and add a few extras that have a particular purpose in programming.

In this step you will look at arithmetic operators in Python. We’ll go over the usual ones, and add a few extras that have a particular purpose in programming.

A quick note about data types

In the previous week we mentioned the Boolean data type (bool). In this section, we’ll deal with integers and floating-point numbers.

Integers are whole numbers, e.g. 50, 47, 15, 13, -13 (yes, whole minus numbers are also integers).

Floating-point numbers (floats) are numbers with a decimal point, e.g. 6.25, 1.0, 3.14159.

Using the four basic operators: plus +, minus -, multiply *, divide /

By using the mathematical operators, Python can work as a calculator. As we go through the step, try out the code in your chosen Python editor (advice for this is in the “What you need” article from the start of Week 1).

For example:

1000 - 200 + 50

Technically, it is evaluating the expression 1000 – 200 + 50, which evaluates to 850. Test this out for multiplication and division too.

7 * 8

This gives the integer 56, as you would expect.

9 / 2

This gives the float 4.5, again as you would expect.

Integer division versus real division

Mathematicians and programmers refer to the type of division used above as ‘real division’ (or sometimes ‘true division’). If there is a fractional part to the answer, it returns a float.

We also use another type of division known as integer (and sometimes ‘floor’) division (or DIV). Try this out in Python:

9 // 2

This evaluates the expression ‘9 integer divided by 2’, and returns just the integer part of the answer, i.e. 4. The fractional part is effectively ignored.

You would use this in a program when you only want an integer answer. For example, calculating how many three-foot-long shelves you could make from a length of wood.

wood_length = int(input('How much wood do you have? '))shelf_number = wood_length // 3print ('You can build', shelf_number, 'shelves')

Using variables to store numerical values

Python variables can store the result of a numerical operation. In the shelf program above, the variable wood_length is populated by user input. This variable is then reused in combination with an arithmetic operator (integer division). This populates another variable: shelf_number = wood_length // 3.

This variable can then be used in the program in other calculations. For example, calculating the number of rooms we can furnish by reusing the shelf_number variable in combination with a new variable shelves_per_room:

wood_length = int(input('How much wood do you have? '))shelf_number = wood_length // 3shelves_per_room = 10print ('You can build', shelf_number, 'shelves')print ('You can furnish', shelf_number // shelves_per_room, 'rooms')

Modulus operator

Modulus (or MOD) is another type of operation. The modulus takes an expression and returns just the remainder. What do you think would be returned here?

9 % 2

You will have observed that 1 was printed to the screen: 9 divided by 2 is 4 with a remainder of 1.

13 % 5

In this example, 3 would be output since 13 divided by 5 is 2 with a remainder of 3.

Modulus can be used in a program when you want to undertake a division and want to know what’s left over. In your shelf program, modulus can determine if the remaining wood is enough to make one or two plaques that are each 1 foot long.

wood_length = int(input('How much wood do you have? '))remainder = wood_length % 3if remainder == 1: print ('You have got', remainder, 'foot left, at least enough to make a plaque')elif remainder == 2: print ('You have got', remainder, 'feet left, at least enough to make two plaques')

The modulus and integer division operators present the full picture of the integer and its remainder. In the number example above, 9 // 2 evaluated to 4 and 9 % 2 evaluated to 1. This represents the answer to 9 divided by 2: 4, remainder 1.

Power operator

The power operator (also known as index or exponent) determines how many times to multiply a number by itself.

3 to the power of 2 is 3 × 3.
3 to the power of 4 is 3 × 3 × 3 × 3.

In Python this is expressed using a double asterisk before the power. What do you think the result of this would be in Python?:

5**3

This means 5 × 5 × 5, which equals 125.

Try to predict what the expressions below evaluate to, then try them out in Python.

Operator Name Symbol Try this Evaluates to
Addition + 5 + 2 ?
Subtraction 5 – 2 ?
Multiplication * 5 * 2 ?
Real division / 5 / 2 ?
Integer division // 5 // 2 ?
Modulus % 5 % 2 ?
Power ** 5**2 ?

BIDMAS

Python also respects the usual order of operations, which we call BIDMAS. We might expect an expression to be evaluated from left to right, but if there is a mixture of operators this is not the case. Keeping to the rule of BIDMAS, the expressions are evaluated in the following order:

Operation Symbols
Brackets ()
Indices **
Division / or //
Multiplication *
Subtraction

So, for example, try the expression below:

4 + 1 * 2

This evaluates to 6 instead of 10. Instead of going from left to right, it uses BIDMAS and puts multiplication before addition. So the computer evaluates 1 × 2 first, giving 2. It then adds on the 4, returning 6.

If you want to override this, brackets are essential. If you want to know what 4 plus 1, then multiplied by 2, is, you can write:

(4 + 1) * 2

This returns 10, because brackets are always evaluated first.

It’s worth remembering BIDMAS when evaluating maths expressions. In general, brackets should be used; they allow you to consider and evaluate terms in the correct order. It also makes your programs easier to read.

Over to you

Have a go at writing some of your own short programs using arithmetic operators. Once you’ve done this, comment here and share a link to your code (e.g. using Pastebin), along with any comments or questions.

In the next step you will test your knowledge of operators by completing the combining operators quiz.