What is the difference between `=` and `==` in Python?


#1

Question

What is the difference between = and == in Python? Is there any relation?

Answer

In Python, the operators = and == serve very different purposes, and don’t have much relation at all other than similar syntax.

The = operator is used for assignment, such as when assigning a value to a variable.

The == operator is the relational operator for checking equality of two values. If the values are the same, it will return True, and will return False otherwise.

Example

# Assignment
number = 3

# Comparison
number == 30 # False