×

Python Not Equal Operator

Python provides us with many operators to make tasks easier. There are about 7 categories of operators in Python. One of the 7 classifications is the comparison operators. Just as the name suggests, these operators are used to compare the values of two operands. These are the same operators we use in mathematics:

  1. Greater than - >
  2. Less than - <
  3. Greater than or equal to - >=
  4. Less than or equal to - <=
  5. Equals - ==

The one new operator is the "not equal to" operator. It is the opposite of equal to operator. How interesting is that? This article discusses the "not equal to" operator with examples.

Operator: ! =

Name: Not equal to

Type: Comparison operator

Syntax: value 1! = value 2

Return Type: Boolean

Return value:  If value1 and value2 on either side of the operator are not equal, True will be returned; else, False will be returned if both the values are equal.

Example programs:

1:

a = int (input ("Enter the first number: "))
b = int (input ("Enter a number to compare: "))
print (a != b)

Output:

Case 1:

Enter the first number: 3

Enter a number to compare: 3

False

Case 2:

Enter the first number: 4

Enter a number to compare: 8

True

Understanding:

The program takes input for two numbers to use the operator. If the user gives the same input for both the numbers, the operator returns False, and when different inputs are given, it returns true.

#2:

As a condition:

a = int (input ("Enter the first number: "))
b = int (input ("Enter a number to compare: "))
res = a != b
if (res == True):
    print ("The values are not equal")
else:
    print ("The values are not equal")

Output:

Case 1:

Enter the first number: 3

Enter a number to compare: 2

The values are not equal

Case 2:

Enter the first number: 3

Enter a number to compare: 3

The values are equal

Understanding:

In the above program, we compared two numbers, stored the result in res, and used it as a condition in the if statement to print if the two values are equal.

#3 Data type variation

a = int (input ("Enter the first number: "))
b = input ("Enter a number to compare: ")
res = a != b
if (res == True):
    print ("The values are not equal")
else:
    print ("The values are not equal")

Output:

Case 1:

Enter the first number: 4

Enter a number to compare: 4

The values are not equal

Understanding:

The input is the same for both values. But why is it false? It is because, when taking the input for the second number, int is not mentioned. Hence, the first number is an integer, and the second number is a string. An integer is not equal to a string.

  • Even if we take the same number, if both the numbers are of different data types, they are not considered equal.
  • If multiple comparison operators are used, there is no proper rule for precedence and associativity. Unless the expression is well-defined with parenthesis, it is evaluated from left to right by breaking it down into pieces.

If we say 2! = 3! = 4

We get true: It compares if the three values are equal.

But if we say:

X < Y < Z

This expression neither means (X < Y) < Z nor X < (Y < Z). It just means X < Y and Y < Z. By default, it will be evaluated from left to right.

Examples:

#Operator overloading
class not_eq :
    def __init__ (self, num):
        self. num = num
    def __ne__ (self, a):
        if (type (a. num) != type (self. num)):
            return True
        if self. num != a. num:
            return True
        else:
            return False
obj1 = not_eq (int (input ("Enter number 1: ")))
obj2 = not_eq (int (input ("Enter number 2: ")))
print (obj1 != obj2)

Output:

Enter number 1: 3

Enter number 2: 2

True

Understanding:

In the program, we created a class, “not_eq," in which we wrote our magic method __ne__ () -> the predefined function that gets invoked by default when we call the! = operator anywhere in the program. We created two objects with values taking inputs from the user and used the! = operator. By overloading the operator, we can now compare two objects.


Related Topics

YOLO Python

YOLO means you only look once.It is a technique to perform object detection is called YOLO. It is the algorithmused by the program to identify items in the image. Earlier detection...

4 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

Python List extend() method

Python List extend() method The list.extend() method extends the list by appending all the items from the iterable. Syntax list.extend(iterable) Parameter iterable: It is a required parameter which represents any iterable unlike list, set, tuple, etc. Example 1 # Python...

1 minute read.

XGBoost for Regression in Python

Regression problem results real values. Decision Trees and Linear Regression are regularly used regression algorithms and use some metrics involved in regression like mean squared error and root mean squared...

5 minutes read.

Python Compiler

What is Compiler? The compiler is mainly a program used to convert the source code into the machine or binary code. The source code is generally a computer program written using...

6 minutes read.

Rank Based Percentile GUI Calculator using PyQt5 in Python

PyQt5: PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With the capabilities...

3 minutes read.

Python Set symmetric_difference_update() method

Python Set symmetric_difference_update() method The set.symmetric_difference_update() method in Python updates the original set by removing items that are present in both sets and inserting the other items of the set. Syntax set.symmetric_difference_update(set1) Parameter set- This parameter represents the first...

2 minutes read.

How to Add Elements in List in Python?

How to Add Elements in List in Python We all are aware of the wide spectrum of applications where the data structures of Python are used. Be it lists, tuples, or...

4 minutes read.

Iterate a Dictionary in Python

In this tutorial, we will learn how to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to store data values,...

3 minutes read.

Python Online Compiler

Compose, Run and Offer Python code internet involving OneCompiler's Python online compiler free of charge. It's one of the hearty, highlight-rich web-based compilers for python language, supporting both the adaptations, which...

3 minutes read.

What is Collaborative Filtering in ML, Python

Introduction Contents recommendation is a useful tactic for almost any specific technology looking to increase interest, but it frequently calls for a lot of user data and perhaps laborious content tagging...

3 minutes read.

Python Set difference() Method

Python Set difference() Method The set.difference() method in Python returns the set difference of two sets(A-B). Syntax set.difference(set1) Parameter set- This argument represents a set (minuend) set1- This arguments represents a set(subtrahend) Return This method returns the difference of the two specified...

2 minutes read.

Python If-else statement

In real life, there are situations where we have to make decisions for a particular circumstance and based on those decisions, and we plan our next move. The same thing...

4 minutes read.

Function annotations() in Python

What is Function Annotations? Function annotations provide a way related to different parts of a function at compile time with arbitrary Python expressions. It doesn’t have life in Python runtime execution....

3 minutes read.

Python md5() function

Python md5() function The md5() function in PHP calculates the md5 hash of a string. Syntax md5 ( string $str [, bool $raw_output] ) Parameter str(required)- This parameter specifies the string. raw_output(optional)- This parameter specifies a hex or binary output format: TRUE - Raw 16 character binary...

1 minute read.

Is Python Case Sensitive

Case sensitivity is the mode of dealing with the written alphabet. The cases of the alphabet are examined and based on these words are being treated. The uppercase and lowercase...

3 minutes read.

Python String Lowercase

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

App Config Python

An XML file called App. Config serves as the document for any programme. In other terms, you can modify any configuration inside of it without going to edit the code...

7 minutes read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

Python PPTX

Python-pptx is a python library that enables the user to create and update PowerPoint (.pptx) presentations. It is used to create modified PowerPoint presentations from the db content that can...

10 minutes read.