Python 3 – Comparison Operators Example
Python is a very popular programming language that has gained immense popularity in recent years. It is an easy-to-learn language that comes with a wide range of features and libraries that can be used to develop complex applications. One of the essential features of Python is the comparison operators that allow developers to compare values and determine if they are true or false.
Comparison Operators
Python has six built-in comparison operators that can be used to determine if two values are equal or not, greater than or less than, or if they fall within a range. The six comparison operators are:
1. Equal to ()
The “” operator is used to check if two values are equal to each other. If the values are equal, then the operator returns true, and if they are not, it returns false.
# Example
x = 10
y = 10
print(x == y)
Output:
True
2. Not equal to (!=)
The “!=” operator checks if two values are not equal to each other. If the values are not equal, then the operator returns true, and if they are equal, it returns false.
# Example
x = 10
y = 20
print(x != y)
Output:
True
3. Greater than ( > )
The “>” operator checks if one value is greater than another. If the first value is greater than the second value, then the operator returns true, and if it is not, it returns false.
# Example
x = 10
y = 5
print(x > y)
Output:
True
4. Less than ( < )
The “<” operator checks if one value is less than another. If the first value is less than the second value, then the operator returns true, and if it is not, it returns false.
# Example
x = 5
y = 10
print(x < y)
Output:
True
5. Greater than or equal to ( >= )
The “>=” operator checks if one value is greater than or equal to another. If the first value is greater than or equal to the second value, then the operator returns true, and if it is not, it returns false.
# Example
x = 10
y = 10
print(x >= y)
Output:
True
6. Less than or equal to ( <= )
The “<=” operator checks if one value is less than or equal to another. If the first value is less than or equal to the second value, then the operator returns true, and if it is not, it returns false.
# Example
x = 5
y = 10
print(x <= y)
Output:
True
Conclusion
In conclusion, comparison operators in Python come in handy when writing complex programs. They allow developers to determine if values are equal, greater, or lesser than other values. With the above examples, you should be able to use comparison operators effectively in your program. Happy coding!