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.

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 “ravi” and “ravi” are the same, so False is returned by the != operator.

4. Not Equal with Conditional statement

First, we will consider the age if ravi i.e 22 and and check if his age is equal to 20 inside the if statements using the 1= operator.

# 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

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

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

operator.

string_1 = “Python”

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

# Output:
# Length of the string is 6

Number of characters in the string – “Python” is 6, which is not equal to 5. So the became True.

5. Not Equal with While loop

We will iterate the string – “Sparkby-Examples” 5 times by initializing the iterator to 5, != is utilized in the condition in which the iterator is compared with 0 each time. Finally, we display the string till the value becomes 0. Each time value is decremented using the decrement operator.

# 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. 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 *