Python Beginner Diary Day008: = vs == (assign vs compare)

This is such a basic thing, but I always got it wrong. So I created a reminder video for myself. Here, it’s very important to remember that the equals sign and Python is not the equals sign; it’s the assignment operator, because it assigns a value to a variable.

The single equals sign (=) in Python is the assignment operator, which assigns a value on the right to a variable on the left. The double equals sign (==) is the equality comparison operator, which checks if two values are equal.

Here’s a simple breakdown to reinforce your reminder:

  • = (Assignment): This is an action. It’s like putting something into a box. For example, x = 5 means “put the value 5 into the box named x.”
  • == (Comparison): This is a question. It’s like asking if two things are the same. For example, x == 5 asks, “Is the value in the box x equal to 5?” The answer is either True or False.

# == comparison operator (checks if the values of two operands are equal. It returns True if the values are the same and False otherwise.
# = Assignment operator. The assignment operator assigns a value to a variable, while the comparison operator is used to check for equality.

x = 5
y = 4
print(x == y)


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *