DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Updated on • Originally published at flexiple.com

How to use 'not equal' operator in Python?

In this tutorial, we learn how to use the not equal operator in Python. This is one of the many comparison operators in Python, like greater than, less than, equal to, etc. We can use them with conditional statements like if, if-else, while, do-while, etc.

Table of contents

  1. What does the not equal operator do in Python?
  2. Syntax of the not equal operator in Python
  3. Using the not equal operator
  4. Using not equal operator for comparison
  5. Using not equal operator with if statement
  6. Using not equal operator with while loop
  7. Closing thoughts

What does the not equal operator do in python?

The not equal is a comparison operator used to check if the first variable is not equal to the second variable.

It has two return values. If the variables are not equal, it returns TRUE otherwise FALSE. Both value and data type of the variables are considered while returning TRUE or FALSE.

Syntax of the not equal operator

The not equal operator can be written in two ways:

1: <>

Removed in Python 3, but was widely used in Python 2. It was taken from Python's predecessor- ABC programming language. It caused a lot of syntax error and therefore, was removed.

Syntax:

a <> b
Enter fullscreen mode Exit fullscreen mode

2: !=

More commonly used because it is supported in both Python 2 and Python 3.

Syntax:

a != b
Enter fullscreen mode Exit fullscreen mode

Some fonts might change != to ≠, does not really mean anything.

Using not equal operator

While using not equal operator, it is important to make sure what the data type of the variables is. For example, below you can see two variables with integer value 2.

Input:

a = 2

b = 2

if (a != b):

print("a is not equal to b")

else

print("a is equal to b")
Enter fullscreen mode Exit fullscreen mode

Since "a" and "b" are equal, the condition is FALSE and the block of code will be skipped and the else block of code will be executed.

Output:

a is equal to b
Enter fullscreen mode Exit fullscreen mode

Now, in another case, if both variables have the value 2 but have different data types. One is an integer and the other is a string.

a = 2

b = "2"

if (a != b):

print("a is not equal to b")

else

print("a is equal to b")
Enter fullscreen mode Exit fullscreen mode

This time, "a" has integer value 2 while "b" has string value 2. Both variables are not equal, therefore satisfying the condition. Hence, the block of code is executed.

Output:

a is equal to b
Enter fullscreen mode Exit fullscreen mode

Using not equal operator for comparison

The not equal operator can be used to compare variables. If the variables are not equal, it will print TRUE and if the variables are equal, it will print FALSE.

Input:

a = 5

b = 5

c = 7

print(a != b)

print(a != c)
Enter fullscreen mode Exit fullscreen mode

Output:

FALSE

TRUE
Enter fullscreen mode Exit fullscreen mode

Since, "a" and "b" are equal, a != b is FALSE and "a" and "c" are not equal, a != c is TRUE.

We can also use the not equal operator to compare more than two variables at the same time.

Input:

a = 3

b = 4

c = 5

if (a != b and c) :

print("a is not equal to both b and c")
Enter fullscreen mode Exit fullscreen mode

The block of code will be executed as "a" is not equal to both "b" and "c". If "a" was equal to either "b" or "c", then the block of code would not have run.

Output:

a is not equal to both b and c
Enter fullscreen mode Exit fullscreen mode

Another Example:

a = 3

b = 4

c = 5

if (a != b or c) :

print("all three variables are not equal")
Enter fullscreen mode Exit fullscreen mode

Here, the block of code will be executed if either "b" or "c" is not equal to "a".

Output:

all three variables are not equal
Enter fullscreen mode Exit fullscreen mode

Using not equal operator with if statement

The not equal operator is used with the IF statement as a condition. If the condition is satisfied, the block of code is executed, otherwise, the block of code is skipped.

Input:

a = 15

if (a%2 != 0):

print(a, "is an odd number")
Enter fullscreen mode Exit fullscreen mode

Since the remainder of the variable when divided by 2 is not equal to 0, it satisfies the if condition and the block of code are executed.

Output:

15 is an odd number
Enter fullscreen mode Exit fullscreen mode

Using not equal operator with while loop

The not equal operator is used with the while loop as a condition. The block of code will be executed until the condition is TRUE.

Input:

a = 1

while (a != 5):

print(a)

a += 1
Enter fullscreen mode Exit fullscreen mode

Since the variable is not equal to 5, the loop will continue till "a" is equal to "5".

Output:

1

2

3

4
Enter fullscreen mode Exit fullscreen mode

Closing thoughts

We have learnt how to use the not equal operator in Python. It returns boolean values opposite to the equal operator. The data type of the variable is also considered while using this operator. Python also has other comparison operators like equal, greater than, less than etc.

P.S. If you are looking to hire a developer for your team and are not sure how to go about with writing a job description, check out the following pages for easy tips:

  1. Flutter JD
  2. Backend JD

Top comments (0)