Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Difference between == and is operator in Python
Next article icon

Python NOT EQUAL operator

Last Updated : 14 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
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


Next Article
Difference between == and is operator in Python

D

ddeevviissaavviittaa
Improve
Article Tags :
  • Blogathon
  • Python
  • Blogathon-2021
  • Python-Operators
Practice Tags :
  • python
  • python-operators

Similar Reads

  • Python Operators
    In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. OPERATORS: These are the special symbols. Eg- + , * , /,
    6 min read
  • Precedence and Associativity of Operators in Python
    In Python, operators have different levels of precedence, which determine the order in which they are evaluated. When multiple operators are present in an expression, the ones with higher precedence are evaluated first. In the case of operators with the same precedence, their associativity comes int
    4 min read
  • Python Arithmetic Operators

    • Python Arithmetic Operators
      Python operators are fundamental for performing mathematical calculations. Arithmetic operators are symbols used to perform mathematical operations on numerical values. Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). OperatorDescriptionS
      5 min read
    • Difference between / vs. // operator in Python
      In Python, both / and // are used for division, but they behave quite differently. Let's dive into what they do and how they differ with simple examples. / Operator (True Division)The / operator performs true division.It always returns a floating-point number (even if the result is a whole number).I
      2 min read
    • Python - Star or Asterisk operator ( * )
      The asterisk (*) operator in Python is a versatile tool used in various contexts. It is commonly used for multiplication, unpacking iterables, defining variable-length arguments in functions, and more. Uses of the asterisk ( * ) operator in PythonMultiplicationIn Multiplication, we multiply two numb
      3 min read
    • What does the Double Star operator mean in Python?
      The ** (double star)operator in Python is used for exponentiation. It raises the number on the left to the power of the number on the right. For example: 2 ** 3 returns 8 (since 2³ = 8)It is one of the Arithmetic Operator (Like +, -, *, **, /, //, %) in Python and is also known as Power Operator. Pr
      2 min read
    • Division Operators in Python
      Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. There are two types of division operators: Float divisionInteger division( Floor division)When an in
      5 min read
    • Modulo operator (%) in Python
      Modulo operator (%) in Python gives the remainder when one number is divided by another. Python allows both integers and floats as operands, unlike some other languages. It follows the Euclidean division rule, meaning the remainder always has the same sign as the divisor. It is used in finding even/
      4 min read
    • Python Logical Operators

      geeksforgeeks-footer-logo
      Corporate & Communications Address:
      A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
      Registered Address:
      K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
      GFG App on Play Store GFG App on App Store
      Advertise with us
      • Company
      • About Us
      • Legal
      • Privacy Policy
      • In Media
      • Contact Us
      • Advertise with us
      • GFG Corporate Solution
      • Placement Training Program
      • Languages
      • Python
      • Java
      • C++
      • PHP
      • GoLang
      • SQL
      • R Language
      • Android Tutorial
      • Tutorials Archive
      • DSA
      • Data Structures
      • Algorithms
      • DSA for Beginners
      • Basic DSA Problems
      • DSA Roadmap
      • Top 100 DSA Interview Problems
      • DSA Roadmap by Sandeep Jain
      • All Cheat Sheets
      • Data Science & ML
      • Data Science With Python
      • Data Science For Beginner
      • Machine Learning
      • ML Maths
      • Data Visualisation
      • Pandas
      • NumPy
      • NLP
      • Deep Learning
      • Web Technologies
      • HTML
      • CSS
      • JavaScript
      • TypeScript
      • ReactJS
      • NextJS
      • Bootstrap
      • Web Design
      • Python Tutorial
      • Python Programming Examples
      • Python Projects
      • Python Tkinter
      • Python Web Scraping
      • OpenCV Tutorial
      • Python Interview Question
      • Django
      • Computer Science
      • Operating Systems
      • Computer Network
      • Database Management System
      • Software Engineering
      • Digital Logic Design
      • Engineering Maths
      • Software Development
      • Software Testing
      • DevOps
      • Git
      • Linux
      • AWS
      • Docker
      • Kubernetes
      • Azure
      • GCP
      • DevOps Roadmap
      • System Design
      • High Level Design
      • Low Level Design
      • UML Diagrams
      • Interview Guide
      • Design Patterns
      • OOAD
      • System Design Bootcamp
      • Interview Questions
      • Inteview Preparation
      • Competitive Programming
      • Top DS or Algo for CP
      • Company-Wise Recruitment Process
      • Company-Wise Preparation
      • Aptitude Preparation
      • Puzzles
      • School Subjects
      • Mathematics
      • Physics
      • Chemistry
      • Biology
      • Social Science
      • English Grammar
      • Commerce
      • World GK
      • GeeksforGeeks Videos
      • DSA
      • Python
      • Java
      • C++
      • Web Development
      • Data Science
      • CS Subjects
      @GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
      We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
      Lightbox
      Improvement
      Suggest Changes
      Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
      geeksforgeeks-suggest-icon
      Create Improvement
      Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
      geeksforgeeks-improvement-icon
      Suggest Changes
      min 4 words, max Words Limit:1000

      Thank You!

      Your suggestions are valuable to us.

      What kind of Experience do you want to share?

      Interview Experiences
      Admission Experiences
      Career Journeys
      Work Experiences
      Campus Experiences
      Competitive Exam Experiences