How to use “not equal” operator in Python?

In this Python tutorial, you will learn how to use the “not equal” operator in Python.

Table Of Contents

Let’s dive into the tutorial.

“not equal” operator (!=) in Python

In Python, we will use != as the “not equal” operator. It is used to compare two expressions/operators and return True if they are not equal, otherwise, it will return False.

Syntax:

operand1 != operand2

where operand1 is the first operand and operand2 is the second operand.

Example:

In this example, we will apply the != operator on different operands.

# Check 34 is not equal to 56.
print(34 != 56)

# Check 34 is not equal to 34.
print(34 != 34)

# Check "thisPointer" is not equal to "python".
print("thisPointer" != "python")

Output:

True
False
True

We can see that

  1. 34 is not equal to 56, hence the output is True
  2. 34 is equal to 34, hence the output is False
  3. thisPointer is not equal to python, hence the output is True

If the variable is the same, but the data type of variable is different, In that case,

  1. Integer type and string type are different. So != returns True
  2. Integer type and double type are the same. So != returns False
  3. string type and double type are different. So != returns True

Example:

In this example, we will demonstrate the above scenario.

# check if 34 is not equal to '34'
print(34 != '34')

# check if 34 is not equal to 34.0
print(34 != 34.0)

# check if 34.0 is not equal to '34'
print(34.0 != '34')

Output:

True
False
True

In the above code, we are

  1. Comparing integer type 34 with string type 34
  2. Comparing integer type 34 with double type 34
  3. Comparing double type 34 with string type 34

“not equal” Operator ( != ) in conditional statements

If we want to compare any expressions using the not equal operator in conditional statements like if,if-else, then we can use it.

Syntax:

if(operand/expression != operand/expression):
    statements
    .........
    .........
elif(operand/expression != operand/expression):
    statements
    .........
    .........
else:
    statements
    .........
    ......... 

Example 1:

In this example, we will create two variables and compare the expressions with != operator.

# Create two variables
a=43
b=77

# check if a+b is not equal to a*b
# if true, assign a and b to 100
if(a+b != a*b):
    a=100
    b=100
    print(a,b)
else:
    # otherwise display a and b
    print(a,b)

Output:

100 100

In the above code, we are evaluating the expressions a+b and ab. If they are not equal, and the if block is executed if value of (a + b) is not equal to (a * b)*, otherwise else block is executed by displaying actual values of a and b.

We can see that if block is executed. Hence the output is 100 100

Example 2:

In this example, we will create two variables and compare the expression and an operand with != operator.

# create two variables
a=43
b=77

# check if a+b is not equal to 120
# if true, assign a and b to 100
if(a + b != 120):
    a=100
    b=100
    print(a,b)
else:
    # otherwise display a and b
    print(a,b)

Output:

43 77

In the above code, we are evaluating the expression a+b and compared it with 120. If they are not equal the the if block is executed and values of a and b are updated to 100. Otherwise the else block is executed by displaying the actual values of a and b.

We can see that else block is executed. Hence the output is 43 77.

“not equal” operator (!=) in Loops in Python

It is possible to use != operator with conditional statements under looping structures like for loop, while loop.

Syntax:

for iterator in range(sequence):
        if(iterator != operand/expression):
                statements
                .........
                .........
        elif(iterator != operand/expression):
                statements
                .........
                .........
        else:
                statements
                .........
                .........

Where, an iterator is used to iterate the sequence of elements inside for loop.

Example:

In this example, we are iterating elements from 20 to 30 inside for loop and if the iterator is not equal to 25, we are displaying the iterator.

# iterate elements from 20 to 30
for iterator in range(20,30):
    # If the iterator not equals to 25
    # then display the iterator
    if(iterator != 25):
        print(iterator)

Output:

20
21
22
23
24
26
27
28
29

We can see that 25 is not displayed.

“not equal” operator (!=) with Data Structures in Python

We can also use the != operator with the Python data structures like list, tuple, dictionary, and set. Elements inside these data structures can be compared using this != operator.

We can use for loop to iterate elements inside these data structures and conditional statements inside for loop. The != operator is also used in conditional statements.

Example:

In this example, we created a list and tuple with 5 string elements each and iterate these elements inside for loop and compare each value with != operator.

# Create list
list1=["welcome","to","thispointer","python","tutorial"]

# Create tuple
tuple1=("welcome","to","thispointer","python","tutorial")

# for loop to iterate elements inside list1
for iterator in list1:
    # Check if iterator is not equal to python
    if(iterator!="python"):
        print(iterator, end=" ")

print("")

# for loop to iterate elements inside tuple1
for iterator in tuple1:
    # Check if iterator is not equal to "tutorial"
    if(iterator != "tutorial"):
        print(iterator, end=" ")

print("")

Output:

welcome to thispointer tutorial 
welcome to thispointer python 

Summary

By the end of this tutorial, you must have noticed that the != operator had a vast range of applications. It can be used everywhere. So based on the requirement, you can use this not equal operator in Python. Happy Learning

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top