Operators & Control Flow

Python Fundamentals — Operators & Control Flow
Python Fundamentals
Course 1 · Chapter 3 · Operators & Control Flow

⚙️ Operators & Control Flow

Chapter 2 covered Python's basic types. This chapter covers combining and comparing values with operators, and branching logic with if/elif/else — plus a genuinely modern addition, the walrus operator.

➕ Arithmetic Operators

7 + 3 # 10 7 - 3 # 4 7 * 3 # 21 7 / 3 # 2.3333333333333335 — always a float (Chapter 2) 7 // 3 # 2 — floor division, discards the remainder 7 % 3 # 1 — modulo, the remainder 7 ** 2 # 49 — exponentiation, built directly into the operator

Exponentiation: Go/Kotlin vs Python

Neither Go nor Kotlin has an exponent operator at all — both require a function call (math.Pow(7, 2) in Go, Math.pow(7.0, 2.0) in Kotlin). Python's ** is a genuine convenience built directly into the language's operator set.

⚖️ Comparison Operators

5 == 5 # True 5 != 3 # True 5 > 3 # True 5 <= 3 # False

== compares by value"abc" == "abc" is True, even though they might be two separately-created strings. A separate operator, is, checks whether two names point at the exact same object in memory — a distinction that matters more once mutable objects like lists arrive in Chapter 6.

🔗 Logical Operators

True and False # False True or False # True not True # False

Logical Operators: Symbols vs Words

Go / KotlinPython
AND&&and
OR||or
NOT!not

Python spells logical operators out as words instead of symbols — a small but real readability choice consistent with the language's overall design philosophy.

🔀 if / elif / else

age = 20 if age < 13: print("child") elif age < 20: print("teenager") else: print("adult") # adult

No braces at all — the colon : starts a block, and consistent indentation defines what's inside it, exactly the "significant whitespace" Chapter 1 mentioned. elif is Python's spelling of "else if," used in every conditional chain instead of Go/Kotlin's else if.

⚠ Indentation Errors Are a Real, Common Beginner Trap

Mixing tabs and spaces, or indenting one line of a block slightly differently than its siblings, produces an IndentationError — Python is strict about this since indentation isn't just style, it's the actual block structure. Most editors (including IDLE) convert Tab to 4 spaces automatically to avoid this; stick to spaces consistently.

🐘 The Walrus Operator (:=)

Added in Python 3.8, the walrus operator assigns a value and uses it in the same expression — useful for avoiding a repeated function call:

data = [1, 2, 3, 4, 5] if (n := len(data)) > 3: print(f"list is too long: {n} items") # list is too long: 5 items

Without the walrus operator, this would need len(data) called twice — once inside the condition, once again inside the print — or a separate line assigning n before the if. := does both in one expression.

Arithmetic

+ - * / // % ** — including a built-in exponent operator, unlike Go or Kotlin.

Comparison

== != < > <= >= — value equality by default; is checks identity.

Logical

and / or / not — spelled as words, not symbols.

Walrus (:=)

Assign and use a value in one expression — genuinely new syntax, added in 3.8.

💻 Coding Challenges

Challenge 1: FizzBuzz-Style Check

Write an if/elif/else chain that checks a number: if it's divisible by both 3 and 5, print "FizzBuzz"; if only by 3, print "Fizz"; if only by 5, print "Buzz"; otherwise print the number itself.

Goal: Combine comparison operators, the modulo operator, and logical and in one realistic conditional chain.

→ Solution

Challenge 2: Floor Division vs Modulo

Given a total number of minutes, write code using // and % to compute and print the equivalent hours and remaining minutes (e.g. 125 minutes → 2 hours, 5 minutes).

Goal: Practice using floor division and modulo together for a genuinely practical calculation.

→ Solution

Challenge 3: Walrus in a Loop-Free Context

Given a string, use the walrus operator inside an if condition to check whether its length exceeds 10, printing the actual length only if the condition is true — without calling len() twice.

Goal: Get hands-on with := in the simplest realistic case before it appears again in later chapters.

→ Solution

🎯 What's Next

Next chapter: Loopsfor and while, range(), break/continue/the loop else clause, and enumerate().