Mathematical Operators
Mathematical operators are the basic operations that can be performed with numerical data. They are:
+
Addition; this results in the sum of two values being computed.
–
Subtraction; this results in the second value in the expression being subtracted from the first.
*
Multiplication; multiply two numbers together
/
Division; divide the first value by the second value
% or MOD
This performs an integer division and returns the remainder. In normal division, the result of 8/5 would be 1.6
Integer division would yield a result of 1 – just the integer part of the result. However, as we have seen above, 8/5 is not 1. We have a remainder of 3 – so 8%5 or 8 MOD 5 would give the result 3.
One example of its use is to determine whether a number is even or odd:
n % 2 gives a result of 0 for an even number, and 1 for an odd number (where n is any integer).