tabreturn.github.io

# Processing.py in Ten Lessons – 1.5: Arithmetic Operators

2018-06-16

## Arithmetic Operators

Variables are far more useful when you perform arithmetic operations using them. Add the following code to your “variables” sketch:

I’m guessing that did exactly what you expected? You can also subtract:

Multiplication is performed using the `*` operator:

Now add the code below, but before hitting run, see if you can predict what the total is:

The console displays a `7` – and not a `9` – because multiplication occurs before addition. Certain operators take precedence over others. Remember BEDMAS? Or BODMAS (depending on where you’re from)? It’s an acronym to help you recall the order of operations. If you want to override this order, use brackets:

For division, use a forward-slash:

Be aware, though, that dividing two integers always produces an integer result (integers are ‘whole’ numbers, as opposed to those with a decimal point). For example:

Python discards any decimal digits, effectively ‘rounding-down’ the result. To allow decimal results, define at least one of your operands using a decimal point:

Decimal numbers are usually referred to as floating point, or float values, in programming terminology.

Of course, division by zero operations will result in errors:

### Modulo Operator

The modulo operator is written as a percentage sign (`%`). It calculates the remainder of a division operation. Take five divided by two as an example:

• one could say the answer is `2.5`;
• or, that the answer is `2` remainder `1`, because two ‘goes into’ five twice with one left over.

The modulo operator performs the latter. Here’s some code contrasting a division and modulo operation:

It may not be evident why this is operator useful. However, many important algorithms – such as those used in cryptography – make use of it. For now, consider that modulo operations resulting in a `0` indicate that numbers divide exactly. Among other uses, this is handy for establishing whether a number is odd or even:

You’ll be making use of modulo operators in future lessons.

Time for another challenge!

The idea here’s to follow the instructions to reveal a symbol. Create a new sketch and save it as “symbol_reveal”. Add some code to get started:

To begin revealing the symbol, follow instruction steps 1 to 6. To get you started, here’s the first instruction, along with the correct code:

1. Draw a line beginning at an x-coordinate of half the display window `width`, and y-coordinate of a third of the window `height`. The endpoint must have an x/y-coordinate equal to `xco` & `yco`.

– which will be coded as follows:

1. Draw a centred ellipse with a width that's an eleventh of the display window `width`, and a height that's a fourteenth of the window `height`.
2. Draw a centred ellipse with a width that's a nineteenth of the display window `width`, and a height that's a twenty-second of the window `height`.
3. Draw a line beginning at an x/y-coordinate equal to `xco` & `yco` respectively. The endpoint must have an x-coordinate of the display window `width` minus `xco`, and a y-coordinate equal to `yco`.
4. Draw a line beginning at an x-coordinate of the display window `width` minus `xco`, and y-coordinate equal to `yco`. The endpoint must have an x-coordinate of half the display window `width`, and a y-coordinate of a third of the window `height`.
5. Draw a centred ellipse with a width that's a fifth of the display window `width`, and height that's a twelfth of the display window `height`.