Is there a "not equal" operator in Python?
Yes, in Python there is an operator for not equal (!=) . You can use it to compare two values and see if they are not equal to each other. For example:
x = 5
y = 3
if x != y:
print("x is not equal to y")
else:
print("x is equal to y")
This would output "x is not equal to y", because 5 is not equal to 3.
Additionally, there is a second way to express not equal comparison, this is with <>
operator. It was used in python 2 but it's no longer supported in python 3.
x = 5
y = 3
if x <> y:
print("x is not equal to y")
else:
print("x is equal to y")
This would output "x is not equal to y", because 5 is not equal to 3.
And of course, you can also use the equality operator ==
to check for equality.
x = 5
y = 3
if x == y:
print("x is equal to y")
else:
print("x is not equal to y")
This would output "x is not equal to y", because 5 is not equal to 3.