← Master Index
Vol. 03 Module 3.1 Lecture

Operators

Python Basics

How This Lesson Fits the Module

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 == vs is for ML code.

Arithmetic Operators

Definition — 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
# Manual MSE for predictions [2.0, 4.0] vs targets [1.0, 5.0] predictions = [2.0, 4.0] targets = [1.0, 5.0] squared_errors = [(p - t) ** 2 for p, t in zip(predictions, targets)] mse = sum(squared_errors) / len(predictions) print(f"MSE = {mse:.4f}") # 0.5000 # Learning-rate decay: multiply by factor each epoch learning_rate = 1e-3 learning_rate = learning_rate * 0.95

Comparison and Logical Operators

Comparison

  • val_loss < best_loss
  • accuracy >= 0.90
  • epoch == max_epochs
  • Return True or False

Logical

  • loss_dropped and epoch > 10
  • use_gpu or use_tpu
  • not is_training
  • Combine boolean conditions
best_val_loss = float("inf") patience_counter = 0 patience = 5 # Early stopping condition if val_loss < best_val_loss: best_val_loss = val_loss patience_counter = 0 else: patience_counter += 1 should_stop = patience_counter >= patience and epoch > 0 if should_stop: print("Early stopping triggered")

Assignment and Augmented Assignment

Augmented assignment operators (+=, -=, *=, /=) update a variable in place—common for accumulators and running statistics.

total_loss = 0.0 num_samples = 0 for loss_value, batch_n in [(0.5, 32), (0.3, 32), (0.4, 16)]: total_loss += loss_value * batch_n # weighted sum num_samples += batch_n mean_loss = total_loss / num_samples

Operator Precedence

Python evaluates ** before *//, then +/-, then comparisons, then not, and, or. Use parentheses when readability matters—especially in loss formulas.

Definition — Operator Precedence

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.

What’s Next in This ModuleThe next lecture, Loops, repeats operator-based computations across epochs, batches, and dataset rows.

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

Misconception 1:== 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.

Misconception 2: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.

Misconception 3: “Parentheses are optional if you know precedence.”

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

  1. Short Answer: Write the operator for squared error. Answer: (prediction - target) ** 2 or pow(..., 2).
  2. True/False: // performs floating-point division. Answer: False — floor division.
  3. Multiple Choice: patience_counter >= patience returns: (a) int, (b) bool, (c) float, (d) None. Answer: (b).
  4. Short Answer: What does total_loss += batch_loss do? Answer: Adds batch_loss to total_loss (augmented assignment).
  5. True/False: Use is to compare two float loss values. Answer: False — use == or isclose.
  6. Computation: 3 + 4 * 2 equals? Answer: 11.
  7. Short Answer: Write a condition for saving a checkpoint every 10 epochs. Answer: epoch % 10 == 0 (with appropriate epoch > 0 guard).
  8. True/False: not use_augmentation inverts a boolean flag. Answer: True.
  9. Multiple Choice: Best operator to test missing optional metric: (a) == None, (b) is None, (c) != 0, (d) is 0. Answer: (b).
  10. 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, is for identity (especially None).
  • Next: Loops apply these operations across datasets and training epochs.
Trainer’s Guide

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?

What’s Next Continue to Loops to iterate over batches and epochs with the operators you have learned.