← Home

Ch 3 — Operators

Ch 3 — Operators

Aryan’s RAM manager needs to crunch numbers constantly — converting bytes to gigabytes, calculating usage percentages, comparing thresholds, and deciding whether to fire an alert. All of that math and logic runs through Python’s operators. Getting operator precedence wrong by even one step can turn a warning into a silent miss.


Arithmetic Operators

total = 16_384   # MB
used  = 10_240   # MB

# Basic arithmetic
free       = total - used          # 6144 MB
used_gb    = used / 1024           # 10.0  (float division)
used_gb_i  = used // 1024          # 10    (floor division → int result)
remainder  = used % 1024           # 0     (modulus)
squared    = 2 ** 10               # 1024  (exponentiation)
doubled    = used * 2              # 20480

Floor Division vs True Division

>>> 10_240 / 1024      # True division — always float
10.0
>>> 10_240 // 1024     # Floor division — rounds toward -∞
10
>>> 10_500 // 1024     # 10  (floors 10.25)
10
>>> -10_500 // 1024    # -11 (floors -10.25 toward -∞, not toward 0)
-11

Modulus

Great for “every N seconds” polling logic:

tick = 37
if tick % 5 == 0:
    print("Polling RAM…")   # fires at 0, 5, 10, 15 …

Exponentiation

bytes_in_gb = 1024 ** 3   # 1_073_741_824

Assignment Operators

Shorthand for x = x op y:

used_mb = 6144
used_mb += 512    # used_mb = 6144 + 512  →  6656
used_mb -= 256    # 6400
used_mb *= 2      # 12800
used_mb //= 1024  # 12  (GB now)
used_mb **= 2     # 144
used_mb %= 100    # 44

Comparison Operators

Return True or False. The bread and butter of alert logic.

used_pct = 87.5
threshold = 80.0

used_pct > threshold    # True
used_pct >= 90          # False
used_pct == 87.5        # True
used_pct != 90          # True
used_pct < 90           # True
used_pct <= 87.5        # True

Chained comparisons work naturally in Python:

60 < used_pct < 90   # True — reads like math notation

Logical Operators

Combine boolean expressions.

swap_on = True
used_pct = 87.5

if used_pct > 80 and swap_on:
    print("High RAM + swap active — watch out")

if used_pct > 95 or swap_on:
    print("Something needs attention")

if not swap_on:
    print("Swap is disabled")

Short-circuit evaluation: and stops at the first False; or stops at the first True.


Identity Operators

is checks object identity (same object in memory), not equality.

snapshot = None
if snapshot is None:
    print("No snapshot yet")

# is vs ==
a = [1, 2, 3]
b = [1, 2, 3]
a == b   # True  (same value)
a is b   # False (different objects)

Use is / is not only for None, True, False.


Membership Operators

in and not in check containment.

high_usage_procs = ["chrome", "electron", "java"]
current = "chrome"

if current in high_usage_procs:
    print(f"{current} is a known RAM hog")

if "python" not in high_usage_procs:
    print("python is RAM-efficient here")

Operator Precedence

Higher rows evaluate first:

PriorityOperators
1 (highest)**
2+x, -x, ~x (unary)
3*, /, //, %
4+, -
5<<, >>
6&
7^
8|
9Comparisons (==, !=, <, >, <=, >=, is, in)
10not
11and
12 (lowest)or
# Without parentheses — might surprise you
result = 2 + 3 * 4      # 14  (multiplication first)

# Explicit parentheses — always clear
result = (2 + 3) * 4    # 20

# RAM percentage — needs correct order
pct = used / total * 100        # (used / total) * 100 — correct
pct = used / (total * 100)      # WRONG — divides by 1,638,400

Operator Relationships

flowchart LR
    A[Operators] --> B[Arithmetic]
    A --> C[Comparison]
    A --> D[Logical]
    A --> E[Assignment]
    A --> F[Identity & Membership]
    B --> G["+ - * / // % **"]
    C --> H["== != < > <= >="]
    D --> I["and or not"]
    E --> J["+= -= *= //= etc"]
    F --> K["is  is not  in  not in"]

Precedence Decision Flow

flowchart TD
    A[Expression] --> B{Contains **?}
    B -->|Yes| C[Evaluate ** first]
    B -->|No| D{Contains * / // %?}
    C --> D
    D -->|Yes| E[Evaluate mul/div next]
    D -->|No| F{Contains + -?}
    E --> F
    F -->|Yes| G[Evaluate add/sub]
    F -->|No| H[Evaluate comparisons]
    G --> H
    H --> I[Evaluate logical: not → and → or]
    I --> J[Final boolean result]

Key Takeaways

  • // is floor division (integer result, floors toward negative infinity); / always returns a float.
  • % (modulus) gives the remainder — useful for periodic polling every N ticks.
  • ** is exponentiation and has the highest precedence among arithmetic ops.
  • Augmented assignment (+=, //=, etc.) modifies a variable in one step.
  • Use is / is not for identity checks (None, True, False), not ==.
  • in / not in test membership in sequences and are readable in alert conditions.
  • When in doubt about precedence, add parentheses — they cost nothing and prevent bugs.