Not Equal operator in Python

In Python, the "not equal" operator is used to compare two values and determine whether they are not equal to each other. It is represented by the symbol !=. The result of the comparison is a Boolean value: True if the values are not equal, and False if the values are equal.

Numeric Comparison

x = 5 y = 10 result = x != y print(result) # Output: True

In this example, x is not equal to y, so the result is True.

String Comparison

name1 = "Willaim" name2 = "Mary" result = name1 != name2 print(result) # Output: True

Here, the strings "Alice" and "Bob" are not equal, so the result is True.

List Comparison

list1 = [1, 2, 3] list2 = [1, 2, 4] result = list1 != list2 print(result) # Output: True

In this case, the lists list1 and list2 are not equal because they have different elements at the third position.

Boolean Comparison

bool1 = True bool2 = False result = bool1 != bool2 print(result) # Output: True

Here, bool1 is not equal to bool2, so the result is True.

Mixed Data Types

number = 5 text = "5" result = number != text print(result) # Output: True

Even though the values number and text appear similar, they are of different types (integer and string), so the result is True.

Comparison with None

value = None result = value != 10 print(result) # Output: True

In this example, value is None, which is not equal to the integer 10.

Python Comparison Operators

A comparison operator , also called python relational operator, compares the values on both sides of the operator to classify the relation between them as either true or false .


How to use not equal operator in python

Conclusion

Remember that the "not equal" operator only checks for inequality between two values. If you want to check for both inequality and the data types being the same, you would need to use more complex comparisons, like using the != operator and additionally comparing the types using the type() function.