Skip to main content

Comparison Operators (==, !=, >, <, >=, <=)

Comparison Operators (==, !=, >, <, >=, <=) for Python Programmers

Comparison operators are used to compare values and determine the relationship between them. This type of operator is called a relational operator, because it compares two different values and returns a result based on the relationship between them. Comparison operators are essential for making decisions in programming. In Python, comparison operators include ==, !=, >, <, >=, <=.

Examples of Comparison Operators

Let’s take a look at some Python examples of comparison operators:

Example 1: == Operator

The == operator is used to check if two values are equal. For example:

a = 10
b = 10

if a == b:
    print("a and b are equal")

In this example, we declared two variables a and b, and assigned them the value of 10. Then, we used the == operator to check if the values of a and b are equal. Since they are equal, the if statement will evaluate to True and the string “a and b are equal” will be printed.

Example 2: != Operator

The != operator is used to check if two values are not equal. For example:

a = 10
b = 11

if a != b:
    print("a and b are not equal")

In this example, we declared two variables a and b, and assigned them the value of 10 and 11 respectively. Then, we used the != operator to check if the values of a and b are not equal. Since they are not equal, the if statement will evaluate to True and the string “a and b are not equal” will be printed.

Example 3: > Operator

The > operator is used to check if the value of the left operand is greater than the value of the right operand. For example:

a = 10
b = 9

if a > b:
    print("a is greater than b")

In this example, we declared two variables a and b, and assigned them the values of 10 and 9 respectively. Then, we used the > operator to check if the value of a is greater than b. Since a is greater than b, the if statement will evaluate to True and the string “a is greater than b” will be printed.

Tips for Using Comparison Operators in Python

  • Always use parentheses ( ) to group comparison operators and expressions.
  • Use the == operator to check if two values are equal, the != operator to check if two values are not equal, the > operator to check if the value of the left operand is greater than the value of the right operand, the < operator to check if the value of the left operand is less than the value of the right operand, the >= operator to check if the value of the left operand is greater than or equal to the value of the right operand, and the <= operator to check if the value of the left operand is less than or equal to the value of the right operand.
  • Remember that comparison operators return a Boolean result (either True or False).

With these tips in mind, you should be able to use comparison operators in Python with ease.

Optimized keywords: Comparison Operators, Python, ==, !=, >, <, >=, <=, Relational Operator, Boolean Result, Examples.