How To Use Not Equal To Operator in Python

Last Updated on May 15, 2024 by Roshan Parihar

To use the not equal to operator in Python, use the operator symbol != in your coding. It is the comparison operator to compare the values and get the result in boolean form.

Let’s find out with the examples given below.

How to Use Not Equals to Operator in Python

You can compare two variables in Python using the not equal to operator !=. It gives results in true or false as given below.

Example 1

Output

False
True

The above examples contain three variables and all are numeric. The first and second variables contain the same values while the third one is different. When you compare the values, you will get the result as given above.

Using if Condition to Compare

The operator can be used with the if conditional statement in Python. You can use the condition inside the loop or without the loop. This operator returns true when the two values are different or not the same.

Example 2

Output

N
a
t
i
n

The above example prints all the characters of the string that are not matching with “o”. Upon execution of each iteration of the loop, a single string character gets printed in a single line.