Updated April 17, 2023
Introduction to Python not equal to operator
In this article, we will discuss a Python not equal to an operator. In general, the operator’s concept in any programming language is used to perform any logical or arithmetic operations on defined variables and their values, and this operator is known as the comparison operator. This not equal to the operator is a special symbol in Python that can evaluate logical or arithmetic expressions. This is usually represented as “ != ” and “ is not ” for operating this not equal to operator. In Python, the values that this not equal to the operator operates on is known as an operand. This not equal to the operator is exactly the opposite of the equal to the operator. This returns true when the values of operands on each side do not match or are not equal; otherwise, it will return false.
Working of not equal to operator in Python with syntax
In this section, we will see the syntax and examples of not equal to the operator in Python. This operator is used to compare the two values, which returns true if both the operand values are not equal at both the side of the operator but have the same type, else it returns false. Hence the not equal to the operator is a comparison operator and is denoted as “!=”. Now let us in detail in the below section.
Syntax:
opr1 != opr2
or
if…else (opr1 is not equal opr2)
opr1: Any valid operand or object.
opr2: Any valid operand or object.
This statement or expression will return Boolean values such as “True” or “False”.
In the above syntax, we saw that we can define “not equal to” in two ways in Python using “!=” or “is not” in the expressions or conditions and returns the Boolean value “True” or “False” having operands with the same type on both sides of the operator.
Examples of Python not equal
Now let us see an example to demonstrate the not equal to the operator (!=).
Example #1
Code:
print("Program to demonstrate not equal to (!=) operator is as follows:")
x = 5
print("The first operand with its value is as follows:")
print(x)
y = 3
print("The second operand with its value is as follows:")
print(y)
print("The manipulation after applying not equal to operator on above operands is as follows:")
print(x != y)
Output:
In the above program, we can see we have declared two variables, x and y, which are considered as operands with values 5 and 3. We can see we are trying to print the result of the expression x != y, which will print “True” as both the given operands are not equal.
To see the above program to have the output as “False”, then we need to define the values of two operands x and y with the same value. So we can see the screenshot below.
In Python, when we are using not equal to operator, then we have to note that the two operands that are declared must be of the same data type; else in Python, if both the operands have different data types, then it will return not equal. Let us demonstrate below with an example.
Example #2
Code:
print("Program to demonstrate not equal to operator with different data types of operands:")
x = 5
y = "5"
if ( x != y ):
print("x is not equal to y")
Output:
In the above program, we can see we have declared two variable, x as an int data type with value 5 and y as a string data type with value “5”. Therefore the not equal to operator prints, the operands have different values though the value is given is 5 to both operands where one is int, and the other is string type.
Now let us see an example where not equal to the operator can be defined as “is not” when using conditional loops like if … else. Let us demonstrate this with the below example.
Example #3
Code:
print("Program to demonstrate not equal to (is not) operator is as follows:")
x = 5
print("The first operand with its value is as follows:")
print(x)
if x is not 7:
print("not equal")
else:
print("equal")
Output:
In the above program, we can see that we have declared only one operand x with the value as 5. Then we are trying to check if x value “is not” equal to 7. In the program, we can see the expression given in the “ if ” condition using “is not” for comparing the values of the given operand with the value specified in the expression inside the if loop. So if this expression is satisfying, then it will print “not equal”; else, if the expression is not satisfying, then it will print “equal”. This result can be seen in the above screenshot.
In Python, the not equal to the operator is denoted by (!=) and is more recommended by developers and is supported by Python 2 and 3 versions. In Python, the older versions had another operator for comparing the not equal to the operator, which is denoted as ( < > ). But this operator is not supported in the latest version as well as Python 3, and the above version also does not support it. Now let us see the demonstration in the below example with the error that displays when the < > not equal to the operator in the program instead of ( != ).
Example #4
Code:
print("Program with not equal to operator <> in Python 2.7 version")
print("\n")
x = 4
y = 5
print(x<>y)
Output:
In the above program, we can see that we are using <> not equal to the operator which only works in Python 3.0 below versions and is not supported by the above Python 3.0 version, which will give syntax error as “ < >” this operator is deprecated.
Conclusions
In this article, we conclude that in Python, there are 3 ways to define not equal to the operator, such as “!= “, “is not”, and “<>”. In this article, we saw syntax and examples for each of the operators. We saw that using the “ !=” operator is the most and recommended operator for not equal to operator. We also saw this is a comparison operator that works when both operands are of the same type and return true if both values are not equal and false if both values are the same. We also saw in the if loop we use “is not” for checking equality between the values. Lastly, we saw <> not equal to operator example.
Recommended Articles
This is a guide to Python not equal. Here we discuss the Working of not equal to the operator in Python with syntax and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –