DEV Community

Ridwan Ahmed Arman
Ridwan Ahmed Arman

Posted on • Edited on

Revise Python in 15 days for ML (2025): Day 3 (Operators & Conditions)

Welcome back, Python explorers! This is Day 3 of our coding journey. Missed Day 2? Catch up before diving in today!

🔧 The Power Tools: Python Operators

Think of operators as Python's Swiss Army knife - small symbols that perform powerful actions. Let's unlock them one by one!

➕ Arithmetic Operators: Math Made Easy

print(3 + 4)   # 7 (Addition)
print(3 - 4)   # -1 (Subtraction)
print(3 * 4)   # 12 (Multiplication)
print(3 / 4)   # 0.75 (Division)
print(2 ** 4)  # 16 (Exponentiation - 2 raised to power 4)
print(3 // 4)  # 0 (Floor division - divides and rounds down)
print(3 % 4)   # 3 (Modulus - returns remainder after division)
Enter fullscreen mode Exit fullscreen mode

Quick Tips:

  • Use ** for powers (faster than importing math.pow)
  • // gives you clean integers when you don't need decimals
  • % is perfect for checking divisibility or creating cycles

🔍 Identity Operators: The Memory Detectives

x = 3
y = 4
print(x is y)      # False
print(x is not y)  # True
Enter fullscreen mode Exit fullscreen mode

💡 Key Insight: is checks if two variables reference the same object in memory - not just the same value!

⚠️ The == vs is Trap:

x = [1, 2, 3] 
y = [1, 2, 3]

print(x == y)  # True (values are equal)
print(x is y)  # False (different objects in memory)
Enter fullscreen mode Exit fullscreen mode

But wait! Numbers and strings can behave differently:

x = 10
y = 10
print(x is y)  # True (Python optimizes by reusing same object)
Enter fullscreen mode Exit fullscreen mode

🧮 Bitwise Operators: Binary Magic

print(3 & 4)   # 0 (Bitwise AND)
print(3 | 4)   # 7 (Bitwise OR)
print(3 ^ 4)   # 7 (Bitwise XOR)
print(~4)      # -5 (Bitwise NOT)
print(3 << 4)  # 48 (Left shift)
print(3 >> 4)  # 0 (Right shift)
Enter fullscreen mode Exit fullscreen mode

Visual breakdown: When we do 3 & 4:

  • 3 in binary = 011
  • 4 in binary = 100
  • 011 & 100 = 000 (0 in decimal)

⚖️ Comparison Operators: Decision Makers

print(3 == 4)  # False (Equal to)
print(3 != 4)  # True (Not equal to)
print(3 > 4)   # False (Greater than)
print(3 < 4)   # True (Less than)
print(3 >= 4)  # False (Greater than or equal to)
print(3 <= 4)  # True (Less than or equal to)
Enter fullscreen mode Exit fullscreen mode

Remember: These always return Boolean values (True or False) - the foundation of conditional logic!

✏️ Assignment Operators: Elegant Shortcuts

x = 3      # Basic assignment
x += 4     # x = x + 4 (now x is 7)
Enter fullscreen mode Exit fullscreen mode

More shortcuts you'll love:

  • -= for subtraction
  • *= for multiplication
  • /= for division
  • **= for exponentiation
  • //= for floor division
  • %= for modulus

🔎 Membership Operators: The Finders

x = [1, 2, 3]
print(3 in x)       # True
print(4 not in x)   # True
Enter fullscreen mode Exit fullscreen mode

Pro tip: These work on any iterable - lists, tuples, strings, dictionaries, and sets!

🧠 Conditional Statements: The Decision Trees

⚡ Basic if-elif-else Structure

x = 10
if x == 10:
    print("x is 10")
elif x == 20:
    print("x is 20")
else:
    print("x is neither 10 nor 20")
Enter fullscreen mode Exit fullscreen mode

Flow visualization:

  1. Check if x is 10
    • If TRUE → Print message and exit
    • If FALSE → Continue to next check
  2. Check if x is 20
    • If TRUE → Print message and exit
    • If FALSE → Execute else block

🌳 Nested Conditionals: Complex Decision Trees

num = int(input("Enter a number: "))
if num % 2 == 0:
    if num % 3 == 0:
        print("The number is divisible by both 2 and 3.")
    else:
        print("The number is divisible by 2 but not by 3.")
else:
    if num % 3 == 0:
        print("The number is not divisible by 2 but divisible by 3.")
    else:
        print("The number is not divisible by either 2 or 3.")
Enter fullscreen mode Exit fullscreen mode

Decision chart:

  • Is num divisible by 2?
    • YES → Is num divisible by 3?
    • YES → "Divisible by both 2 and 3"
    • NO → "Divisible by 2 but not by 3"
    • NO → Is num divisible by 3?
    • YES → "Not divisible by 2 but divisible by 3"
    • NO → "Not divisible by either 2 or 3"

🏆 Challenge Yourself!

Put your new skills to use:

  • Create a program that determines if a year is a leap year
  • Build a simple calculator that uses all arithmetic operators
  • Write a script to check if a number is prime

🔗 Resources

🔮 Coming Up Next...

Tomorrow we'll explore loops and iteration - the tools that let your programs repeat tasks efficiently!


← Day 2: Python Data Types | Day 3: Operators & Conditionals | [Day 4: Coming Soon →]


Drop a comment below with your favorite Python operator and why you love it! Let's learn together! 💬

Top comments (0)