1.2.3 - Operators in algorithms
Operators are the small symbols and words that make an algorithm calculate, compare and decide. By the end of this lesson, you will be able to follow their effect precisely and write arithmetic, relational and logical expressions that match a requirement.
The three operator families
An algorithm needs more than stored values. It needs ways to process those values and form conditions. An expression is a value, or a combination of values and operators, that can be evaluated to produce a result.
Operator
An operator is a symbol or keyword that tells an algorithm to perform an operation on one or more values.
The three required families have different jobs:
| Family | What it is needed for | Python-style operators | Kind of result |
|---|---|---|---|
| Arithmetic | Performing calculations | +, -, *, /, //, %, ** | A number |
| Relational | Comparing two values | ==, !=, <, >, <=, >= | True or False |
| Logical | Combining or reversing conditions | and, or, not | True or False |
For example, price * quantity calculates a value, age >= 13 compares two values, and age >= 13 and has_ticket combines conditions. In pseudocode, the logical operators may be written as AND, OR and NOT; Python uses the lowercase keywords shown above.
Arithmetic and precedence
Arithmetic operators calculate new numerical values. Each operator answers a different kind of numerical question.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 8 + 3 | 11 |
- | Subtraction | 8 - 3 | 5 |
* | Multiplication | 8 * 3 | 24 |
/ | Division | 17 / 5 | 3.4 |
// | Integer division | 17 // 5 | 3 |
% | Modulus (remainder) | 17 % 5 | 2 |
** | Exponentiation (power) | 2 ** 3 | 8 |
The three division-related operators are easy to confuse. If 17 items are placed into groups of 5, / gives the exact quotient 3.4, // gives the number of complete groups 3, and % gives the remainder 2. For positive whole numbers, these results fit together as:
17 = (17 // 5) * 5 + (17 % 5) = 3 * 5 + 2
Here % means remainder, not percentage. Python's // operation is floor division: it rounds the quotient down to the nearest whole number. With the positive whole-number quantities used here, that is the number of complete groups.
When an expression contains several operators, precedence controls which operation is performed first:
- Parentheses:
() - Exponentiation:
** - Multiplication and division:
*,/,//,% - Addition and subtraction:
+,-
This is BIDMAS in algorithm notation: brackets, indices (**), division and multiplication, then addition and subtraction. Integer division and modulus share the same precedence as division and multiplication.
Operators at the same level are evaluated from left to right, except repeated exponentiation, which Python groups from right to left. Parentheses can always make the intended grouping explicit.
Worked example
Evaluate 3 + 2 ** 3 * 2.
- Exponentiation first:
2 ** 3 = 8 - Multiplication next:
8 * 2 = 16 - Addition last:
3 + 16 = 19
The result is 19, not 40: simply working from left to right would ignore precedence.
Comparing values
Relational operator
A relational operator compares two values and produces the Boolean result True or False.
| Operator | Meaning | Example that is True |
|---|---|---|
== | Equal to | 7 == 7 |
!= | Not equal to | 7 != 5 |
< | Less than | 5 < 7 |
> | Greater than | 7 > 5 |
<= | Less than or equal to | 7 <= 7 |
>= | Greater than or equal to | 7 >= 7 |
The word or equal to includes the boundary value. For example, score >= 60 is True when score is exactly 60 as well as when it is above 60.
In Python-style algorithms, = assigns a value, while == compares two values:
score = 70 # store 70 in score
score == 70 # compare score with 70: True
For the stored value score = 70:
| Expression | Comparison | Boolean result |
|---|---|---|
score == 70 | Is 70 equal to 70? | True |
score < 70 | Is 70 less than 70? | False |
score >= 60 | Is 70 at least 60? | True |
score != 70 | Is 70 different from 70? | False |
The result is a Boolean, not the numerical difference between the two values. Also write >= and <= in that order; => and =< are not the required Python operators.
Combining conditions
Logical operators work with conditions that are already True or False. They are needed when one comparison is not enough to express a requirement.
| Operator | Meaning |
|---|---|
and | True only when both connected conditions are True |
or | True when at least one connected condition is True, including when both are True |
not | Reverses a Boolean result: True becomes False, and False becomes True |
The or operator is inclusive here. It does not mean "one or the other, but not both".
Worked example
An entry algorithm has these stored values:
age = 15
has_ticket = True
is_banned = False
Follow each condition in small steps:
age >= 13 and has_ticketbecomesTrue and True, so it isTrue.age < 13 or has_ticketbecomesFalse or True, so it isTrue.not is_bannedbecomesnot False, so it isTrue.
A complete entry condition could be:
(age >= 13 and has_ticket) and not is_banned
This is True only if the age comparison and ticket condition are both true, while the banned condition is false. In an expression without parentheses, comparisons are evaluated before not, then and, then or. Parentheses are still useful because they make the intended grouping clear, especially around the condition affected by not.
Following and writing expressions
To follow an algorithm that uses operators, substitute the current values into each expression, apply precedence, and carry the result forward to any later expression. To write one, start from the requirement: decide whether it asks for a calculation, a comparison, or a combination of conditions, then choose the operator family that performs that job.
Worked algorithm
The following written algorithm organises 29 participants into teams of 6. The input values are explicitly stored on lines 1 and 2. The numbers at the left are line labels, not part of the Python statements.
1 | participants = 29
2 | team_size = 6
3 | complete_teams = participants // team_size
4 | waiting = participants % team_size
5 | extra_helper_needed = waiting > 0
6 | event_ready = complete_teams >= 4 and not extra_helper_needed
Follow the expressions in line order:
| Line | Substitution and operation | Stored result |
|---|---|---|
| 3 | 29 // 6 | complete_teams = 4 |
| 4 | 29 % 6 | waiting = 5 |
| 5 | 5 > 0 | extra_helper_needed = True |
| 6 | 4 >= 4 and not True becomes True and False | event_ready = False |
The operator families work together: arithmetic produces the quantities, relational operators turn comparisons into Boolean results, and logical operators combine or reverse those results. Notice that the algorithm uses // for complete teams and % for the participants left over; / would answer a different question.
When writing an expression, translate requirement phrases carefully:
| Requirement phrase | Useful operator idea |
|---|---|
| complete groups | integer division // |
| amount left over | modulus % |
| at least | >= |
| no items left | == 0 |
| both requirements must hold | and |