Skip to main content
Module 1 · Lesson 2 of 42/36 in the course~14 min
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.

CategoryOperatorsPython key detailExample
Arithmetic+, -, *, // always produces float8 / 2 == 4.0
Division & Remainder//, %, **// truncates towards -∞ (floor)-7 // 2 == -4, 2 ** 10 == 1024
Comparison==, !=, <, <=, >, >=Chainable (a < b < c)0 <= score <= 100
Logicand, or, notShort-circuit: Returns the decisive operand"" or "guest" == "guest"

Integer vs decimal arithmetic

Python
# 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.

Python
# 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
Python
# 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

Python Quiz#python.m1.l2.e4
Ready

What is the exact result of the expression 10 / 2 + 7 // 2?

Python
result = 10 / 2 + 7 // 2
Answer options
Python Quiz#python.m1.l2.e5
Ready

What does Python assign to the variable mode with and and or?

Python
custom_mode = ""
mode = custom_mode or "standard"
Answer options

Test yourself

Warm-Up Stage

Step 1 — Powers with **

Combine powers and sum using the ** operator.

Exercise#python.m1.l2.e1
Attempts: 0Loading…

Calculate the square of 9 plus the cube of 2 using **, assign the result to result and evaluate it.

Loading editor…
Show hint

9 ** 2 is equal to 81 and 2 ** 3 is equal to 8.

Solution available after 3 attempts

Practice Stage

Step 2 — Parity with the modulus operator %

Construct a deterministic boolean by checking the remainder of division by 2.

Exercise#python.m1.l2.e2
Attempts: 0Loading…

Starting from number = 17, assign is_even a boolean (True/False) that indicates whether number is even using %.

Loading editor…
Show hint

An integer is even when number % 2 == 0.

Solution available after 3 attempts

Final Challenge

Step 3 — Integer quotient, remainder and inequality

Combine //, % and != to test divisibility without conversions.

Exercise#python.m1.l2.e3
Attempts: 0Loading…

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.

Loading editor…
Show hint

Use 10 // 3, 10 % 3 and compare to != 0.

Solution available after 3 attempts