Arithmetic and Boolean Operators
More than just plus and minus. This lesson covers the full set of arithmetic operators including MOD and DIV, the boolean operators used to build conditions, and the precedence rules that determine which operations happen first.
The division operator is the most dangerous operator in programming. 7 / 2 gives 3.5 in Python but gave 3 in Python 2. In C# it gives 3 if both numbers are integers, and 3.5 if either is a float. MOD - the remainder after division - is used in dozens of everyday programming tasks: checking if a number is even, wrapping around the end of a list, implementing a cipher, generating a grid position from a flat index. Get the division operators wrong and your program silently computes the wrong answer.
| Operator | Python | C# | Example | Result |
|---|---|---|---|---|
| Addition | + | + | 7 + 3 | 10 |
| Subtraction | - | - | 7 - 3 | 4 |
| Multiplication | * | * | 7 * 3 | 21 |
| True division | / | / (floats) | 7 / 2 | 3.5 |
| Integer division (DIV) | // | / (ints) | 7 // 2 | 3 |
| Modulus (MOD) | % | % | 7 % 2 | 1 |
| Exponentiation | ** | Math.Pow() | 2 ** 8 | 256 |
In C#, 7 / 2 gives 3 (not 3.5) because both operands are integers. To get 3.5, at least one must be a float: 7.0 / 2 or (double)7 / 2. In Python 3, / always gives a float and // always gives an integer - there is no ambiguity. This is a very common source of bugs in C# programs.
# Python arithmetic examples print(17 % 5) # 2 (MOD - remainder) print(17 // 5) # 3 (DIV - integer quotient) print(17 / 5) # 3.4 (true division) print(2 ** 10) # 1024 (exponentiation) # Practical uses of MOD n = 14 if n % 2 == 0: print("even") # MOD 2: even test position = n % 7 # 0 (wrap into range 0-6 - useful for circular lists)
// C# arithmetic examples Console.WriteLine(17 % 5); // 2 (MOD) Console.WriteLine(17 / 5); // 3 (integer DIV - both are ints!) Console.WriteLine(17.0 / 5); // 3.4 (true division - one is double) Console.WriteLine(Math.Pow(2,10)); // 1024.0 (returns double) int n = 14; if (n % 2 == 0) Console.WriteLine("even"); // MOD 2: even test
MOD appears in exam questions constantly. Key uses to recognise: (1) Even/odd test: n MOD 2 = 0 means even. (2) Digit extraction: 173 MOD 10 = 3 (last digit), 173 DIV 10 = 17 (remove last digit). (3) Circular wrap: index MOD length keeps a value in range. (4) Time conversion: 150 DIV 60 = 2 hours, 150 MOD 60 = 30 minutes.
Comparison operators return True or False. Boolean operators combine multiple conditions. Together they form the conditions used in IF statements and WHILE loops.
| Operator | Python | C# | Example | Result |
|---|---|---|---|---|
| Equal to | == | == | 5 == 5 | True |
| Not equal to | != | != | 5 != 3 | True |
| Greater than | > | > | 7 > 3 | True |
| Less than | < | < | 3 < 7 | True |
| Greater or equal | >= | >= | 5 >= 5 | True |
| Less or equal | <= | <= | 4 <= 5 | True |
| AND | and | && | True and False | False |
| OR | or | || | True or False | True |
| NOT | not | ! | not True | False |
# Python boolean examples age = 17 has_id = True # AND: both must be true if age >= 18 and has_id: print("Entry granted") # OR: at least one must be true if age < 0 or age > 120: print("Invalid age") # NOT: flip the boolean if not has_id: print("No ID - entry refused")
// C# boolean examples int age = 17; bool hasId = true; // AND: both must be true if (age >= 18 && hasId) Console.WriteLine("Entry granted"); // OR: at least one must be true if (age < 0 || age > 120) Console.WriteLine("Invalid age"); // NOT: flip the boolean if (!hasId) Console.WriteLine("No ID - entry refused");
Operator precedence (BIDMAS + logic):
# Order: brackets first, then ** (power), then */%, then +-, then comparisons, then not, then and, then or result = 2 + 3 * 4 ** 2 # = 2 + 3 * 16 = 2 + 48 = 50 (NOT 400) check = 4 > 2 and 10 % 3 == 1 # = True and 1 == 1 = True and True = True # Always use brackets when mixing arithmetic and logic safe = (score >= 50) and (grade != "U")
Three Quick Challenges
What is the result of the modulus operation?
print(17 % 5)Console.WriteLine(17 % 5);
Use the floor division operator to get the integer part of 10 divided by 3 (result should be 3, not 3.333...):
This condition raises a SyntaxError. What is wrong?
if x = 5: print("five")
Test yourself
1. What is the value of 23 MOD 7?
2. In C#, what does 9 / 2 evaluate to?
3. What does 2 + 3 * 4 evaluate to in Python?
4. Evaluate: True AND False OR True
5. A program checks: if score >= 50 and score <= 100. Which score fails this condition?
A program needs to convert a number of seconds (e.g. 3750) into hours, minutes and seconds (e.g. 1 hour, 2 minutes, 30 seconds). Write the three calculations using only DIV and MOD. No loops, no libraries - just integer arithmetic. Then verify your answer manually.
hours = 3750 // 3600 - gives 1 (3750 divided by seconds-per-hour)remaining = 3750 % 3600 - gives 150 (leftover seconds after removing whole hours)minutes = remaining // 60 - gives 2 (150 divided by seconds-per-minute)seconds = remaining % 60 - gives 30Verification: 1*3600 + 2*60 + 30 = 3600 + 120 + 30 = 3750. Correct.
Pattern: DIV extracts the whole units; MOD strips them off to get the remainder. This pattern (DIV then MOD) is used in every unit-conversion problem: hours/minutes/seconds, pounds/shillings/pence, degrees/minutes/seconds of arc.
Practice what you have learned
Three levelled worksheets. Download, print and complete offline.