← Back to Python Basics
Module 1Lesson 42 hours

Operators and Expressions

Operators and Expressions

Learn how to manipulate data using Python's powerful operators.

Arithmetic Operators

Perform mathematical operations:

a = 10 b = 3 # Addition print(a + b) # Output: 13 # Subtraction print(a - b) # Output: 7 # Multiplication print(a * b) # Output: 30 # Division (always returns float) print(a / b) # Output: 3.3333333333333335 # Floor Division (rounds down) print(a // b) # Output: 3 # Modulus (remainder) print(a % b) # Output: 1 # Exponentiation (power) print(a ** b) # Output: 1000 (10³)

Comparison Operators

Compare values and get boolean results:

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

Comparing Strings

name1 = "Alice" name2 = "Bob" print(name1 == name2) # False print(name1 < name2) # True (alphabetical order)

Logical Operators

Combine boolean expressions:

age = 25 has_license = True # AND - both must be True can_drive = age >= 18 and has_license print(can_drive) # True # OR - at least one must be True is_weekend = False is_holiday = True can_relax = is_weekend or is_holiday print(can_relax) # True # NOT - inverts the boolean is_raining = False is_sunny = not is_raining print(is_sunny) # True

Practical Example

age = 16 has_license = True has_insurance = True # Check if person can legally drive can_drive = age >= 18 and has_license and has_insurance print(f"Can drive: {can_drive}") # False (age requirement not met)

Assignment Operators

Update variable values efficiently:

# Basic assignment x = 10 # Addition assignment x += 5 # Same as: x = x + 5 print(x) # 15 # Subtraction assignment x -= 3 # Same as: x = x - 3 print(x) # 12 # Multiplication assignment x *= 2 # Same as: x = x * 2 print(x) # 24 # Division assignment x /= 4 # Same as: x = x / 4 print(x) # 6.0 # Floor division assignment x //= 2 # Same as: x = x // 2 print(x) # 3.0 # Modulus assignment x %= 2 # Same as: x = x % 2 print(x) # 1.0 # Exponentiation assignment x **= 3 # Same as: x = x ** 3 print(x) # 1.0

Operator Precedence

Order in which operators are evaluated:

# Without parentheses result = 2 + 3 * 4 print(result) # 14 (multiplication first) # With parentheses result = (2 + 3) * 4 print(result) # 20 (addition first) # Complex expression result = 10 + 5 * 2 ** 2 - 3 print(result) # 10 + 5 * 4 - 3 = 10 + 20 - 3 = 27

Precedence Order (High to Low)

  1. () - Parentheses
  2. ** - Exponentiation
  3. *, /, //, % - Multiplication, Division, Floor Division, Modulus
  4. +, - - Addition, Subtraction
  5. <, <=, >, >=, ==, != - Comparisons
  6. not - Logical NOT
  7. and - Logical AND
  8. or - Logical OR

Practical Examples

Example 1: Calculate Circle Area

PI = 3.14159 radius = 5 area = PI * radius ** 2 circumference = 2 * PI * radius print(f"Area: {area:.2f}") print(f"Circumference: {circumference:.2f}")

Example 2: Check Eligibility

age = 25 income = 50000 credit_score = 700 # Loan eligibility eligible = (age >= 21 and age <= 65) and income >= 30000 and credit_score >= 650 print(f"Eligible for loan: {eligible}")

Example 3: Calculate Discount

price = 100 discount_percent = 20 discount_amount = price * (discount_percent / 100) final_price = price - discount_amount print(f"Original: ${price}") print(f"Discount: ${discount_amount}") print(f"Final: ${final_price}")

Common Mistakes

1. Forgetting Operator Precedence

# Wrong result = 10 + 5 * 2 # Might expect 30, but gets 20 # Right result = (10 + 5) * 2 # 30

2. Using = Instead of ==

# Wrong x = 5 if x = 10: # SyntaxError: assignment in condition print("x is 10") # Right if x == 10: print("x is 10")

3. Integer Division Surprise

# In Python 3 print(10 / 3) # 3.333... (float division) print(10 // 3) # 3 (floor division)

Practice Exercises

  1. Calculate the area and perimeter of various shapes
  2. Write expressions to check multiple conditions
  3. Use assignment operators to update a counter
  4. Practice operator precedence with complex expressions
  5. Build a simple calculator using operators

Next Lesson: String Operations and Formatting