Day 3 45 min beginner

Operators and Expressions

Master arithmetic, comparison, and logical operators

Learning Objectives

  • Use arithmetic operators for calculations
  • Compare values with comparison operators
  • Combine conditions with logical operators
  • Understand operator precedence

What are Operators?

Operators are special symbols that perform operations on values (operands). Python supports several types of operators:

flowchart TB A[Python Operators] --> B[Arithmetic] A --> C[Comparison] A --> D[Logical] A --> E[Assignment] B --> B1[+ - * / // % **] C --> C1[== != < > <= >=] D --> D1[and or not] E --> E1[= += -= *= /=]

Arithmetic Operators

Use these for mathematical calculations:

Operator Name Example Result
+ Addition 5 + 3 8
- Subtraction 10 - 4 6
* Multiplication 6 * 7 42
/ Division 15 / 4 3.75
// Floor Division 15 // 4 3
% Modulus (Remainder) 17 % 5 2
** Exponentiation 2 ** 3 8

Examples in Action

arithmetic.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Basic arithmetic
a = 10
b = 3

print(f"Addition: {a} + {b} = {a + b}")         # 13
print(f"Subtraction: {a} - {b} = {a - b}")      # 7
print(f"Multiplication: {a} * {b} = {a * b}")   # 30
print(f"Division: {a} / {b} = {a / b}")         # 3.333...
print(f"Floor Division: {a} // {b} = {a // b}") # 3
print(f"Modulus: {a} % {b} = {a % b}")          # 1
print(f"Exponent: {a} ** {b} = {a ** b}")       # 1000

Division Types Explained

flowchart LR A["15 / 4"] --> B["3.75"] C["15 // 4"] --> D["3"] E["15 % 4"] --> F["3"] A -.- G["Regular division\n(always float)"] C -.- H["Floor division\n(integer part)"] E -.- I["Modulus\n(remainder)"]
division.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Division returns a float
result = 10 / 3
print(result)       # 3.333...
print(type(result)) # <class 'float'>

# Floor division returns an integer (rounded down)
floor_result = 10 // 3
print(floor_result) # 3

# Modulus returns the remainder
remainder = 10 % 3
print(remainder)    # 1 (because 10 = 3*3 + 1)
Practical Uses
Floor division is useful for integer calculations like dividing items into groups. Modulus helps check if a number is even/odd (n % 2 == 0 means even) or for wrapping values.

Comparison Operators

These compare values and return True or False:

Operator Name Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 7 > 3 True
< Less than 2 < 8 True
>= Greater than or equal 5 >= 5 True
<= Less than or equal 4 <= 3 False
Common Mistake

Don’t confuse = (assignment) with == (comparison)!

  • x = 5 assigns the value 5 to x
  • x == 5 checks if x equals 5

Examples

comparison.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
age = 25
min_age = 18
max_age = 65

# Comparisons
print(age == 25)      # True
print(age != 30)      # True
print(age > min_age)  # True
print(age < max_age)  # True
print(age >= 25)      # True
print(age <= 24)      # False

# Comparing strings (alphabetical order)
print("apple" < "banana")  # True
print("A" < "a")           # True (uppercase comes first)

Chained Comparisons

Python allows elegant chained comparisons:

chained.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
age = 25

# Instead of: age >= 18 and age <= 65
# You can write:
print(18 <= age <= 65)  # True

# This also works
x = 5
print(1 < x < 10)       # True
print(1 < x < 3)        # False

Logical Operators

Combine multiple conditions:

Operator Description Example Result
and Both must be True True and False False
or At least one True True or False True
not Inverts the value not True False

Truth Tables

flowchart TB subgraph AND["and"] A1["True and True"] --> B1["True"] A2["True and False"] --> B2["False"] A3["False and True"] --> B3["False"] A4["False and False"] --> B4["False"] end subgraph OR["or"] C1["True or True"] --> D1["True"] C2["True or False"] --> D2["True"] C3["False or True"] --> D3["True"] C4["False or False"] --> D4["False"] end

Practical Examples

logical.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
age = 25
has_license = True
is_insured = False

# AND: Both conditions must be true
can_drive = age >= 18 and has_license
print(f"Can drive: {can_drive}")  # True

# OR: At least one condition must be true
can_rent_car = age >= 25 or has_license
print(f"Can rent car: {can_rent_car}")  # True

# NOT: Inverts the condition
needs_insurance = not is_insured
print(f"Needs insurance: {needs_insurance}")  # True

# Complex conditions
can_work = age >= 18 and (has_license or is_insured)
print(f"Can work: {can_work}")  # True
Short-Circuit Evaluation
Python is smart! With and, if the first condition is False, it doesn’t check the second. With or, if the first is True, it doesn’t check the second.

Assignment Operators

Shorthand for updating variables:

Operator Example Equivalent To
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 2 x = x - 2
*= x *= 4 x = x * 4
/= x /= 2 x = x / 2
//= x //= 3 x = x // 3
%= x %= 2 x = x % 2
**= x **= 2 x = x ** 2
assignment.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
score = 0

# Increment
score += 10
print(score)  # 10

# Multiply
score *= 2
print(score)  # 20

# Subtract
score -= 5
print(score)  # 15

# This is cleaner than:
# score = score + 10
# score = score * 2
# score = score - 5

Operator Precedence

When multiple operators are in one expression, Python follows this order:

flowchart TB A["Highest Priority"] --> B["() Parentheses"] B --> C["** Exponentiation"] C --> D["* / // % Multiplication/Division"] D --> E["+ - Addition/Subtraction"] E --> F["< > <= >= == != Comparisons"] F --> G["not Logical NOT"] G --> H["and Logical AND"] H --> I["or Logical OR"] I --> J["Lowest Priority"]

Examples

precedence.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Without parentheses
result = 2 + 3 * 4
print(result)  # 14 (not 20!)
# Python does: 2 + (3 * 4) = 2 + 12 = 14

# With parentheses to change order
result = (2 + 3) * 4
print(result)  # 20

# Complex expression
x = 10
y = 5
z = 2

result = x + y * z ** 2  # x + (y * (z ** 2))
print(result)  # 10 + (5 * 4) = 10 + 20 = 30

# Boolean precedence
a = True
b = False
c = True

result = a or b and c  # a or (b and c)
print(result)  # True (because 'and' before 'or')
When in Doubt, Use Parentheses!
Parentheses make your code clearer and ensure the order you want. (a + b) * c is easier to understand than relying on precedence rules.

Practical Example: Calculator

Let’s build a simple calculator:

calculator.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Simple calculator
num1 = 15
num2 = 4

print("Calculator Results")
print("=" * 30)
print(f"{num1} + {num2} = {num1 + num2}")
print(f"{num1} - {num2} = {num1 - num2}")
print(f"{num1} * {num2} = {num1 * num2}")
print(f"{num1} / {num2} = {num1 / num2:.2f}")
print(f"{num1} // {num2} = {num1 // num2}")
print(f"{num1} % {num2} = {num1 % num2}")
print(f"{num1} ** {num2} = {num1 ** num2}")

Summary

Today you learned:

  • Arithmetic operators: +, -, *, /, //, %, **
  • Comparison operators: ==, !=, >, <, >=, <=
  • Logical operators: and, or, not
  • Assignment operators: =, +=, -=, *=, /=
  • Operator precedence: Parentheses first, then exponents, then multiplication/division, then addition/subtraction

Practice Exercise

Calculate and print:

  1. The area of a rectangle (width=5, height=3)
  2. Whether a number is even (hint: use modulus)
  3. Whether a person can vote (age >= 18) AND is a citizen
  4. The result of 2 + 3 * 4 - 1 and explain why

Next Steps

Tomorrow, we’ll learn about control flow - how to make decisions in your code with if, elif, and else statements.

Complete the quiz below to unlock Day 4!

Quiz

Complete this quiz with a minimum score of 80% to mark Day 3 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!