The Computational Heart of the Machine
Recall from Course 01 and Course 02 that the CPU features a dedicated hardware sub-unit called the ALU (Arithmetic Logic Unit). Operators are the programming language keywords and symbols that instruct the ALU. They do not merely perform arithmetic—they ask critical questions: “Is this balance greater than zero? Are both security credentials valid?”
01 & 02 What Truly are Operators & Expressions?
Stated formally and precisely:
An Operator is a special character or reserved keyword directing the runtime to perform a specific mathematical or logical action on one or more data items (Operands). An Expression is any valid syntactic combination of operands and operators that evaluates to a single resulting value.
The complete unit 10 + 5 is an expression. Likewise, price * quantity and age >= 18 are fundamental expressions evaluated during runtime execution.
03 to 10 Arithmetic Operators
Python provides seven essential arithmetic operators:
| Operator | Operation | Example | Result | Description |
|---|---|---|---|---|
| + | Addition | 10 + 3 | 13 | Computes numerical sum |
| - | Subtraction | 10 - 3 | 7 | Subtracts right operand from left |
| * | Multiplication | 10 * 3 | 30 | Computes product |
| / | Float Division | 10 / 2 | 5.0 | Always returns a floating-point quotient |
| // | Floor Division | 10 // 3 | 3 | Truncates remainder towards negative infinity |
| % | Modulus (Remainder) | 10 % 3 | 1 | Returns the integer remainder of division |
| ** | Exponentiation | 2 ** 3 | 8 | Raises left operand to power of right ($2^3$) |
The modulus operator is the canonical mechanism for detecting numerical parity (Even vs. Odd): number % 2 == 0 yields True if even, and False if odd. It is also used to wrap cyclic clock intervals and hash bucket distributions.
11 to 19 Comparison (Relational) Operators
When software must measure relationships between quantities, it uses Comparison Operators. These operations always evaluate to a boolean truth value: True or False.
Avoid the Classic Trap: `=` vs. `==`
20 to 24 Logical Operators: `and`, `or`, `not`
Real business rules routinely evaluate compound truth criteria (e.g., The user credentials must match and the two-factor token must be verified). Python provides three expressive logical operators:
Returns True only when both operands evaluate to True.
Returns True if at least one operand evaluates to True.
Inverts the boolean state of its operand.
25 & 26 Compound Assignment Operators (Shorthand)
Modifying an existing variable with its own previous value is such a frequent idiom that Python provides compact syntactic shortcuts:
27 & 28 Operator Precedence & Parentheses `()`
When an expression contains multiple operators, Python resolves them according to canonical precedence rules (BODMAS/PEMDAS):
Best Practice: Whenever composing multi-step business logic, wrap your terms explicitly in parentheses () to guarantee clarity for human teammates and the runtime engine alike.
29 to 32 Commercial E-Commerce Calculation & Logical Queries
Let us unite input collection, arithmetic, relational comparisons, and formatted outputs into a commercial cart processor:
33 Standing at the Doorway to Decision Making
We have now reached a profound milestone. Our programs can hold state, take inputs, emit output, calculate arithmetic, and evaluate complex logical assertions that produce True or False.
Yet our code remains linear—it still runs blindly from line 1 to the end. The true spark of artificial computation occurs when software can branch: executing one block of instructions if an expression is True, and an entirely different block if False. That transformative power arrives in our next lesson: Control Flow & Decision Making (`if`, `elif`, `else`)!
34 Hands-On Practice Challenges
Open operators_practice.py in your editor and solve these five exercises:
number % 2 == 0 to indicate if the number is even (True/False).
age >= 18.
has_pass = True) using the and operator to authorize facility entry.
35 🧠 Key Takeaways & Summary
/ returns float quotient, // computes floor integer, and % computes division remainder.= binds values to names; == queries equality to return booleans.and, or, and not formulate compound decision logic.Next Lesson: Decision Making & Conditions
How do programs execute different instructions based on changing boolean logic? Master branching execution flows using if, elif, and else.