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
| Operation | Symbol | Example | Result |
|---|---|---|---|
| Addition | + | 5 + 3 | 8 |
| Subtraction | - | 5 - 3 | 2 |
| Multiplication | * | 5 * 3 | 15 |
| Division | / | 6 / 4 | 1.5 |
| Remainder (mod) | % | 7 % 2 | 1 |
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
| Meaning | Symbol | Example | Result |
|---|---|---|---|
| Is equal to? | == | 5 == 5 | True |
| Is not equal to? | != | 5 != 3 | True |
| Is less than? | < | 3 < 5 | True |
| Is greater than? | > | 3 > 5 | False |
| Is less than or equal? | <= | 5 <= 5 | True |
| Is greater than or equal? | >= | 6 >= 7 | False |
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
- AND: The result is true only when both conditions are true.
- OR: The result is true when at least one condition is true.
- NOT: It flips the result of a condition to its opposite.
The easiest way to see this is with a truth table. In the table T = True and F = False:
| A | B | A AND B | A OR B |
|---|---|---|---|
| T | T | T | T |
| T | F | F | T |
| F | T | F | T |
| F | F | F | F |
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
- Arithmetic operators (
+ - * / %) let us calculate;//gives the whole part and%gives the remainder. - Comparison operators (
== != < > <= >=) compare two values and produceTrueorFalse. - Use
=to assign and==to check equality; confusing them is a frequent mistake. - Logical connectives AND, OR and NOT combine conditions; a truth table makes the results easy to see.
- In precedence, multiplication and division come first, while parentheses always run before everything.
Check questions
- What is the result of
17 % 5? - Which symbol do we use to assign a value to a variable, and which one to compare two values?
- What is the difference between the results of
7 / 2and7 // 2? - When
A = TrueandB = False, what are the results ofA and BandA or B? - What is the result of
2 + 3 * 4, and why?
Answers
- It is
2. Dividing 17 by 5 gives a quotient of 3 with a remainder of 2, and%returns the remainder. - We use a single
=to assign and a double==to compare. 7 / 2gives3.5(decimal division), while7 // 2gives3(integer division, the fraction is dropped).A and BisFalse(both must be true, but B is false).A or BisTrue(at least one is true, and A is true).- It is
14. Because multiplication happens before addition,3 * 4 = 12is calculated first, then2 + 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.