Python · Module 1 · Lesson 31 min read

Operators

Arithmetic operators

OperatorMeaning7 ? 2
+ - *add, subtract, multiply9, 5, 14
/division, always a float3.5
//floor division (rounds down)3
%remainder (modulo)1
**power49

⚠️ // rounds down, so -7 // 2 is -4, not -3.

Comparison operators → True / False

== != > < >= <=. You can chain them: 0 < x < 10.

Logical operators

and, or, not. They short-circuit: in a and b, b is never checked if a is false.

Assignment shortcuts

x += 1 means x = x + 1; the same works for -= *= /= //= %= **=.

Membership and identity

  • in / not in: "a" in "cat" → True
  • is checks if two names point to the same object. Use it for None: if x is None:

Precedence (highest first)

** → * / // % → + - → comparisons → not → and → or. Use brackets to make intent clear.

▶️ Runs in the app.

Example

Python
a, b = 7, 2
print(a / b)     # 3.5
print(a // b)    # 3
print(a % b)     # 1
print(a ** b)    # 49
print(-7 // 2)   # -4  (rounds down!)

x = 5
print(0 < x < 10)           # True (chained comparison)
print(a > b and b > 0)      # True
print("py" in "python")     # True

value = None
print(value is None)        # True

total = 10
total += 5
print(total)                # 15