Python 3 — Basic Operators & Math

🐍 Python 3 🟢 Lesson 3 📅 July 2026

Operators are the symbols that tell Python how to perform calculations and comparisons. From simple addition to advanced bitwise operations, understanding operators is fundamental to writing any real program.

1 Arithmetic Operators
OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division (float)10 / 33.333...
//Floor Division10 // 33
%Modulo (remainder)10 % 31
**Exponent (power)2 ** 101024
Python 3 — Arithmetic▶ Run Code
a, b = 17, 5
print(f"{a} + {b} = {a + b}")    # 22
print(f"{a} - {b} = {a - b}")    # 12
print(f"{a} * {b} = {a * b}")    # 85
print(f"{a} / {b} = {a / b}")    # 3.4
print(f"{a} // {b} = {a // b}")  # 3 (floor)
print(f"{a} % {b} = {a % b}")    # 2 (remainder)
print(f"{a} ** {b} = {a ** b}")  # 1419857

# Common use of modulo: check even/odd
for n in range(1, 11):
    status = "even" if n % 2 == 0 else "odd"
    print(f"{n} is {status}")
2 Assignment Operators

Python has shorthand operators that combine assignment with an operation:

Python 3 — Assignment Operators▶ Run Code
score = 100

score += 10   # score = score + 10  → 110
print(score)

score -= 5    # score = score - 5   → 105
print(score)

score *= 2    # score = score * 2   → 210
print(score)

score //= 3   # score = score // 3  → 70
print(score)

score **= 2   # score = score ** 2  → 4900
print(score)

score %= 100  # score = score % 100 → 0
print(score)
3 Comparison Operators
Python 3 — Comparison▶ Run Code
x, y = 10, 20
print(x == y)   # False
print(x != y)   # True
print(x < y)    # True
print(x > y)    # False
print(x <= 10)  # True
print(x >= 10)  # True

# Chained comparisons (very Pythonic!)
age = 25
print(18 <= age <= 65)  # True — adult working age

temp = 37
print(36.1 <= temp <= 37.5)  # True — normal body temp
4 Logical Operators
Python 3 — Logical Operators▶ Run Code
# and — both must be True
print(True and True)    # True
print(True and False)   # False

# or — at least one must be True
print(False or True)    # True
print(False or False)   # False

# not — inverts
print(not True)         # False
print(not False)        # True

# Short-circuit evaluation
x = 0
# 'and' stops at first False
result = (x != 0) and (100 / x > 5)  # Safe! Doesn't divide
print(result)  # False (no ZeroDivisionError)
5 Math Module

For advanced math, import Python's built-in math module:

Python 3 — math Module▶ Run Code
import math

print(math.sqrt(144))      # 12.0 — square root
print(math.pi)             # 3.141592653589793
print(math.ceil(4.1))      # 5 — round up
print(math.floor(4.9))     # 4 — round down
print(math.pow(2, 8))      # 256.0
print(math.log(1000, 10))  # 3.0 — log base 10
print(math.factorial(6))   # 720
print(math.gcd(48, 18))    # 6 — greatest common divisor
print(abs(-99))            # 99 — absolute value
print(round(3.14159, 2))   # 3.14
6 Bitwise Operators

Bitwise operators work on the binary representation of integers. Used in system programming, flags, and optimizations:

OperatorNameExampleBinary Logic
&AND5 & 3 = 10101 & 0011 = 0001
|OR5 | 3 = 70101 | 0011 = 0111
^XOR5 ^ 3 = 60101 ^ 0011 = 0110
~NOT~5 = -6Flips all bits
<<Left shift1 << 4 = 16Multiply by 2^n
>>Right shift16 >> 2 = 4Divide by 2^n
7 Operator Precedence

Python follows mathematical order of operations (BODMAS/PEMDAS). Use parentheses to make intent explicit:

Python 3 — Precedence▶ Run Code
# Without parentheses — follows precedence rules
result = 2 + 3 * 4 ** 2
print(result)   # 2 + 3 * 16 = 2 + 48 = 50

# With parentheses — explicit and clear
result = (2 + 3) * (4 ** 2)
print(result)   # 5 * 16 = 80

# Common real-world formulas
radius = 7
area = math.pi * radius ** 2  # πr²
print(f"Circle area: {area:.2f}")

principal = 10000
rate = 0.08
years = 5
compound = principal * (1 + rate) ** years
print(f"Compound interest amount: {compound:.2f}")
8 Coding Challenge

Write a program that:

  • Takes two numbers and prints all 7 arithmetic results (+, -, *, /, //, %, **)
  • Calculates the area and circumference of a circle using math.pi
  • Uses chained comparison to classify a temperature as: "Freezing" (<0), "Cold" (0-15), "Warm" (15-30), "Hot" (>30)
  • Uses assignment operators (+=, -=, *=) to simulate a bank account: start at 1000, deposit 500, charge 50 fee, apply 5% interest