Not Equal (!=) Operator in Python

In this tutorial, we will learn what Not Equal (!=) operator is in Python and how to make use of this operator in multiple ways in Python.

Let’s get started!

Understanding Not Equal (!=) Operator

Whenever you come across the != operator, it means not equal to. The not-equal operator (!=) in Python that allows you to compare two values and determine whether they are equal or not equal. This operator is mainly used in conditional statements and loops to make decisions based on the values of various objects.

Also Read: if-elif-else statement in one single line in Python

Let’s understand better with an example if you have two variables a and b, the expression a != b will evaluate to True if a is not equal to b, and False otherwise.

Also Read: All types of loops in Python with examples

Code Implementation of the != Operator

Let’s start with understanding the basic syntax of the not equal operator in Python which is simply defined as follows: value1 != value2. Here the answer will be True if both values are unequal and it will be False otherwise.

Not Equal Operator with Numeric Values

When it comes to numeric values, the not-equal operator can be used directly without much thought as shown in the code below. We declare both numeric variables and print the value of the != operator value.

value1 = 45
value2 = 81
print(value1 != value2)

When the code is executed the output that gets displayed on the screen is True as value1 is not equal to value2.

Not Equal Operator with Strings Values

Even in the case of strings, the not-equal operator can also be used directly without any complication as shown below. We declare both string variables and print the value of the != operator value.

value1 = "abc"
value2 = "def"
print(value1 != value2)

When the code is executed the output that gets displayed on the screen is True as both strings hold different values.

Not Equal Operator with Lists Values

In this case, let’s declare two separate list values and then apply the not-equal operator on the lists to see what value gets displayed on the screen. Have a look at the code below.

l1 = [1,2,3]
l2 = [1,2,5]
print(l1 != l2)

Even though the two elements are equal, when the code is executed the output that gets displayed on the screen is True because two lists are different by a single element.

Chaining the Not Equal Operators

In case there are more than two variables ( as displayed in the various examples ), we might need to chain multiple, not equal operators to evaluate the values of multiple variables. Have a look at the code snippet as displayed below:

a = 35
b = 35
c = 56
print( a != b != c)

a = 5
b = 35
c = 56
print( a != b != c)

a = 35
b = 3
c = 35
print( a != b != c)

When we write a != b != c , then it means that all three values need to be different. If even two of the consecutive variables are equal ( as mentioned in the first case ) then the value becomes False. For the next situation as all three values are different the output is displayed as True. For the last situation, the variables a and c are equal but hence they are not consecutive, the output comes out as True.

Not-Equal Operator in Conditional Statements

Let’s take two variables x and y and assign some initial values to the variables. Next, we will apply conditional statements based on the two variables to validate if the variables are equal or not. Have a look at the code below:

x = 78
y = 45

if(x != y):
  print("Values are not Equal")
else:
  print("Values are Equal")

When we execute the code snippet mentioned above, as the values of both variables are unequal the output that’s displayed on the screen is Values are not Equal. The output is the expected output.

Usage of Not-Equal Operator in Ternary Operator

Lastly, we will learn how the != operator can be made to use in the Ternary Operations. If you aren’t aware of what Ternary Operator is then do check out the tutorial I have mentioned below.

Also Read: Ternary Operator in Python

The not-equal operator (!=) can be used in a ternary operator to achieve the creation of concise one-liner conditional statements. Have a look at the code implementation below. We will transform the previous if-else statements into ternary operator statements.

x = 78
y = 45

result = "Values are not Equal" if x!=y else "Values are Equal"
print(result)

The result, in this case, will be the same as the example mentioned earlier which is Values are not Equal.

Conclusion

Congratulations! In this tutorial, we learned what is not equal(!=) operator is and how one can make use of the same in various aspects of the code in Python programming. If you liked the tutorial and learned something today then do have a read at the following tutorials.

Also Read:

  1. The conceptual understanding of operators in Python
  2. What does the double slash // operator do in Python
  3. Actual Use of “>>” and “<<” operators in Python

Leave a Reply

Your email address will not be published. Required fields are marked *