17 min read Computational Core Lesson 6 of 16
Course 03 · Part 2 — Input, Operators & Control Flow

Operators & Expressions: How Computers Compute, Compare & Evaluate

The Computational Engine of Arithmetic, Comparisons, and Logical Reasoning

In the previous lesson, we learned how to collect dynamic input() from users and display structured output. But how does the computer know that a + b represents addition, or that price * quantity computes a bill? The universal grammar governing how data is computed, transformed, and evaluated consists of Operators and Expressions.

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.
10 Operand (Value)
+
5 Operand (Value)
──evaluates to──>
15 Result (Evaluation)

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$)
Why is Modulus (%) ubiquitous in computer science?

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. `==`

age = 25 (Assignment) “Assign the value 25 into variable age.” An imperative mutation statement, not a question!
age == 25 (Comparison) “Does age currently hold the value 25?” An inquiry evaluating to True or False!
== (Equality):
10 == 10 # True
10 == 20 # False
!= (Inequality):
10 != 20 # True
10 != 10 # False
> and < (Strict Relational):
10 > 5 # True
5 < 10 # True
>= and <= (Inclusive Relational):
18 >= 18 # True
15 <= 20 # True

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:

and (Conjunction)

Returns True only when both operands evaluate to True.

True and True → True
True and False → False
or (Disjunction)

Returns True if at least one operand evaluates to True.

True or False → True
False or False → False
not (Inversion)

Inverts the boolean state of its operand.

not True → False
not False → True
# Evaluating compound driver eligibility:
age = 22
has_driving_license = True
can_drive = age >= 18 and has_driving_license
print("Eligible to drive?", can_drive) # Output: True

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:

x += 5 x = x + 5
x -= 3 x = x - 3
x *= 2 x = x * 2
x /= 4 x = x / 4

27 & 28 Operator Precedence & Parentheses `()`

When an expression contains multiple operators, Python resolves them according to canonical precedence rules (BODMAS/PEMDAS):

Standard Precedence:
result = 10 + 5 * 2
Multiplication takes precedence ($5 \times 2 = 10$), followed by addition ($10 + 10 = 20$). Result: 20
Overriding via Parentheses:
result = (10 + 5) * 2
Parentheses force addition first ($10 + 5 = 15$), followed by multiplication ($15 \times 2 = 30$). Result: 30

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:

checkout.py
price = float(input("Item price ($): "))
quantity = int(input("Quantity: "))
discount_percent = 10
# Mathematical expression evaluation
subtotal = price * quantity
discount_amount = (subtotal * discount_percent) / 100
final_bill = subtotal - discount_amount
# Relational inquiry (Free shipping granted for orders $50+)
qualifies_free_shipping = final_bill >= 50.0
print(f"Subtotal: ${subtotal:.2f} | Final: ${final_bill:.2f}")
print(f"Free Shipping Eligible: {qualifies_free_shipping}")

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:

Challenge 1: Four-Function Mini Calculator Input two float values and compute their sum, difference, product, and float division.
Challenge 2: Even vs. Odd Parity Checker Prompt the user for an integer and evaluate number % 2 == 0 to indicate if the number is even (True/False).
Challenge 3: Voting Age Eligibility Check Capture the user's age and determine voting franchise with the relational expression age >= 18.
Challenge 4: Commercial Discount Invoicing Input unit price and discount rate to calculate discount deduction and net payable amount.
Challenge 5: Multi-Criteria Access Gate Combine user age and boolean credential status (has_pass = True) using the and operator to authorize facility entry.

35 🧠 Key Takeaways & Summary

Operators: Symbols commanding CPU operations on operands.
Expressions: Syntactic combinations evaluating to single resulting values.
Division Trio: / returns float quotient, // computes floor integer, and % computes division remainder.
Assignment vs. Equality: = binds values to names; == queries equality to return booleans.
Logical Operators: 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.