Python Comparison Operators (AKA Relational Operators) Complete Tutorial

In this section, we will learn what comparison operators are and how to use them in Python.

Note: we’re assuming you already familiar with operators in general.

What are the Comparison Operators?

The comparison operators are used to compare two values in order to see whether they are equal or one is greater than the other.

For example, one of these operators is `>` which stands for greater-than. Now using this operator, we can see if the Left-Hand operand is greater than the Right-Hand operand.

The result of calling comparison operators is always a boolean value. For example, for the `greater-than` operator, if the Left-Hand operator is greater, then the result of this operator will be True. Otherwise, if the opposite was right, then the final value of this comparison will be False.

We mainly use comparison operators with conditional operators like `if` statement.

Conditional statements are basically a block of instructions that will run only if their condition is True. Now, in order to create condition for such type of statements, we use comparison operators.

List of Comparison Operators in Python

Here’s the list of comparison operators:

Operator Symbol Description
Equal == Using this operator, we can check and see if two values are equal or not.
Negate Equal != Using this operator, we can check and see if two values are NOT equal.
Greater than > Using this operator, we can check to see if the left operand is greater than the right operand.
Less than < Using this operator, we can check to see if the right operand is greater than the left operand.
Greater Than or equal to >= Using this operator, we can see if the left operand is either greater or equal compared to the right operand.
Less than or equal to <= Using this operator, we can see if the right operand is either greater or equal compared to the left operand.

Conditional Statements

As mentioned before, comparison operators are mainly used within conditional statements.

Now, because we are not yet reached to the conditional statements, let’s first give an introductory to one of these conditional statements (if statement for this section) and then use the `if` statement to help us see how comparison operators work.

Note: if you are familiar with the `if` statement already, skip this part and move down to the comparison operators then.

Conditional Statements: If statement

The if statement is used to create a block of instructions and block it with a condition statement. Basically, when it comes to an if statement, the Python execution engine will first check the condition on top of the `if` statement to see if the result is True or not! If the result of the condition was true, the execution engine will move into the body of the if statement and runs the entire instructions there, otherwise if the result of the condition was False, then the engine will skip the body of the `if` statement and moves to the instructions that comes after this body.

The important part that you need to be aware of for now is that the condition of an if statement is nothing but an expression that results in either the value True or False. This expression could be as simple as the direct use of the value `True` or `False` or a comparison between two values using the `comparison-operators`. Because after all, using comparison operators would return a boolean value which could be either True or False.

Python if statement Syntax:

if condition:

#body…

`if`: in order to create an if statement, we first start with the keyword `if`.

`condition`: this is where we put the condition of the statement. The execution engine will evaluate this condition and, based on the result, it will decide whether the body of the if statement (hence the instructions in it) should be executed or skipped.

`:`: the colon defines the if statement’s beginning body.

Note: the indented lines after the `if` statement will be part of the body of this statement. They will be executed if the condition of the `if` statement result True. Otherwise, if the result of the condition was False, then the execution engine will move to part of the source code where its no-longer indented. (Or simply the part of the program that is no-longer part of the if statement’s body.)

If statement Example:

age = 50
if age<60:
    print("The age is less than the value 60")

Output:

The age is less than the value 60

Here for the condition of the if statement we’ve used the `less-than >` operator, which is a comparison operator, in order to see if the value of the `age` variable is less than the value 60. Now because the value is less than 60, then the final result of this comparison is True and so the condition is True and the execution engine is allowed to run the instructions within the body of the if statement.

Note: there are more into the if statement and you’ll learn more about them in the Python if-statement section.

Alright, now let’s see how comparison operators work in Python.

Python Equal `==` Operator:

The equal operator is used to see if two operands are equal in value or not.

If both operands were equal, then the return value of this operator will be True.

Note: this operator only cares about the values of the operands and not their memory locations.

If you want to check the equality of two values using their memory location, then use the `is` and `is not` operators.

Equal `==` Operator Example:

a = 20

b = 20

print(a==b)

Output:

True

Here, both variables are pointing to the value 20 and so they are equal and the result of this comparison is True.

Python Not Equal `!=` Operator:

The not-equal operator is used to compare two operands and see if they are not equal in value or not.

If they were not equal, then the return value of this operator will be True. But if operands were equal, then the return value of this operand will be False.

Not Equal `!=` Operator Example:

a = 20

b = 20

print(a!=b)

Output:

False

Here, because both variables are the same value, then the result of this operator will be False.

Python Greater Than `>` Operator:

The greater than operator is used to see if the left operand is greater than the right operand.

If it was, then the return value of this operator will be True. Otherwise the value False will return instead.

Greater Than `>` Operator Example:

a = 50
b = 30
if a>b:
    print("The value of the variable a is bigger than the value of the variable b")

Output:

The value of the variable a is bigger than the value of the variable b

Python Less Than `<` Operator:

The less than operator is used to see if the left-hand operand is less than the right-hand operand. If it was, then the return value of this operator will be True. Otherwise the value False will return instead.

Less Than `<` Operator Example:

a = 50
b = 30
if a<b:
    print("The value of the variable a is bigger than the value of the variable b")

Output:

Note that here the value of the `a` variable is not less than the value of the `b` variable and for that reason the result of the `if` condition became False and hence its body was not executed. That’s why we didn’t get any output.

Python Greater than or Equal To `>=` Operator:

The `Greater than or Equal` operator is used to see if the left-hand operand is greater or equal to the right-hand operand.

If it was either greater or equal, then the return value of this operator will be True. Otherwise the value False will return instead.

Greater than or Equal To `>=` Operator Example:

a = 50
b = 30
if a>=b:
    print("The value of the variable a is either bigger than or equal to the value of the variable b")

Output:

The value of the variable a is either bigger than or equal to the value of the variable b

Python Less Than or Equal To `<=` Operator:

The `Less Than or Equal` operator is used to see if the left-hand operand is lesser or equal to the right-hand operand.

If it was either less than or equal to the right-hand operand, then the result of this operator will be True. Otherwise the value False will return instead.

Less Than or Equal To `<=` Operator Example:

a = 50
b = 30
if a<=b:
    print("The value of the variable a is either bigger than or equal to the value of the variable b")

Output:

Note that we didn’t get any output from this program! This is because the value of the `a` variable is neither less nor equal to the value of the `b` variable and for this reason, the result of the condition in the if statement became False and the instructions in its body skipped.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies