Operators & Control Flow
⚙️ Operators & Control Flow
if/elif/else — plus a genuinely modern addition, the walrus operator.
➕ Arithmetic Operators
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
== 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
Logical Operators: Symbols vs Words
| Go / Kotlin | Python | |
|---|---|---|
| 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
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.
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:
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.
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.
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.
🎯 What's Next
Next chapter: Loops — for and while, range(), break/continue/the loop else clause, and enumerate().