Open In App

Python NOT EQUAL operator

Last Updated : 14 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see != (Not equal) operators. In Python,  != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. 

Python NOT EQUAL operators Syntax

The Operator not equal in the Python description:

  • != Not Equal operator, works in both Python 2 and Python 3.
  • <> Not equal operator in Python 2, deprecated in Python 3.

Syntax: Value A != Value B

Return Type:

  • Returns either True or False 

Note: It is important to keep in mind that this comparison operator will return True if the values are the same but are of different data types.

Examples of NOT EQUAL Operator in Python

Here are a few examples of Python NOT EQUAL operators.

Example 1: NOT EQUAL Operator with same DataType

In this example, we are comparing different values of the same datatype, that is integers to see how all values are does not equal Python and how the NOT EQUAL operator works.

Python3




A = 1
B = 2
C = 2
 
print(A!=B)
print(B!=C)


Output:

True
False

Example 2: NOT EQUAL operator with different DataTypes

In this example, we are comparing similar values of the different datatypes to see how the NOT EQUAL operator works. We are taking an integer, a float, and a Python String as input.

Python3




A = 1
B = 1.0
C = "1"
 
print(A!=B)
print(B!=C)
print(A!=C)


Output:

False
True
True

Compare lists in Python using the Not Equal Operator

Python NOT EQUAL operator can also be used to compare two lists. Let’s see how can this be done.

In this example, we are taking 3 Python lists, out of which two are integers and one is a string list. Then we compared them using the does not equal operator in Python.

Python3




list1 = [10, 20, 30]
list2 = [10, 20, 30]
list3 = ["geeks", "for", "geeks"]
 
print(list1 != list2)
print(list1 != list3)


Output:

False
True

Use of if statement with the Not Equal  operator in Python

The NOT EQUAL operator can also be used with the Python if else statements. Let us see a simple example of this.

In this example, we are comparing two strings and then printing a message based on the output of does not equal operator in Python.

Python3




str1 = 'Geeks'
str2 = 'GeeksforGeeks'
 
if str1 != str2:
    print("Strings are not Equal")
else:
    print("Strings are Equal")


Output:

Numbers are not Equal

Python NOT EQUAL Operator with Custom Object

We can also use the NOT EQUAL operator with custom objects in Python. Here is an example of how the does not equal Python operator works with custom objects.

The Python __ne__() decorator gets called whenever the does not equal Python operator in Python is used. We can override this function to alter the nature of the ‘not equal’ operator.

Python3




class Student:
 
    def __init__(self, name):
        self.student_name = name
 
    def __ne__(self, x):
        # return true for different types
        # of object
        if type(x) != type(self):
            return True
           
        # return True for different values
        if self.student_name != x.student_name:
            return True
        else:
            return False
 
s1 = Student("Shyam")
s2 = Student("Raju")
s3 = Student("babu rao")
 
print(s1 != s2)
print(s2 != s3)


Output:

True
True


Previous Article
Next Article

Similar Reads

Benefits of Double Division Operator over Single Division Operator in Python
The Double Division operator in Python returns the floor value for both integer and floating-point arguments after division. C/C++ Code # A Python program to demonstrate use of # &amp;quot;//&amp;quot; for both integers and floating points print(5//2) print(-5//2) print(5.0//2) Output: 2 -3 2.0 The time complexity of the program is O(1) as it conta
2 min read
Check if a value exists in a DataFrame using in &amp; not in operator in Python-Pandas
In this article, Let’s discuss how to check if a given value exists in the dataframe or not.Method 1 : Use in operator to check if an element exists in dataframe. C/C++ Code # import pandas library import pandas as pd # dictionary with list object in values details = { 'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'], 'Age
3 min read
Difference between != and is not operator in Python
In this article, we are going to see != (Not equal) operators. In Python != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. Whereas is not operator checks whether id() of two objects is same or not. If same, it returns False and if not same, it returns
3 min read
Python IF with NOT Operator
We can use if with logical not operator in Python. The main use of the logical not operator is that it is used to inverse the value. With the help of not operator, we can convert true value to false and vice versa. When not applied to the value so it reverses it and then the final value is evaluated by the if condition. So according to its final va
4 min read
not Operator in Python | Boolean Logic
Python not keyword is a logical operator that is usually used to figure out the negation or opposite boolean value of the operand. The Keyword 'not' is a unary type operator which means that it takes only one operand for the logical operation and returns the complementary of the boolean value of the operand. For example, if we give false as an oper
5 min read
Why 0.3 - 0.2 is not equal to 0.1 in Python?
In this article, we will see why 0.3 - 0.2 is not equal to 0.1 in Python. The reason behind it is called “precision”, and it’s due to the fact that computers do not compute in Decimal, but in Binary. Computers do not use a base 10 system, they use a base 2 system (also called Binary code). Below is the Implementation. C/C++ Code # code print(0.3 -
2 min read
Python3 Program to Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
3 min read
Difference between == and is operator in Python
When comparing objects in Python, the identity operator is frequently used in contexts where the equality operator == should be. In reality, it is almost never a good idea to use it when comparing data. What is == Operator?To compare objects based on their values, Python's equality operators (==) are employed. It calls the left object's __eq__() cl
3 min read
Operator Overloading in Python
Operator Overloading means giving extended meaning beyond their predefined operational meaning. For example operator + is used to add two integers as well as join two strings and merge two lists. It is achievable because '+' operator is overloaded by int class and str class. You might have noticed that the same built-in operator or function shows d
7 min read
Python | Operator.countOf
Operator.countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. Syntax : Operator.countOf(freq = a, value = b) Parameters : freq : It can be list or any other data type which store value value : It is value for which we have to count the number of occurrences Returns: Count of number of occ
2 min read
Practice Tags :
three90RightbarBannerImg