How to use 'not equal' operator in Python?
Tags: python, programming, tutorial, beginners
Published: 2022-03-15 07:40:30

In this tutorial, we learn how to use the not equal operator in Python. This is one of the many [comparison operators](https://flexiple.com/python/python-string-comparison/) in Python, like greater than, less than, equal to, etc. We can use them with conditional statements like if, if-else, while, do-while, etc.

### **Table of contents**

1. [What does the not equal operator do in Python?](#what-does-the-not-equal-operator-do-in-python)
2. [Syntax of the not equal operator in Python](#syntax-of-the-not-equal-operator)
3. [Using the not equal operator](#using-not-equal-operator)
4. [Using not equal operator for comparison](#using-not-equal-operator-for-comparison)
5. [Using not equal operator with if statement](#using-not-equal-operator-with-if-statement)
6. [Using not equal operator with while loop](#using-not-equal-operator-with-while-loop)
7. [Closing thoughts](#closing-thoughts)

## **What does the not equal operator do in python?**

The not equal is a comparison operator used to check if the first variable is not equal to the second variable.

It has two return values. If the variables are not equal, it returns TRUE otherwise FALSE. Both value and data type of the variables are considered while returning TRUE or FALSE.

## **Syntax of the not equal operator**

The not equal operator can be written in two ways:

1: **<>**

Removed in Python 3, but was widely used in Python 2. It was taken from Python&#39;s predecessor- ABC programming language. It caused a lot of syntax error and therefore, was removed.

#### **Syntax:**

```python
a <> b
```

2: **!=**

More commonly used because it is supported in both Python 2 and Python 3.

#### **Syntax:**

```python
a != b
```

Some fonts might change != to ≠, does not really mean anything.

## **Using not equal operator**

While using not equal operator, it is important to make sure what the data type of the variables is. For example, below you can see two variables with integer value 2.

#### **Input:**

```python
a = 2

b = 2

if (a != b):

print("a is not equal to b")

else

print("a is equal to b")
```

Since &quot;a&quot; and &quot;b&quot; are equal, the condition is FALSE and the block of code will be skipped and the else block of code will be executed.

#### **Output:**

```python
a is equal to b
```

Now, in another case, if both variables have the value 2 but have different data types. One is an integer and the other is a string.

```python
a = 2

b = "2"

if (a != b):

print("a is not equal to b")

else

print("a is equal to b")
```

This time, &quot;a&quot; has integer value 2 while &quot;b&quot; has string value 2. Both variables are not equal, therefore satisfying the condition. Hence, the block of code is executed.

#### **Output:**

```python
a is equal to b
```

## **Using not equal operator for comparison**

The not equal operator can be used to compare variables. If the variables are not equal, it will print TRUE and if the variables are equal, it will print FALSE.

#### **Input:**

```python
a = 5

b = 5

c = 7

print(a != b)

print(a != c)
```

#### **Output:**

```python
FALSE

TRUE
```

Since, "a" and "b" are equal, a != b is FALSE and "a" and "c" are not equal, a != c is TRUE.

We can also use the not equal operator to compare more than two variables at the same time.

#### **Input:**

```python
a = 3

b = 4

c = 5

if (a != b and c) :

print("a is not equal to both b and c")
```

The block of code will be executed as &quot;a&quot; is not equal to both &quot;b&quot; and &quot;c&quot;. If &quot;a&quot; was equal to either &quot;b&quot; or &quot;c&quot;, then the block of code would not have run.

#### **Output:**

```python
a is not equal to both b and c
```

#### **Another Example:**

```python
a = 3

b = 4

c = 5

if (a != b or c) :

print("all three variables are not equal")
```

Here, the block of code will be executed if either &quot;b&quot; or &quot;c&quot; is not equal to &quot;a&quot;.

#### **Output:**

```python
all three variables are not equal
```

## **Using not equal operator with if statement**

The not equal operator is used with the IF statement as a condition. If the condition is satisfied, the block of code is executed, otherwise, the block of code is skipped.

#### **Input:**

```python
a = 15

if (a%2 != 0):

print(a, "is an odd number")
```

Since the remainder of the variable when divided by 2 is not equal to 0, it satisfies the if condition and the block of code are executed.

#### **Output:**

```python
15 is an odd number
```

## **Using not equal operator with while loop**

The not equal operator is used with the while loop as a condition. The block of code will be executed until the condition is TRUE.

#### **Input:**

```python
a = 1

while (a != 5):

print(a)

a += 1
```

Since the variable is not equal to 5, the loop will continue till &quot;a&quot; is equal to &quot;5&quot;.

#### **Output:**

```python
1

2

3

4
```

## **Closing thoughts**

We have learnt how to use the not equal operator in Python. It returns boolean values opposite to the equal operator. The data type of the variable is also considered while using this operator. Python also has other comparison operators like equal, greater than, less than etc. 

P.S. If you are looking to hire a developer for your team and are not sure how to go about with writing a job description, check out the following pages for easy tips:
1. [Flutter JD](https://flexiple.com/flutter/job-description)
2. [Backend JD](https://flexiple.com/backend/job-description)