Як записується умова рівності на Python: просте пояснення

How to Write an Equality Condition in Python

An equality condition in Python is written with the == operator, not with =. That difference is one of the first things beginners need to learn, because one symbol assigns a value and two symbols compare values.

What the == operator means in Python

The == operator checks whether two values are the same. If they match, the expression returns True; if they do not, it returns False.

Examples:

  • 5 == 5 → True
  • 5 == 3 → False
  • "cat" == "cat" → True

This is the basic equality check used in if, while, and other conditional statements.

How == differs from =

The = sign in Python means assignment, not equality testing. Writing x = 10 creates a variable or changes its value.

The difference is simple:

  • x == 10 checks whether x is equal to 10
  • x = 10 stores 10 in x

Using a single equals sign in a condition usually causes a SyntaxError, because Python expects a comparison, not an assignment.

How to write an equality condition in if

An equality condition in Python inside an if statement is written directly with ==.

Examples:

  • if age == 18:
  • if password == "admin":
  • if score == 100:

This reads naturally: if the variable matches the expected value, the code inside the block runs.

Common mistakes when checking equality

An equality condition in Python usually breaks because of a few simple mistakes.

  • Mixing up = and == — this is the most common error.
  • Comparing numbers and strings — for example, 5 == "5" returns False because the types are different.
  • Ignoring letter case"Python" == "python" returns False.
  • Forgetting about spaces — an extra space in a string changes the result.

To verify that the condition works, print the result with print(). If you see True or False, the comparison is written correctly. If you get an error, check first whether = was used instead of ==.

Practical example of a comparison check

An equality condition in Python is useful for simple input checks.

For example, when a user enters a password:

  • store the input in a variable;
  • compare it with the expected string using ==;
  • show a success or error message.

If the result is not what you expected, check the data type, letter case, and any extra spaces. For text values, strip() often helps remove accidental spaces, and lower() helps when you want a case-insensitive comparison.

The correct answer to how to write an equality condition in Python is simple: use the == operator. It compares values, while = only assigns them to a variable.