Thursday, December 3, 2026 | London 10°C · Drizzle
DailyGlimpse

AI

Python Beginners Beware: The #1 Comparison Operator Mistake

Data Science With Dennis 1 min read

When learning Python, many beginners trip over a simple yet critical detail: using a single equals sign (=) instead of a double equals sign (==) in comparisons. This is more than a typo—it changes the meaning of your code.

In Python, a single equals sign is an assignment operator: it assigns a value to a variable. For example, x = 5 sets x to 5. A double equals sign is a comparison operator: it checks if two values are equal, returning True or False. The classic mistake is writing if x = 5: instead of if x == 5:. The former assigns 5 to x and always evaluates to True (since assignment expressions in Python always yield the assigned value), leading to unexpected behavior.

Python offers six comparison operators:

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

Mastering these and remembering the double equals sign is essential for writing correct conditional logic. Avoid this rookie mistake and your Python code will be more reliable.

React