Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is operation of <> in Python?
The <> operator was a comparison operator in Python 2 used to check if two values are not equal. This operator has been removed in Python 3 and replaced with != for the same functionality.
Syntax
Python 2 (Deprecated)
variable1 <> variable2
Python 3 (Current)
variable1 != variable2
Both operators return True if the values are not equal, and False if they are equal.
Python 2 Example (Historical)
In Python 2, the <> operator worked as follows ?
# Python 2 only a = 10 b = 20 print(a <> b) # Returns True (10 is not equal to 20)
True
Python 3 Equivalent Using !=
In Python 3, we use the != operator for the same functionality ?
a = 10
b = 20
result = (a != b)
print("10 != 20:", result)
# Same values comparison
x = 5
y = 5
result2 = (x != y)
print("5 != 5:", result2)
10 != 20: True 5 != 5: False
Using != with Different Data Types
The != operator works with various data types ?
# Lists
numbers1 = [1, 2, 3, 4, 5]
numbers2 = [1, 2, 3, 4, 5]
numbers3 = [1, 2, 3, 4, 6]
print("List comparison:")
print(numbers1 != numbers2) # False (identical lists)
print(numbers1 != numbers3) # True (different lists)
# Strings
name1 = "Python"
name2 = "Java"
print("\nString comparison:")
print(name1 != name2) # True (different strings)
List comparison: False True String comparison: True
Error When Using <> in Python 3
Attempting to use the <> operator in Python 3 results in a syntax error ?
# This will cause a SyntaxError in Python 3 a = 10 b = 20 c = (a <> b) # Invalid syntax print(c)
File "<string>", line 3
c = (a <> b)
^
SyntaxError: invalid syntax
Migration from Python 2 to Python 3
| Python 2 | Python 3 | Status |
|---|---|---|
<> |
!= |
Replaced |
!= |
!= |
Still supported |
Conclusion
The <> operator was deprecated in Python 3 and replaced with !=. Always use != for "not equal" comparisons in modern Python code. The functionality remains identical, returning boolean values based on inequality comparison.
