Lesson 4 of 25

Operators

Arithmetic and Assignment Operators

Operators are special symbols that perform operations on values and variables. Python supports all the standard arithmetic operators you'd expect, plus a few extras like floor division and exponentiation.

Assignment operators let you update a variable's value in shorthand. Instead of writing x = x + 5, you can write x += 5.

Example
# Arithmetic operators
a = 15
b = 4

print(a + b)   # 19   Addition
print(a - b)   # 11   Subtraction
print(a * b)   # 60   Multiplication
print(a / b)   # 3.75 Division (always returns float)
print(a // b)  # 3    Floor division (rounds down)
print(a % b)   # 3    Modulus (remainder)
print(a ** b)  # 50625 Exponentiation (15^4)

# Assignment operators
x = 10
x += 5   # x = x + 5 → 15
x -= 3   # x = x - 3 → 12
x *= 2   # x = x * 2 → 24
x //= 5  # x = x // 5 → 4
print(x) # 4
  • + Addition, - Subtraction, * Multiplication
  • / Division (always returns float), // Floor division (rounds down to nearest integer)
  • % Modulus (returns remainder), ** Exponentiation (power)
  • +=, -=, *=, /=, //=, %=, **= are compound assignment operators
Try Arithmetic Operators
JavaScript
a = 15
b = 4

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponentiation:", a ** b)
Notes
  • The / operator always returns a float in Python 3, even if both operands are integers and the result is a whole number. Use // if you want an integer result.

Comparison and Logical Operators

Comparison operators compare two values and return a boolean (True or False). They are essential for writing conditions in if statements and loops.

Logical operators combine multiple boolean expressions. Python uses the English words 'and', 'or', and 'not' instead of symbols like && and ||.

Example
# Comparison operators
x = 10
y = 20

print(x == y)   # False — equal to
print(x != y)   # True  — not equal to
print(x > y)    # False — greater than
print(x < y)    # True  — less than
print(x >= 10)  # True  — greater than or equal
print(x <= 5)   # False — less than or equal

# Logical operators
a = True
b = False

print(a and b)  # False — both must be True
print(a or b)   # True  — at least one must be True
print(not a)    # False — inverts the boolean

# Combining comparisons
age = 25
print(age >= 18 and age <= 65)  # True — age is between 18 and 65
  • == Equal, != Not equal
  • > Greater than, < Less than
  • >= Greater or equal, <= Less or equal
  • and: True if both operands are True
  • or: True if at least one operand is True
  • not: Inverts the boolean value
Try Comparison Operators
JavaScript
age = 25
has_id = True

print("Is adult:", age >= 18)
print("Is senior:", age >= 65)
print("Can enter:", age >= 18 and has_id)
print("Is teenager:", age >= 13 and age < 20)
print("Not a minor:", not (age < 18))
Notes
  • Python also supports chained comparisons: '18 <= age <= 65' is equivalent to 'age >= 18 and age <= 65', and is more readable.