Appearance
C2 · Operators
Spec reference: Section C - Programming Paradigms
Key idea: Use arithmetic, relational and Boolean operators to manipulate and compare data.
Relational operators
Used to compare two values. The result is always a Boolean (True or False):
| Operator | Meaning | Example | Result |
|---|---|---|---|
== or = | Equal to | 5 == 5 | True |
!= or ≠ | Not equal to | 5 != 3 | True |
< | Less than | 3 < 7 | True |
> | Greater than | 9 > 12 | False |
<= or ≤ | Less than or equal | 5 <= 5 | True |
>= or ≥ | Greater than or equal | 4 >= 6 | False |
Boolean operators
Used to combine multiple conditions:
| Operator | Meaning | Example | Result |
|---|---|---|---|
AND | Both conditions must be true | age >= 18 AND hasID = True | True only if both |
OR | At least one must be true | day = "Sat" OR day = "Sun" | True if either |
NOT | Inverts the result | NOT isLoggedIn | True if not logged in |
Truth tables
AND:
| A | B | A AND B |
|---|---|---|
| T | T | T |
| T | F | F |
| F | T | F |
| F | F | F |
OR:
| A | B | A OR B |
|---|---|---|
| T | T | T |
| T | F | T |
| F | T | T |
| F | F | F |
NOT:
| A | NOT A |
|---|---|
| T | F |
| F | T |
Date and time operations
Programs often need to work with dates:
python
import datetime
today = datetime.date.today()
print(today) # e.g. 2025-03-13
# Calculate age
born = datetime.date(2006, 5, 14)
age_days = (today - born).days
age_years = age_days // 365Test Yourself
Question 1 of 5
What is the result of 17 MOD 5?