How to use Python not equal and equal to operators?

An infographic on Python's 'equal to' (==) and 'not equal to' (!=) operators. Learn how to compare values and make logical decisions in Python

In Python, you may use the equal to (==) and not equal to (!=) operators for testing the equality of two objects.

Python supports a number of comparison operators as given below:

Operator Description
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to

For example:

Not equal to

int_x != int_y

Equal to:

x == 4

str = ‘Python’

In the following section, I will show you examples of how to use Python not equal and equal to operator (as the title of this article suggests).

Note: The <> (not equal to or equivalent of !=) was also supported in version 2 of Python, however, this is deprecated in version 3 and produces an “Invalid Syntax” Error.

A simple example of not equal operator

For this example, the int_x variable is assigned the value of 20, and int_y = 30. In the if statement, the condition is to check if int_x is not equal to int_y i.e.

Int_x != int_y

If int_x is not equal to int_y then if statement should be True, so the statement inside the if block should execute, otherwise, the else part should.

Python code:

#A demo of Python equal to operator

int_x = 20

int_y = 30



if (int_x != int_y):

    print ("Yes, both are not equal!")

else:

    print ("Equal")

Output:

Yes, both are not equal!

As the values of both objects are not equal so condition became True.

Comparison of the string objects example

In the following example, a string object is created and compared by using the != (not equal to) operator in the if statement.

#A demo of Python not equal to operator

str_tst = 'Python is Cool'


if (str_tst != 'Python is Cool'):

    print ("String are different!")

else:

    print ("Strings are same, so condition is False!")

Output:

Strings are same, so condition is False!

A demo of equal to (==) operator with for loop

In this example, the Python equal to operator (==) is used in the if statement. A range of values from 2000 to 2030 is created. This is followed by using a for loop to iterate through that range.

The purpose is to get the leap years from 2000 to 2030 and omit all other years from displaying.

For that, the if statement is used and the current item in the range is divided by 4.

If it is equal to 0, then if the condition becomes True and print function will display the year. Otherwise, the current item is omitted by using the continue statement.

See how equal to (==) operator is used:

#A Demo of equal to operator

for a_yr in range(2000, 2030):
   if a_yr % 4 == 0:
      print("This year is a Leap Year: ", a_yr)
      continue

Output:

Python equal to

An example of getting even numbers by using not equal operator

For this example, a while loop is used, and in the if statement not equal to (!=) operator is used to get the even numbers. Have a look:

#Example of not equal operator with while loop

int_y = 30



while int_y <= 50:            

   int_y = int_y + 1

   if int_y%2 != 0:

      continue

   print (int_y)

Output:

not equal even

You can see, in the while loop, the less than or equal to operator (<=) is also used.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!