After Data Types, you know what objects variables reference. Operators are the verbs—symbols and keywords that combine those objects: add feature values, compare validation loss, scale gradients, and chain boolean conditions for early stopping.
Every loss function in Volume 02 calculus—MSE, cross-entropy—is built from arithmetic and logarithmic operations. Operators are how you express those formulas in Python before delegating to NumPy or PyTorch.
Learning Objectives
By the end of this lesson, students should be able to:
- Use arithmetic operators (
+,-,*,/,//,%,**) on numeric types. - Apply comparison operators (
==,!=,<,>,<=,>=) to evaluate metrics. - Combine conditions with logical operators (
and,or,not). - Understand operator precedence and use parentheses for clarity.
- Implement a manual MSE computation using operators.
- Recognize when to use
==vsisfor ML code.
Arithmetic Operators
Arithmetic operators perform numeric computation. In AI engineering, they implement loss formulas, learning-rate scaling, feature normalization, and batch-size arithmetic.
| Operator | Name | ML Example |
|---|---|---|
+, - |
Addition, subtraction | Residual: error = target - prediction |
*, / |
Multiply, divide | Scale gradient: lr * grad |
// |
Floor division | num_batches = len(data) // batch_size |
% |
Modulo | epoch % 10 == 0 for checkpointing |
** |
Exponentiation | MSE: error ** 2 |
Comparison and Logical Operators
Comparison
val_loss < best_lossaccuracy >= 0.90epoch == max_epochs- Return
TrueorFalse
Logical
loss_dropped and epoch > 10use_gpu or use_tpunot is_training- Combine boolean conditions
Assignment and Augmented Assignment
Augmented assignment operators (+=, -=, *=, /=) update a variable in place—common for accumulators and running statistics.
Operator Precedence
Python evaluates ** before *//, then +/-, then comparisons, then not, and, or. Use parentheses when readability matters—especially in loss formulas.
Precedence determines evaluation order in compound expressions. In a + b * c, multiplication binds tighter than addition. For ML code, explicit parentheses document intent: (x - mean) / std.
Identity vs Equality
| Operator | Tests | When to Use |
|---|---|---|
== |
Value equality | Compare metrics, hyperparameters |
is |
Same object identity | x is None, singleton checks |
!= |
Value inequality | Filter mislabeled samples |
Common Misconceptions
== and is are interchangeable.”
Why people believe it: Both return booleans from comparisons.
Reality: == compares values; is compares object identity. Two equal floats may be different objects; use == for metrics, is None for missing values.
and returns True or False only.”
Why people believe it: Boolean logic courses emphasize True/False.
Reality: Python’s and/or return the last evaluated operand. config.get("lr") or 1e-3 provides a default learning rate—a common ML pattern.
Why people believe it: Short expressions seem obvious to the author.
Reality: Loss and normalization formulas are read by teammates. Parentheses in (1 - epsilon) prevent misreading during code review.
Quick Knowledge Check
- Short Answer: Write the operator for squared error. Answer: (prediction - target) ** 2 or pow(..., 2).
- True/False:
//performs floating-point division. Answer: False — floor division. - Multiple Choice:
patience_counter >= patiencereturns: (a) int, (b) bool, (c) float, (d) None. Answer: (b). - Short Answer: What does
total_loss += batch_lossdo? Answer: Adds batch_loss to total_loss (augmented assignment). - True/False: Use
isto compare two float loss values. Answer: False — use == or isclose. - Computation:
3 + 4 * 2equals? Answer: 11. - Short Answer: Write a condition for saving a checkpoint every 10 epochs. Answer: epoch % 10 == 0 (with appropriate epoch > 0 guard).
- True/False:
not use_augmentationinverts a boolean flag. Answer: True. - Multiple Choice: Best operator to test missing optional metric: (a) == None, (b) is None, (c) != 0, (d) is 0. Answer: (b).
- Short Answer: Why implement MSE manually before using a library? Answer: Understand the math behind the loss function.
Key Takeaways
- Arithmetic operators implement loss formulas, scaling, and batch arithmetic.
- Comparison and logical operators drive early stopping, checkpointing, and gating.
- Augmented assignment (
+=,*=) updates accumulators efficiently. - Precedence matters; parentheses clarify normalization and loss expressions.
- Use
==for values,isfor identity (especiallyNone). - Next: Loops apply these operations across datasets and training epochs.
Hands-on idea: Students implement MSE and MAE from scratch on a tiny list of predictions, then verify against a calculator.
Debugging exercise: Break an early-stopping condition by using & instead of and (NumPy vs Python)—preview of library differences.
Discussion prompt: Where in a training script do augmented assignment operators appear most often?