In this tutorial, we will learn about python not equal operator. We will understand python not equal with multiple examples and real use cases.so let’s get started.
Python not equal operator
In python, the not equal operator is used to compare the left side with the right side. It returns true if both sides are the same otherwise returns a false value.
Python not equal operator syntax
in python not equal to is represented by the below syntax
return type: True or False
Python not equal examples
In this session, we will understand how to use the not equal operator in python to perform different operations.
Compare two numbers using python not equal
Let’s use the not equal operator to compare two numerical values.
The above operations give the False as a result
Compare two strings using python not equal
Now let’s compare two strings using the not equal operator.
a = "jack"
b = "john"
a != b
The above operation gives True as output because both strings are not the same
Compare two lists using python not equal
Let’s create two lists and compare them using the not equal operator
a = [1,2,3]
b = [2,1,3]
a != b
The above operation returns True because both lists are not the same.
python not equal use cases
In this session, we will understand python not equal operator with many real-world use-case examples.
if not equal in python
In many cases, users need to check if condition and take the appropriate action. In this example, we will check if two numbers are equal using python not operator using if statement.
if a != b:
The above code will return Not equal as output because both the numbers are not equal.
while not equal in python
Similar to the if condition a user can use the python not equal operator in the while statement. Let’s take the below example.
a = 0
b = 2
while(a+b != 8):
a += 1
The above code snipped will check the sum of a+b and print the value of a as long as the sum is not 8. Once the values reach 8 the loop will break.
Python not true
Similar to not equal in python we have not operator which returns a boolean value(True or False).In python 1 means True and 0 means value.
Now let’s check the not operator in action with the below examples.
a = 0
b = not a
In the above example value of b is not a which is 1 as a digit. Since the not operator returns true or false so the corresponding boolean value is True
I hope you have liked this small tutorial on python not true with various examples. Please do let me know if you are facing any issues while following along.