Area 2.4

Operators

Master arithmetic, relational, and Boolean operators for data manipulation and comparison. Learn operator precedence rules to write correct and efficient expressions.

3
Operator Types
16
Total Operators
~1.5hrs
Study Time

Learning Objectives

  • Understand arithmetic operators for mathematical calculations
  • Apply relational operators for comparisons between values
  • Use Boolean operators for logical operations and decision making
  • Know operator precedence rules and use parentheses effectively
  • Combine different operator types in complex expressions

Operator Categories

Arithmetic Operators

Perform mathematical calculations on numeric values

+Addition
5 + 3 = 8
Adding numbers, concatenating strings
-Subtraction
10 - 4 = 6
Subtracting numbers, finding differences
*Multiplication
6 * 7 = 42
Multiplying numbers, repeating strings
/Division
15 / 3 = 5.0
Dividing numbers (always returns float)
//Floor Division
17 // 5 = 3
Integer division (no remainder)
%Modulus
17 % 5 = 2
Finding remainder after division
**Exponentiation
2 ** 3 = 8
Raising to power, square roots

Python Examples:

# Arithmetic operations
result = 10 + 5 * 2    # 20 (multiplication first)
area = length * width
remainder = 17 % 5     # 2
power = 2 ** 8         # 256
average = total / count

# String operations
greeting = "Hello" + " " + "World"    # Concatenation
repeated = "Hi! " * 3                 # "Hi! Hi! Hi! "

Relational Operators

Compare values and return True or False results

==Equal to
5 == 5 → True
Check if values are the same
!=Not equal to
5 != 3 → True
Check if values are different
<Less than
3 < 5 → True
Check if left value is smaller
>Greater than
7 > 4 → True
Check if left value is larger
<=Less than or equal
5 <= 5 → True
Check if left ≤ right
>=Greater than or equal
8 >= 6 → True
Check if left ≥ right

Python Examples:

# Relational comparisons
age = 18
is_adult = age >= 18        # True
is_child = age < 13         # False

password = "secret123"
is_correct = password == "secret123"  # True
is_wrong = password != "wrong"        # True

score = 85
passed = score >= 60        # True

Boolean Operators

Combine logical conditions using AND, OR, and NOT

andLogical AND
True and False → False
Both conditions must be true
orLogical OR
True or False → True
At least one condition must be true
notLogical NOT
not True → False
Reverses the boolean value

Truth Table:

ABA and BA or B
TrueTrueTrueTrue
TrueFalseFalseTrue
FalseTrueFalseTrue
FalseFalseFalseFalse

Python Examples:

# Boolean operations
age = 20
has_license = True

# AND - both must be true
can_drive = age >= 18 and has_license     # True

# OR - at least one must be true  
eligible = age >= 65 or income < 30000    # depends on income

# NOT - reverses boolean
is_minor = not (age >= 18)               # False

# Complex conditions
if (score >= 90 and attendance >= 0.8) or extra_credit:
    grade = "A"

Operator Precedence Rules

Remember: PEMDAS + Logical

Parentheses → Exponents → Multiplication/Division → Addition/Subtraction → Comparisons → NOT → AND → OR

1
Parentheses - highest priority
()(2 + 3) * 4 = 20
2
Exponentiation
**2 ** 3 * 4 = 32
3
Multiplication and Division
*, /, //, %2 + 3 * 4 = 14
4
Addition and Subtraction
+, -2 * 3 + 4 = 10
5
Relational comparisons
<, <=, >, >=, ==, !=2 + 3 < 10 → True
6
Boolean NOT
notnot 5 < 3 → True
7
Boolean AND
andTrue and 5 > 3 → True
8
Boolean OR - lowest priority
orFalse or 2 == 2 → True

Learning Activities

Operator Precedence Challenge

Calculation
30 minutes

Evaluate complex expressions following correct order of operations

Boolean Logic Workshop

Logic
40 minutes

Create complex conditional statements using AND, OR, and NOT operators

Expression Building Exercise

Translation
35 minutes

Translate word problems into mathematical and logical expressions