Home · Academy · Robotics & Coding · Algorithms · Operators and Comparisons

Operators and Comparisons

Learn to calculate and combine conditions with arithmetic, comparison and logical operators.

LESSON COMPASS

What will you use this page for?

Core idea

Operators are small but powerful symbols that let us do arithmetic, compare values and combine conditions in our programs.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Confusing = with == = assigns, while == compares. Writing if number = 5 causes an error; the correct form is if number == 5 . This is the most common mistake beginners make. Confusing integer division with normal division 7 / 2 gives 3.5 , while 7 // 2 gives 3 . If you expect a whole number but use / , you will get a…

Next connection

Input, Processing and Output: You will write your first complete program that takes information from the user, processes it, and produces a meaningful result.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration25–35 min
PrerequisiteVariables
ContentStandard lesson · 1,322 words
Last updated

One-sentence summary

Operators are small but powerful symbols that let us do arithmetic, compare values and combine conditions in our programs.

Why does it matter?

In the previous lesson we saw that a variable stores information in a box. But storing information is not enough on its own; we need to do something with it. We may want to average two grades, decide whether a number is even, or express a rule such as "if it is cold and rainy."

This is exactly where operators come in. Operators let a program calculate and make decisions. When I started coding, learning operators was the moment programs began to feel alive to me: variables were no longer fixed boxes but values that could talk to each other.

Arithmetic operators

Arithmetic operators are very similar to the four operations you already know from maths. Only the symbols are a little different.

The basic symbols

The basic symbols table
OperationSymbolExampleResult
Addition+5 + 38
Subtraction-5 - 32
Multiplication*5 * 315
Division/6 / 41.5
Remainder (mod)%7 % 21

For multiplication we use * (a star), not x, because x is often the name of a variable. Division with / gives a decimal result: 6 / 4 is 1.5.

Remainder (mod) and the idea of integer division

The % symbol gives the remainder of a division. For example, 7 % 2 asks "when I divide 7 by 2, what is left over?" The answer is 1.

Sometimes, though, we want a whole result rather than a fraction. Imagine sharing 7 sweets between 2 children and asking how many whole sweets each child gets. This is called integer division, written with // in Python:

sweets = 7
children = 2

each_child = sweets // children   # whole part: 3
left_over = sweets % children     # remainder: 1

print(each_child)  # 3
print(left_over)   # 1

So each child gets 3 sweets, and 1 sweet is left over.

Everyday example: the average of two grades

Suppose you scored 80 and 90 on two exams. Your average is:

grade1 = 80
grade2 = 90

average = (grade1 + grade2) / 2
print(average)  # 85.0

Notice the parentheses here; we will soon see why they are needed.

Comparison operators

Comparison operators compare two values and produce either True or False as a result.

The six basic comparisons

The six basic comparisons table
MeaningSymbolExampleResult
Is equal to?==5 == 5True
Is not equal to?!=5 != 3True
Is less than?<3 < 5True
Is greater than?>3 > 5False
Is less than or equal?<=5 <= 5True
Is greater than or equal?>=6 >= 7False

The most important point here is this: to check equality we use a double ==, not a single =. A single = is for assigning a value to a variable.

Everyday example: even or odd?

A number is even when it divides exactly by 2, meaning its remainder is 0. We can write this by using a comparison and the mod operator together:

Start
Receive a number
If number % 2 equals 0
  display "Even"
Otherwise
  display "Odd"
End

In Python:

number = 14

if number % 2 == 0:
    print("Even")
else:
    print("Odd")

Here number % 2 calculates the remainder first, then == 0 compares that remainder with zero.

Logical connectives and precedence

Sometimes one condition is not enough; we want to combine several. For this we use logical connectives: AND, OR and NOT.

AND, OR, NOT

The easiest way to see this is with a truth table. In the table T = True and F = False:

AND, OR, NOT table
ABA AND BA OR B
TTTT
TFFT
FTFT
FFFF

Everyday example: combining conditions

Suppose you are deciding whether to take an umbrella. The rule is: take an umbrella if it is rainy AND the wind is NOT very strong.

rainy = True
strong_wind = False

if rainy and not strong_wind:
    print("Take an umbrella")
else:
    print("Do not take an umbrella")

If the wind is very strong the umbrella turns inside out, so we use not to leave that case out.

Operator precedence

Just as in maths, operations in code have a precedence. Multiplication and division happen before addition and subtraction. Parentheses come before everything.

print(2 + 3 * 4)      # 14, because 3*4 happens first
print((2 + 3) * 4)    # 20, because the parentheses run first

Comparisons run after arithmetic, and logical connectives run last. So in number % 2 == 0, the % runs first, then the ==. When you are unsure, adding parentheses is always safe and makes the code easier to read.

Mini task

Think about code that decides whether a student passes the class. The rule: a student passes if their average is 50 or above AND their absence count is less than 20 days.

Write the following pseudocode in your own notebook, then translate it into Python:

Start
Receive two grades and calculate the average
Receive the absence count
If average >= 50 AND absences < 20
  display "Passed"
Otherwise
  display "Failed"
End

Hint: Remember to use parentheses when calculating the average, and to compare with >= rather than the assignment =. Try different grade and absence values to check that your code handles every case correctly.

Common mistakes

Confusing = with ==

= assigns, while == compares. Writing if number = 5 causes an error; the correct form is if number == 5. This is the most common mistake beginners make.

Confusing integer division with normal division

7 / 2 gives 3.5, while 7 // 2 gives 3. If you expect a whole number but use /, you will get a decimal value and be surprised.

Assuming the wrong precedence

Thinking that 2 + 3 * 4 equals 20 is a common error; it is actually 14, because multiplication happens first. Use parentheses to guarantee the order you want.

Choosing the wrong logical connective

"Cold AND rainy" produces very different results from "cold OR rainy." When combining conditions, think clearly about whether you want and or or.

Lesson summary

Check questions

  1. What is the result of 17 % 5?
  2. Which symbol do we use to assign a value to a variable, and which one to compare two values?
  3. What is the difference between the results of 7 / 2 and 7 // 2?
  4. When A = True and B = False, what are the results of A and B and A or B?
  5. What is the result of 2 + 3 * 4, and why?

Answers

  1. It is 2. Dividing 17 by 5 gives a quotient of 3 with a remainder of 2, and % returns the remainder.
  2. We use a single = to assign and a double == to compare.
  3. 7 / 2 gives 3.5 (decimal division), while 7 // 2 gives 3 (integer division, the fraction is dropped).
  4. A and B is False (both must be true, but B is false). A or B is True (at least one is true, and A is true).
  5. It is 14. Because multiplication happens before addition, 3 * 4 = 12 is calculated first, then 2 + 12 = 14.

Source and verification note

For “Operators and Comparisons”, verification focuses on whether the relationship between Arithmetic operators and Remainder (mod) and the idea of integer division remains consistent across examples. The algorithms in this lesson are checked by tracing sample inputs by hand and comparing them with expected outputs. Pseudocode is used to make the reasoning sequence visible without tying it to one programming language.

Next lesson

Input, Processing and Output: You will write your first complete program that takes information from the user, processes it, and produces a meaningful result.

Start QuizBack to Algorithms
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.