Skip to content

Python Not Equal Operator With Examples Gottumukkala Sravan Kumar Spark By {Examples}

  • by

The Not Equal operator (!=) in python is used to check if two values are not equal. If they are not equal, True is returned, otherwise False is returned. We can use this operator in conditional statements, and looping statements like for, while loop etc. We can also use this operator to compare different values in a List, Set, Tuple, or Dictionary.

To evaluate compound expressions/Boolean expressions Not Equal operator can be utilized. This operator is quite opposite to the Equal operator (==). In this article, we will see how to use the Not Equal operator inside if statements, While loop and integer, and string variables.

Note Equal OperatorDescription!=Works in both Python 2 and Python 3.<>Works only in Python 2. It is deprecated in Python 3.Python not equal operator

1. Quick Examples of using Not Equal Operator

Let’s quickly see the examples below to understand using the Not Equal operator in different scenarios. If you are already working on Python, you can quickly look into it. For a better understanding of each example, please go through the entire article.

# Quick Examples of Not Equal operator.

# Example 1: Not Equal operator with integer variables
ravi = 22
shyam = 20
sravan = 22
print(“Ages of three students:”)
print(ravi,shyam,sravan,sep=”n”)
print(ravi != sravan)
print(ravi != shyam)

# Example 2: Not Equal operator with strings
string_1 = “ravi”
string_2 = “shyam”
string_3 = “ravi”
print(“Three students:”)
print(string_1,string_2,string_3,sep=”n”)
print(string_1 != string_2)
print(string_1 != string_3)

# Example 3: Not Equal operator in if statement
if(ravi != 20):
print(“Age is ravi is not equal to 20”)

# Example 4: Not Equal operator in Compound statements
string_1 = “Python”
if(len(string_1) == 6 != 5):
print(“Length of the string is 6”)

# Example 5: Not Equal operator in While loop
# Set initial value to 5
value = 5
# Use while loop to display the string till the value becomes 0.
while (value != 0):
print(“sparkby”)
value -= 1

2. Not Equal operator Usage in Python

Like any other programming language Python Not Equal (!=) operator is used to checking the provided values/expressions are False or not. If they are not equal, False is returned, otherwise True is returned. From Python-3, we need to use this != operator. In Python 2 the operator used for Not Equal is “<>”, and this is deprecated in Python 3.

2.1 Not Equal operator Syntax

Let’s see how to specify the Not Equal operator.

# Syntax of != Operator
value1 != value2

# For expressions:
(Expression1) != (Expression2)

3. Usage of Not Equal Operator with Examples

Example 1: Consider the age of three students and compare the ages of each of them.

# Consider the age of three students
ravi = 22
shyam = 20
sravan = 22

print(“Ages of three students:”)
print(ravi,shyam,sravan,sep=”n”)
print(ravi != sravan)
print(ravi != shyam)

Yields the output:

Here, first, we compared the ages of ravi and sravan. ravi’s age is 22 and sravan’s age is 22, which are the same hence, the != operator returned False. Then we compared the ages of ravi and shyam where ravi’s age is 22 and shyam’s age is 20, which are not the same hence, the != operator returned True.

Example 2: Consider the names of three students and compare the names.

# Consider the names of three students
string_1 = “ravi”
string_2 = “shyam”
string_3 = “ravi”

print(“Three students:”)
print(string_1,string_2,string_3,sep=”n”)
print(string_1 != string_2)
print(string_1 != string_3)

Yields the output:

Here, values “ravi” and “shyam” are not the same, so True is returned. and in the second example both values are the same, so False is returned by the != operator.

4. Not Equal with Conditional statement

Typically the not equal operator is used with the conditional statement like if. In the below example, first, we assign value 22 to ravi variable (consider the age of ravi is 22) and check if ravi is not equal to 20. This condition becomes true and the body of the if statement is executed.

# Consider the age of student
ravi = 22

if(ravi != 20):
print(“Age is ravi is not equal to 20”)

# Output:
# Age is ravi is not equal to 20

Here, As ravi’s age is not equal to 20, != return True. So the condition inside the if statement will become True and the statement inside the if statement is executed.

Let’s look at another example in which we will compare the string length.

string_1 = “Python”

if(len(string_1) == 6 != 5):
print(“Length of the string is 6”)

# Output:
# Length of the string is 6

The number of characters in the string “Python” is 6, which is not equal to 5. So the condition becomes True.

5. Not Equal with a While loop

While working with the while loop we have to use the expression when to exists the loop, and the expression could be with != operator sometimes. In the below example, we will iterate the loop until the condition value != 0. 5 Variable value starts with the initial value 5, and decrement the value by 1 for every iteration. This loop exists when the condition becomes false.

# Set initial value to 5
value = 5

# Use while loop to display the string till the value becomes 0.
while (value != 0):
print(“Sparkby-Examples”)
value -= 1

Yields the output:

6. Conclusion

We have seen how to use the not equal operator to compare integers, and strings and use it on conditional statements and while loop with examples. The not equal operator (!=) in python is used to check if two values are not equal. If they are not equal, True is returned.

.

 The Not Equal operator (!=) in python is used to check if two values are not equal. If they are not equal, True is returned, otherwise False is returned. We can use this operator in conditional statements, and looping statements like for, while loop etc. We can also use this operator to compare different values  Read More Python, Python Tutorial, Python Operators 

Leave a Reply

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