dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Jun 17, 2021

Python Comparison Operators

The Python language relies on six comparison operators to compare values - for both string and integer data types. These comparison operators are used to some degree in pretty much every application you can imagine and play a key role in evaluation and logical operations. We will cover each one briefly here to show how they work and what they mean.

The six Python comparison operators are:

  • == This operator is used to evaluate whether a value is equal to another value. If the two values are equal, then True is returned. If the two values are not equal, then False is returned. It is often referred to as the Equal to operator.
  • != This operator is known as the Not equal to operator. It evaluates to True if the value is not equal to another value and False if it is equal to the other value.
  • < This operator is the Less than operator. It is used to check to see if a value is less than another value. If the value is less than the other value, True is returned. Otherwise, False is returned. Note, if the values are equal to one another, then False is returned, as, obviously, the value is not less than what it is being compared to.
  • <= This operator is known as the Less than or equal to operator. It checks to see if a value is either less than another value or equal. If so, then True is returned. Otherwise, False is returned.
  • > This operator is known as the Greater than operator. It checks to see if a value is greater than another value. If so, it returns True. If not, it returns False Note that, if the two values are equal, it returns False also, because the value is not greater than the other.
  • >= This operator is known as the Greater than or equal to operator. It checks to see if one value is greater than or equal to another value. If so, it returns True. If not, it returns False.

Equal To Operator Example in Python

The following code is an example of how to use the == operator in Python:

# Using the == operator to test if a value is equal to another
a = 200

if a == 100:
    print("a is equal to 100")
else:
    print("a is not equal to 100")

The above code would result in the text "a is not equal to 100" being printed to the user's screen. Had the value of the variable a been 100, then the first part of our if-else block would have executed, resulting in the text "a is equal to 100".

Using the != Operator in Python

Here is an example of how to use the != or not equal to operator in Python:

# Using the != operator to test if a value is equal to another
a = 200

if a != 100:
    print("a is not equal to 100")
else:
    print("a is equal to 100")

Can you guess the output of this program? If you chose: "a is not equal to 100" then you are correct, because a is definitely not equal to 100.

Using the < Operator in Python

Try out the following code in your Python IDE. See if you can guess the result before you run it:

# Using the < operator to test if a value is equal to another
a = 200

if a < 300:
    print("a is less than 100")
else:
    print("a is not less than 100")

Using the <= Operator in Python

The following code demonstrates how to use the <= or less than or equal to operator:

# Using the <= operator to test if a value is equal to another
a = 200

if a <= 200:
    print("a is less than or equal to 200")
else:
    print("a is not less than or equal to 200")

Using the > Operator in Python

The following code shows how to use the > or greater than operator in Python:

# Using the > operator to test if a value is equal to another
a = 200

if a > 100:
    print("a is greater than 100")
else:
    print("a is not greater than 100")

Using the >= Operator in Python

The following is an example of how to use the >= or greater than or equal to operator in Python:

# Using the >= operator to test if a value is equal to another
a = 200

if a >= 200:
    print("a is greater than or equal to 200")
else:
    print("a is not greater than or equal to 200")

James Payne
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date