Andromeda
Note

Math Operators (Python)

Definition

Symbols used in expressions to perform mathematical calculations on values.

Why It Matters

Math operators are the ‘verbs’ of programming; a lack of mastery over how Python handles division, modulo, and exponentiation leads to subtle ‘logic bugs’ that can crash complex systems or produce dangerously incorrect data.

Core Concepts

  • Operator Hierarchy (Precedence):
    1. ** (Exponent)
    2. *, /, //, % (Multiplication, Division, Integer Division, Modulus)
    3. +, - (Addition, Subtraction)
  • Integer Division (//): Performs division and rounds down to the nearest whole number (floored quotient).
  • Modulus (%): Calculates the remainder of a division (e.g., 22 % 8 is 6).
  • Grouping: Parentheses () override default precedence.
# Basic operations and precedence
result = (2 + 3) * 4  # 20
floor_div = 10 // 3   # 3
remainder = 10 % 3    # 1
power = 2 ** 3        # 8

Connected Concepts