Python Not Equal Operator

In Python, Comparison Not Equal Operator takes two operands and returns a boolean value of True if both the operands are not equal, else it the operands are equal then it returns False.

Syntax

The syntax to check if two values: a and b are not equal using Not Equal Operator is

</>
Copy
a != b

The above expression returns a boolean value.

Examples

1. Check if two numbers are not equal

In the following program, we take two numbers: a, b; and check if these two numbers are equal.

main.py

</>
Copy
a = 4
b = 5

if a != b :
    print('a and b are not equal.')
else :
    print('a and b not equal.')

Output

a and b are not equal.

2. Check if two strings are not equal

In the following program, we take two string values: a, b; and check if these two strings are not equal.

main.py

</>
Copy
a = 'apple'
b = 'apple'

if a != b :
    print('a and b are not equal.')
else :
    print('a and b are equal.')

Output

a and b are equal.

Conclusion

In this Python Tutorial, we learned about Comparison Not Equal Operator, its syntax, and usage, with examples.