Menu
Courses / python basic / Operators & Expressions

Operators & Expressions

04 / 10 Part of python basic




 

    ➕ Topic 03 · Logic
 

 


    Operators & Expressions
 


 


    Master arithmetic, comparison, logical, and assignment operators to build meaningful expressions in every Python program.
 


 

    ⏱ ~30 min
    🟢 Beginner
    🧮 Math & Logic
 


 
 

   

     
     

Arithmetic Operators


   

   

Python supports all standard math operations plus integer division and exponentiation.


   

     

        arithmetic.py
     

     
a, b = 10, 3

print(a + b)   # 13  — addition
print(a - b)   # 7   — subtraction
print(a * b)   # 30  — multiplication
print(a / b)   # 3.333... — true division
print(a // b)  # 3   — floor division
print(a % b)   # 1   — modulo (remainder)
print(a ** b)  # 1000 — exponentiation

   

 


 
 

   

     
     

Comparison Operators


   

   

Comparison operators return True or False and are the foundation of conditional logic.


   

     

        comparison.py
     

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

   

 


 
 

   

     
     

Logical Operators


   

   

Combine boolean expressions with and, or, and not.


   

     

        logical.py
     

     
is_adult    = True
has_ticket  = False

print(is_adult and has_ticket)  # False
print(is_adult or  has_ticket)  # True
print(not is_adult)              # False

   

 


 
 

   

     
     

Assignment Operators


   

   

     

        x = 5
       

Basic assignment


     

     

        x += 3
       

Add & assign → x = x + 3


     

     

        x -= 2
       

Subtract & assign


     

     

        x *= 4
       

Multiply & assign


     

     

        x //= 2
       

Floor divide & assign


     

     

        n := 10
       

Walrus (3.10+) — assign inside expression


     

   

 


 
 

   

     
     

Operator Precedence


   

   

Python follows standard math precedence: parentheses → exponents → multiply/divide → add/subtract.


   

     

        precedence.py
     

     
result = 2 + 3 * 4          # 14 (not 20)
result = (2 + 3) * 4        # 20
result = 2 ** 3 ** 2        # 512 (right-associative)

   

   

      💡 Best Practice
     

Use parentheses to make complex expressions clear — don't rely on readers memorising precedence rules.