1.2.3 - Operators in algorithms

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:

FamilyWhat it is needed forPython-style operatorsKind of result
ArithmeticPerforming calculations+, -, *, /, //, %, **A number
RelationalComparing two values==, !=, <, >, <=, >=True or False
LogicalCombining or reversing conditionsand, or, notTrue 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.

OperatorNameExampleResult
+Addition8 + 311
-Subtraction8 - 35
*Multiplication8 * 324
/Division17 / 53.4
//Integer division17 // 53
%Modulus (remainder)17 % 52
**Exponentiation (power)2 ** 38

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:

  1. Parentheses: ()
  2. Exponentiation: **
  3. Multiplication and division: *, /, //, %
  4. 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.

  1. Exponentiation first: 2 ** 3 = 8
  2. Multiplication next: 8 * 2 = 16
  3. 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.

OperatorMeaningExample that is True
==Equal to7 == 7
!=Not equal to7 != 5
<Less than5 < 7
>Greater than7 > 5
<=Less than or equal to7 <= 7
>=Greater than or equal to7 >= 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:

Python
score = 70       # store 70 in score
score == 70      # compare score with 70: True

For the stored value score = 70:

ExpressionComparisonBoolean result
score == 70Is 70 equal to 70?True
score < 70Is 70 less than 70?False
score >= 60Is 70 at least 60?True
score != 70Is 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.

OperatorMeaning
andTrue only when both connected conditions are True
orTrue when at least one connected condition is True, including when both are True
notReverses 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:

Python
age = 15
has_ticket = True
is_banned = False

Follow each condition in small steps:

  • age >= 13 and has_ticket becomes True and True, so it is True.
  • age < 13 or has_ticket becomes False or True, so it is True.
  • not is_banned becomes not False, so it is True.

A complete entry condition could be:

Python
(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.

Plain text
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:

LineSubstitution and operationStored result
329 // 6complete_teams = 4
429 % 6waiting = 5
55 > 0extra_helper_needed = True
64 >= 4 and not True becomes True and Falseevent_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 phraseUseful operator idea
complete groupsinteger division //
amount left overmodulus %
at least>=
no items left== 0
both requirements must holdand