Module lessons (2/4)
Operators
Operators combine objects to produce new values. Although the basic arithmetic resembles other languages, Python introduces three crucial details: / always returns a float, ** is the power, and Boolean logic uses readable English words (and, or, not) with short-circuit behavior.
The mental model: operators that return values
Unlike languages like JavaScript (where == vs === creates coercion traps), in Python there is only one value equality operator (==), while is compares the identity of the object in memory.
| Category | Operators | Python key detail | Example |
|---|---|---|---|
| Arithmetic | +, -, *, / | / always produces float | 8 / 2 == 4.0 |
| Division & Remainder | //, %, ** | // truncates towards -∞ (floor) | -7 // 2 == -4, 2 ** 10 == 1024 |
| Comparison | ==, !=, <, <=, >, >= | Chainable (a < b < c) | 0 <= score <= 100 |
| Logic | and, or, not | Short-circuit: Returns the decisive operand | "" or "guest" == "guest" |
Integer vs decimal arithmetic
# Esempio 1: differenza netta tra /, //, % e **
raw_ratio = 7 / 2 # 3.5 (sempre float)
page_count = 7 // 2 # 3 (divisione intera verso il basso)
remainder = 7 % 2 # 1 (resto -> utile per pari/dispari o cicli)
kilobyte = 2 ** 10 # 1024 (NON usare ^ che è lo XOR bit-a-bit!)Chained and short-circuit comparisons
In Python you don't need to repeat the variable in range checks: you can chain comparisons just like in math. Furthermore, and and or stop as soon as they know the final outcome.
# Esempio 2: range check leggibile come in matematica
score = 84
is_passing = 60 <= score <= 100 # True (valuta score una volta sola)
# Esempio 3: fallback veloce con short-circuit `or`
raw_input_name = ""
display_name = raw_input_name or "Anonymous" # "Anonymous" perché "" è falsy# Esempio 4: precedenza — prima **, poi * / // %, poi + -, confronti, not, and, or
masked = (3 + 2) ** 2 * 2 # 50: le parentesi eliminano ogni dubbio
is_valid = not (score < 0) and (score % 2 == 0)Quick check
What is the exact result of the expression 10 / 2 + 7 // 2?
result = 10 / 2 + 7 // 2What does Python assign to the variable mode with and and or?
custom_mode = ""
mode = custom_mode or "standard"Test yourself
Step 1 — Powers with **
Combine powers and sum using the ** operator.
Calculate the square of 9 plus the cube of 2 using **, assign the result to result and evaluate it.
Show hint
9 ** 2 is equal to 81 and 2 ** 3 is equal to 8.
Solution available after 3 attempts
Step 2 — Parity with the modulus operator %
Construct a deterministic boolean by checking the remainder of division by 2.
Starting from number = 17, assign is_even a boolean (True/False) that indicates whether number is even using %.
Show hint
An integer is even when number % 2 == 0.
Solution available after 3 attempts
Step 3 — Integer quotient, remainder and inequality
Combine //, % and != to test divisibility without conversions.
Calculate how many times 3 is in 10 with // (quotient), the rest in remainder with %, and check in is_not_multiple if remainder != 0. Currency is_not_multiple.
Show hint
Use 10 // 3, 10 % 3 and compare to != 0.
Solution available after 3 attempts