« 1.4: Variables 
1.6: Disk Space Analyser Task »
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 forwardslash:
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 ‘roundingdown’ 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
remainder1
, 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.
Image Reveal Task
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:
 Draw a line beginning at an xcoordinate of half the display window
width
, and ycoordinate of a third of the windowheight
. The endpoint must have an x/ycoordinate equal toxco
&yco
.
– which will be coded as follows:
Now, carryout the rest of the instructions:

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 windowheight
. 
Draw a centred ellipse with a width that's a nineteenth of the display window
width
, and a height that's a twentysecond of the windowheight
. 
Draw a line beginning at an x/ycoordinate equal to
xco
&yco
respectively. The endpoint must have an xcoordinate of the display windowwidth
minusxco
, and a ycoordinate equal toyco
. 
Draw a line beginning at an xcoordinate of the display window
width
minusxco
, and ycoordinate equal toyco
. The endpoint must have an xcoordinate of half the display windowwidth
, and a ycoordinate of a third of the windowheight
. 
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 windowheight
.
Clue: if this seems like a conspiracy, you may be on the right track.
1.6: Disk Space Analyser Task »
Complete list of Processing.py lessons
References
 http://py.processing.org/reference/