 English French Spanish German
Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
Share on:

# How to Use Python Not Equal and Equal Operators Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

In Python, you can use the not equal to and the equal to operators to check if two Python objects have the same value. This tutorial will teach you how to use these operators with lots of example code.

In this tutorial you’ll learn:

• the syntax of the not equal (`!=`) operator and use cases,
• the syntax of the equal operator (`==`) with examples, and
• the use of `is` and `is not` operators to check for the identity of any two Python objects.

Let’s get started.

## Python Not Equal Operator Syntax

For any two Python objects `obj1` and `obj2` , the general syntax to use the not equal operator is:

``<obj1> != <obj2>``
• returns `True` when the values of `obj1` and `obj2` are not equal, and
• returns `False` otherwise.

Note: As mentioned above, `obj1` and `obj2` can be integers, floating point numbers, strings, lists and so on.

## Python Not Equal Operator Code Examples

In this section, let’s code a few examples to understand the not equal operator better.

### Using Python Not Equal Operator for Comparison

Here’s our first example.

``````num1 = 27
num2 = 3*9

num1 != num2

# Output: False``````

You can run the code examples on the Geekflare Python IDE—right from your browser. Or you could choose to run on your local machine.

As `num1 = 27`, and `num2` also evaluates to `27` (`3*9 = 27`), the values of `num1` and `num2` are equal. So the `!=` operator returns `False`.

Let’s take another example.

In the code below, `num1` is set to 7. And `num2` is set to the string 7. As they’re of different data types, the not equal operator returns `True`.

``````num1 = 7
num2 = "7"
num1 != num2

# Output: True``````

You cast the string to an integer, as shown:

``````num1 = 7
num2 = int("7")

num1 != num2

# Output: False``````

In this case, you can see that the returned result is `False`—as `num1` and `num2` are now equal to the integer 7.

You can also use the not equal operator with Python collections such as lists, tuples, and sets.

Note: For collections of data such as lists, the not equal operator works by checking the values of individual items. For example, two lists `list1` and `list2`—each of length `n`—are equal only if `list1[i] == list2[i]` for `i` in `{0,1,2,3,..n-1}`.

Here’s an example:

``````list1 = [2,4,6,8]
list2 = [2,4,6,9]

list1 != list2

# Output: True``````

In the above example, `list1` and `list2` differ by only one element. And the not equal `!=` operator returns `True` as expected.

### Using Python Not Equal Operator in Conditionals

You’ll often use the not equal to operator as part of Python conditionals.

For example, the code snippet below shows how you can you can check whether or not a number is odd.

A number that is not evenly divisible by 2 is odd. And this reduces to the condition `num%2 != 0`.

``````num = 7
if(num%2 != 0):
print("The number is odd.")
else:
print("The number is even.")

# Output: The number is odd.``````

You can also use conditionals in list comprehensions when you want to retain only those list elements that meet a specific condition. In the example below, `odd_10` is the list of all odd numbers less than 10.

``````odd = [num for num in range(10) if num%2 != 0]
print(odd)

# Output: [1, 3, 5, 7, 9]``````

And that completes our discussion of the not equal (`!=`) operator.✅

As you might have guessed by now the equal to operator produces the opposite effect of of the not equal to operator.

## Python Equal Operator Syntax

Here’s the syntax to use Python’s equal to operator:

``<obj1> == <obj2>  #where <obj1> and <obj2> are valid Python objects``
• returns `True` when the values of `obj1` and `obj2` are equal, and
• returns `False` otherwise.

## Python Equal Operator Code Examples

The equal operator (`==`) can be used very similarly to the not equal operator.

Let’s code the following examples:

• to check if two strings are equal,
• to check if a number is even, and
• to use conditionals in list comprehension

### Using Python Not Equal Operator for Comparison

In the code snippet below, `str1` and `str2`are equal in terms of value. So the equal operator (`==`) returns `True`.

``````str1 = "coding"
str2 = "coding"

str1 == str2

# Output: True``````

Let’s now use the equal operator in a conditional expression.

Note: A number that is evenly divisible by 2 is even. And in code, this reduces to the condition `num%2 == 0`

``````num = 10
if(num%2 == 0):
print("The number is even.")
else:
print("The number is odd.")

# Output: The number is even.
``````

Let’s now build on this example, use Python’s list comprehension to get all even numbers less than 10.

``````even_10 = [num for num in range(10) if num%2 == 0]
print(even_10)

# Output: [0, 2, 4, 6, 8]``````

In the above example,

• `range(10)` returns a range object which can be looped through to get all integers from 0 to 9.
• The condition `num%2 == 0` is `True` only for even numbers.
• So `even_10` is the list of all even numbers less than 10.

So far you’ve learned how to to check for equality using the not equal (`!=`) and equal (`==`) operators.

In the next section, you’ll learn how to verify the identity of two objects. You’ll check if two Python objects are identical.

## How to Use Python’s is and is not Operators

If you are a beginner in Python programming, it’s possible that you’re confused between the `==` and `is` operators. Let’s clarify that in this section.

In the previous section, we had an example where `str1` and `str2` where equal and the `==` operator returned `True`.

Now run the following code snippet.

``````str1 = "coding"
str2 = "coding"

str1 is str2

# Output: False``````

You can see that `str1 is str2` returns `False`.

Let’s take a step back and understand what Python’s `is` operator does.

The `is` operator operates on on any two Python objects.
And returns `True` only if the two objects are identical—that is they refer to the same object in memory.

Even though `str1` is equal to `str2`, `str1` is not `str2` as they point to two different objects in memory. And therefore, they have different identities.

In Python, you can use the `id()` function to get the identity of the object.

▶ Run the following code cell to get the identities of `str1` and `str2`.

``````id(str1)

# Sample output: 139935398870320

id(str2)

# Sample output: 139935398871344``````

As you can see, `str1` and `str2` have different identities. And `str1 is str2` returns `False` as expected.

Putting it together,

``````<obj1> is <obj2> # returns True if and only if
id(<obj1>) == id(<obj2>) # returns True``````

Let’s quickly verify this, as shown:

``````str1 = "coding"
str2 = str1

print(str1 is str2)
print(id(str1) == id(str2))

# Output
True
True``````

Intuitively, the `is not` operator does the opposite of the `is` operator.

The `is not` operator operates on on any two Python objects.
And returns `False` only if the two objects are identical—that is they refer to the same object in memory. Otherwise, it returns `True`.

In the above code examples, try replacing `is` with `is not` and check the results.

### Conclusion 👩‍💻

Hope you found this tutorial helpful.

To summarize, you’ve learned:

• how to use the equal (`==`) and not equal (`!=`) operators to check if two Python objects have the same value,
• the difference between equality and identity of Python objects, and
• how Python’s `is` and `is not` operators help in checking if two Python objects are identical.

See you all in the next tutorial. Until then, happy learning and coding!🎉