03

Day 3: Operators and Expressions

Chapter 3 • Beginner

30 min

Operators are special symbols that help you perform calculations and comparisons in Python. Think of them as mathematical tools that work with your data.

What are Operators?

Operators are symbols that tell Python what operation to perform on variables and values. They're like mathematical symbols but for programming.

Types of Operators:

Arithmetic Operators:

  • + (Addition) - Adds two numbers
  • - (Subtraction) - Subtracts one number from another
  • * (Multiplication) - Multiplies two numbers
  • / (Division) - Divides one number by another
  • % (Modulus) - Returns the remainder after division
  • ** (Exponentiation) - Raises a number to a power
  • // (Floor Division) - Divides and rounds down to nearest integer

Comparison Operators:

  • == (Equal) - Checks if two values are equal
  • != (Not equal) - Checks if two values are not equal
  • > (Greater than) - Checks if first value is greater
  • < (Less than) - Checks if first value is smaller
  • >= (Greater than or equal) - Checks if first value is greater or equal
  • <= (Less than or equal) - Checks if first value is smaller or equal

Logical Operators:

  • and (Logical AND) - Both conditions must be true
  • or (Logical OR) - At least one condition must be true
  • not (Logical NOT) - Reverses the result

Assignment Operators:

  • = (Assignment) - Assigns a value to a variable
  • += (Add and assign) - Adds and assigns the result
  • -= (Subtract and assign) - Subtracts and assigns the result
  • *= (Multiply and assign) - Multiplies and assigns the result
  • /= (Divide and assign) - Divides and assigns the result

Hands-on Examples

Arithmetic Operations

# Arithmetic operators
a = 10
b = 3

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)

# Mixed operations
result = (a + b) * 2 - 5
print("Complex expression:", result)

Python follows standard mathematical order of operations (PEMDAS/BODMAS).