Part 2: Core Programming Concepts
2. Operators & Expressions
Making the computer calculate and decide
An operator is a symbol that performs an operation on one or more values, called operands. When you combine values and operators you form an expression — something the computer evaluates down to a single result. 3 + 4 is an expression that evaluates to 7; price * quantity is an expression whose result depends on the current values of those variables.
Arithmetic operators
These do ordinary maths. Note the two "division-like" operators many languages provide: regular division and remainder (modulo), which gives what is left over.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 2 | 9 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 7 * 2 | 14 |
/ | Division | 7 / 2 | 3.5 |
% | Modulo (remainder) | 7 % 2 | 1 |
** | Exponent (power) | 7 ** 2 | 49 |
Modulo is more useful than it looks: n % 2 == 0 tests whether n is even, and modulo is how you wrap around clock faces, grids and buffers.
Comparison operators
Comparison operators ask a question about two values and always produce a boolean (true or false): == (equal), != (not equal), <, >, <=, >=. The single most common beginner bug is writing = (assignment) where you meant == (comparison).
age = 20
age == 18 # False (is it equal to 18?)
age >= 18 # True (18 or more?)
age != 18 # True (not equal to 18?)
Logical operators
Logical operators combine boolean values so you can express compound conditions: AND is true only when both sides are true; OR is true when at least one side is true; NOT flips a value. The full behaviour is captured in a truth table:
| A | B | A AND B | A OR B | NOT A |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
age = 25
has_ticket = True
age >= 18 and has_ticket # True — both conditions hold
Assignment operators
Besides plain =, most languages offer compound assignment that updates a variable in place: x += 3 means x = x + 3. The family includes -=, *=, /= and more. They are shorthand, not new powers.
Operator precedence
When an expression mixes operators, the computer follows precedence rules — essentially the maths "BODMAS/PEMDAS" order. Multiplication and division happen before addition and subtraction, and comparisons happen before logical operators. When in doubt, add parentheses: they are free, and they make intent unmistakable.
2 + 3 * 4 # 14, not 20 — * runs first
(2 + 3) * 4 # 20 — parentheses force the addition first
age >= 18 and age < 65 # comparisons evaluate before 'and'
Key idea: operators turn values into new values; an expression is any combination that evaluates to a single result, governed by precedence.