Home »
Python
Python operator.ne() Function with Examples
Python operator.ne() Function: Here, we are going to learn about the operator.ne() function with examples in Python programming language.
Submitted by IncludeHelp, on April 14, 2020
operator.ne() Function
operator.ne() function is a library function of operator module, it is used to perform "not equal to operation" on two values and returns True if the first value is not equal to the second value, False, otherwise.
Module:
import operator
Syntax:
operator.ne(x,y)
Parameter(s):
- x,y – values to be compared.
Return value:
The return type of this method is bool, it returns True if x is not equal to y, False, otherwise.
Example 1:
# Python operator.ne() Function Example
import operator
# integers
x = 10
y = 20
print("x:",x, ", y:",y)
print("operator.ne(x,y): ", operator.ne(x,y))
print("operator.ne(y,x): ", operator.ne(y,x))
print("operator.ne(x,x): ", operator.ne(x,x))
print("operator.ne(y,y): ", operator.ne(y,y))
print()
# strings
x = "Apple"
y = "Banana"
print("x:",x, ", y:",y)
print("operator.ne(x,y): ", operator.ne(x,y))
print("operator.ne(y,x): ", operator.ne(y,x))
print("operator.ne(x,x): ", operator.ne(x,x))
print("operator.ne(y,y): ", operator.ne(y,y))
print()
# printing the return type of the function
print("type((operator.ne(x,y)): ", type(operator.ne(x,y)))
Output:
x: 10 , y: 20
operator.ne(x,y): True
operator.ne(y,x): True
operator.ne(x,x): False
operator.ne(y,y): False
x: Apple , y: Banana
operator.ne(x,y): True
operator.ne(y,x): True
operator.ne(x,x): False
operator.ne(y,y): False
type((operator.ne(x,y)): <class 'bool'>
Example 2:
# Python operator.ne() Function Example
import operator
# input two numbers
x = int(input("Enter first number : "))
y = int(input("Enter second number: "))
# printing the values
print("x:",x, ", y:",y)
# comparing
if operator.ne(x,y):
print(x, "is not equal to", y)
else:
print(x, "is equal to", y)
Output:
RUN 1:
Enter first number : 10
Enter second number: 20
x: 10 , y: 20
10 is not equal to 20
RUN 2:
Enter first number : 10
Enter second number: 10
x: 10 , y: 10
10 is equal to 10