Learn Python in 30 Days — Day 4: Operators & Expressions

Learn Python in 30 Days — Day 4: Operators & Expressions

Welcome to Day 4 of the Learn Python in 30 Days series!
All example files for this series are available on my GitHub: Learn-Python-in-30-Days

Yesterday, you learned how to get input from users and perform simple math.
Today, we’re diving deeper into operators the symbols and keywords that let you combine values and make decisions in your code.

By the end of this lesson, you’ll understand:

  • How to perform arithmetic using operators

  • How to compare values

  • How to combine conditions using logical operators (and, or, not)

Step 1 – Arithmetic Operators (numbers in, number out)

Arithmetic operators perform maths. Most return a number (int or float).

a = 10 b = 3 print(a + b) # Addition → 13 print(a - b) # Subtraction → 7 print(a * b) # Multiplication → 30 print(a / b) # True division → 3.3333333333333335 (a float) print(a // b) # Floor division → 3 (drops the fractional part) print(a % b) # Modulus → 1 (the remainder of 10 ÷ 3) print(a ** b) # Exponentiation → 1000 (10 to the power of 3)


Try this for yourself and get the following result
You can also download this example from my GitHub here and run it yourself.

Key details

  • / always does real (float) division in Python 3, even for two integers.

  • // is floor division: it rounds down to the nearest integer (towards negative infinity).

    • 7 // 3 == 2, but -7 // 3 == -3 (because it floors down).

  • % (modulus) pairs with //: a == (a // b) * b + (a % b). Great for even/odd checks: n % 2 == 0.

  • ** is exponent: 2 ** 10 is 1024.

  • Order of operations (highest to lowest, simplified):

    1. **

    2. unary + and - (e.g., -x)

    3. *, /, //, %

    4. +, -
      Use parentheses to make intent clear: 2 + 3 * 4 is 14, but (2 + 3) * 4 is 20.

Augmented assignment (handy shorthand):

x = 5 x += 2 # same as x = x + 2 → 7 x *= 3 # → 21 x -= 1 # → 20

Step 2 – Comparison Operators (questions that answer True/False)

These test relationships between values and return a Boolean: True or False.

x = 5 y = 8 print(x == y) # Equal to → False print(x != y) # Not equal to → True print(x > y) # Greater than → False print(x < y) # Less than → True print(x >= 5) # Greater or equal → True print(y <= 8) # Less or equal → True

Try this for yourself and get the following result
You can also download this example from my GitHub here and run it yourself.

Key details

  • = assigns; == compares. Mixing them up is the #1 beginner error.

  • Comparisons work on many types (numbers, strings, etc.).

    • String comparisons are case-sensitive and lexicographical: "apple" < "Banana" is False because uppercase letters come before lowercase in Unicode. A common fix is to compare lowercased versions: s1.lower() == s2.lower().

  • Chained comparisons read like maths and are more accurate:

    score = 17 print(10 < score < 20) # True (instead of 10 < score and score < 20)
  • Comparisons return booleans, which you can store:

    score_in_range = 13 <= score <= 19

Step 3 – Logical Operators (combine conditions)

Logical operators work with booleans to build more expressive rules.

age = 20 has_ticket = True print(age >= 18 and has_ticket) # True if both are True print(age < 18 or has_ticket) # True if either is True print(not has_ticket) # Flips True ↔ False

Try this for yourself and get the following result
You can also download this example from my GitHub
here and run it yourself.

Short-circuiting (important behaviour)

  • and stops early if the left side is False (because the whole thing can’t be True).

  • or stops early if the left side is True (because the whole thing is already True).
    This matters if the right-hand side is expensive (e.g., a function call) or has side effects.

Readability tips

  • Prefer positive conditions when possible:

    # Harder to read if not (age < 18 or not has_ticket): # Easier to read if age >= 18 and has_ticket:
  • Group with parentheses to make intent crystal clear, even if you know the precedence (not > and > or).

Step 4 – Putting It All Together

Let’s build a realistic gatekeeper that shows how arithmetic (via comparisons) and logical ops work together:

age = int(input("Enter your age: ")) has_ticket = input("Do you have a ticket (yes/no)? ").strip().lower() == "yes" is_vip = input("Are you on the VIP list (yes/no)? ").strip().lower() == "yes" # Rule: You can enter if you're 18+ AND have a ticket. # VIPs may enter with a ticket even if under 18 (house rule example). if (age >= 18 and has_ticket) or (is_vip and has_ticket): print("Welcome to the concert!") elif has_ticket and age < 18 and not is_vip: print("Sorry, you must be 18 or older (VIPs excepted).") elif not has_ticket: print("You need a valid ticket to enter.") else: print("Sorry, entry requirements not met.")

Try this for yourself and get the following result
You can also download this example from my GitHub here and run it yourself.

What you’ve used here

  • Comparisons: age >= 18

  • Logical combos: (A and B) or (C and B)

  • not to invert a condition

  • Input normalisation with .strip().lower() so users can type “Yes”, “ yes ”, etc.

Step 5 – Extra Patterns You’ll Use All the Time

Even / odd check (modulus)

n = int(input("Number: ")) print("Even" if n % 2 == 0 else "Odd")

Range checks (chained comparisons)

temp = 22 if 18 <= temp <= 24: print("Comfy room temperature")

Safe division (avoid divide-by-zero)

a, b = 10, 0 if b != 0: print(a / b) else: print("Cannot divide by zero.")

Common Mistakes to Avoid

  • = vs ==: = assigns, == compares.

  • Forgetting float behaviour: / returns a float; use // for floor division.

  • Confusing precedence: add parentheses for clarity when mixing and, or, not.

  • Case-sensitive string comparisons: normalise with .lower() before comparing user input.

  • Negation traps: not a or b is not the same as not (a or b). Parentheses matter.

Challenge: “Expression Tester”

Write a short program that:

  1. Asks the user for two numbers.

  2. Prints out the results of different expressions using arithmetic, comparison, and logical operators.

You can also download the code for this challenge from my GitHub here and run it yourself.

Next up: Day 5 – Conditionals

Tomorrow (Day 5) we’ll explore if, elif, and else the building blocks of decision making in Python.

All example files for this series are available on my GitHub: Learn-Python-in-30-Days

Comments

Popular posts from this blog

Math Behind Logic Gates

6502 - Part 2 Reset and Clock Circuit

Building a 6502 NOP Test