Skip to content

Python Not Equal Operator: A Guide

Python Not Equal Operator Cover Image

In this tutorial, you’ll learn how to use the Python not equal operator, !=, to evaluate expressions. You’ll learn how the not equal operator works in both Python 3 and the older Python 2. You’ll learn how the not equal operator is different from the not statement. You’ll also learn how to use the not equal operator in for loops, if statements, and while loops.

By the end of this tutorial, you’ll have learned:

  • What the Not Equal Operator is in Python
  • How to use the Not Equal Operator in Python
  • How to Include the Not Equal Operator in if Statements, For Loops, and While Loops

Let’s get started!

Python Not Equal Operator

The Python not equal operator is written as != and returns a boolean value evaluating if the expressions are not equal.

This means that the expression will:

  • Return True if the two expressions are not equal
  • Return False if the two expressions are equal

Let’s take a look at an example:

# A Basic Example of the Not Equal Operator
a = 1
b = 2
print(a != b)

# Returns: True

We can see that when we evaluate whether or not 1 is equal to 2. Because that’s not the case, the expression returns True.

The expression represents the inverse of the equality operator, ==. The equality operator checks for equality and will return a True if the values are equal to one another.

Python 2 Not Equal Operator

Python 2 had two not equal operators available:

  • <> is available only in Python 2
  • != is available in both Python 2 and Python 3

Because the <> operator has been deprecated in Python 3, to future proof your code (if you’re still running Python 2), it’s better to use !=.

Python Not Equal Operator Versus Not Keyword

One of the great things about Python is the ability to use plain-English words to write your code. It may seem that we can use the not equal operator in Python interchangeably with the not keyword. There are some key differences here, however.

Let’s take a look at an example:

# Comparing != and not in Python
a = 1
b = 2

print(a is not b)
print(a != b)

# Returns:
# True
# True

In this example, it may seem that these perform the same function. However, there are some nuanced differences.

  • is not evaluates whether two expressions point to the same place in memory
  • != evaluates whether two items are not equal to the same value

Understanding these differences can ensure that your code runs as smoothly as possible.

How to Use Python Not Equal in an If Statement

The Python not equal operator returns a boolean value, either True or False. Because of this, we can easily apply it to an if-else statement to make a decision in our code.

For example, we can write an if-else block to evaluate whether or not a value is a multiple of five. We can do this using the module operator, %:

# Using != In an if-else Block
value = 25
if value % 5 != 0:
    print('Is not a multiple of 5.')
else:
    print('Is a multiple of 5.')

# Returns: Is a multiple of 5.

Let’s break down what is happening here:

  1. The expression on the left is evaluated.
  2. Because 25 % 5 evaluates to 0, our expression turns into 0 != 0
  3. Since these two values are equal, the code moves to the else statement

How to Use Python Not Equal in a For Loop

The Python not equal operator != can also be used in a for loop. This allows you to evaluate every iteration of a for loop and perform an action in that loop. Let’s take a look at an example, where we can evaluate whether each item in a list is even or not:

# Using != in a Python For Loop
values = [1,2,3,4,5,6,7,7,8,9,10]
even = []
odd = []
for value in values:
    if value % 2 != 0:
        odd.append(value)
    else:
        even.append(value)

print(f'{even=}')
print(f'{odd=}')

# Returns:
# even=[2, 4, 6, 8, 10]
# odd=[1, 3, 5, 7, 7, 9]

Let’s break down what we did here:

  1. We instantiated three lists. One containing our values and two empty lists to hold our split up values.
  2. We then loop over each value in our list values
  3. We evaluate whether the result of the modulo 2 is not equal to 0.
  4. If it isn’t then the value is appended to the odd list
  5. Otherwise, the value is appended to the list even

How to Use Python Not Equal in a While Loop

Similarly, we can use the Python not equal operator in a Python while loop. A while loop is run until a condition is no longer equal to True. We can use the not equal operator to stop the flow of a while loop. Let’s see how this works:

# Using the Not Equal Operator in a Python While Loop
value = 0
while value != 4:
    print(f'Value equals: {value}')
    value += 1

# Returns:
# Value equals: 0
# Value equals: 1
# Value equals: 2
# Value equals: 3

We can see here that the while loop iterates until the value is equal to 4. We used the augmented assignment operator to increment the value by 1.

Multiple Conditions for Python Not Equal

In this final section, we’ll explore how to use multiple conditions when using the Python not equal operator. In many cases, this is referred to as compound conditions. These can be made with either the and & operator or the or operator, |.

Let’s first look at how compound conditions work with the & operator:

# Using a Compound & with the not equal Operator
a = 1
b = 2
c = 1

print(a != b & c)

# Returns: True

Let’s see what is happening here:

  • The expression evaluates if a is not equal to both b and c.
  • While in this case a is equal to c. However, a is not equal to b.
  • Because of this, the expression returns True since one value isn’t equal.

Now let’s run the same expression using the or operator, |:

# Using a Compound | with the not equal Operator
a = 1
b = 2
c = 1

print(a != b | c)

# Returns: True

This evaluates whether or not either of the values is not equal to the value on the left side. Because one of the values isn’t equal, it returns True.

Conclusion

In this tutorial, you learned how to use the Python not equal operator !=. You learned how the not equal operator is different in Python 2 and Python 3. You then learned how the operator compares to the not keyword in Python. Then, we looked at some practical examples using an if-else block, a for loop, and a while loop. Finally, you learned how to use the not equal operator using compound conditions.

Additional Resources

To learn more about related topics, check out the tutorial below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Tags:

Leave a Reply

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