Skip to content

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):

OperatorMeaningExampleResult
== or =Equal to5 == 5True
!= or Not equal to5 != 3True
<Less than3 < 7True
>Greater than9 > 12False
<= or Less than or equal5 <= 5True
>= or Greater than or equal4 >= 6False

Boolean operators

Used to combine multiple conditions:

OperatorMeaningExampleResult
ANDBoth conditions must be trueage >= 18 AND hasID = TrueTrue only if both
ORAt least one must be trueday = "Sat" OR day = "Sun"True if either
NOTInverts the resultNOT isLoggedInTrue if not logged in

Truth tables

AND:

ABA AND B
TTT
TFF
FTF
FFF

OR:

ABA OR B
TTT
TFT
FTT
FFF

NOT:

ANOT A
TF
FT

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 // 365

Test Yourself

Question 1 of 5

What is the result of 17 MOD 5?

Ad

PassMaven - revision made simple.