Here we provide, technology updates, sports updates, defense updates and educational blogs, HISTORY, Linux Tutorials, Python Tutorial
- Home
- About us
- Contact us
- Sitemap
- Disclaimer
- Privacy Policy
- Terms and Condition
-
Settings
- Dark mode
Python Comparison Operators Example
Comparison Operators
In this article, we will be learning about Comparison Operators in Python. These operators will allow us to compare variables and output a Boolean value (True or False).
If you have any sort of background in Math, these operators should be very straight forward.
First, we'll present a table of the comparison operators and then work through some examples:
Table of Comparison Operators
In the table below, a=5 and b=6.
| Operator | Description | Example |
|---|---|---|
| == | If the values of two operands are equal, then the condition becomes true. | (a == b) is not true. |
| != | If the values of the two operands are not equal, then the condition becomes true. | (a != b) is true |
| > | If the value of the left operand is greater than the value of the right operand, then the condition becomes true. | (a > b) is not true. |
| < | If the value of the left operand is less than the value of the right operand, then the condition becomes true. | (a < b) is true. |
| >= | If the value of the left operand is greater than or equal to the value of the right operand, then the condition becomes true. | (a >= b) is not true. |
| <= | If the value of the left operand is less than or equal to the value of the right operand, then the condition becomes true. | (a <= b) is true. |
Let's now work through quick examples of each of these.
Equal2 == 2OUTPUT
True1 == 0OUTPUT
False
Note that == is a comparison operator, while = is an
assignment operator.
Not Equal
2 != 1OUTPUT
True2 != 2OUTPUT
FalseGreater Than
2 > 1OUTPUT
True2 > 4OUTPUT
FalseNot Equal
2 != 1OUTPUT
True2 != 2OUTPUT
FalseLesser Than
2 < 1OUTPUT
True2 < 1OUTPUT
FalseGreater Than Equal
2 >= 2OUTPUT
True2 >= 1OUTPUT
TrueLesser Than Equal
2 <= 2OUTPUT
True2 <= 4OUTPUT
True
Post a Comment
Post a Comment
if you have any doubts, please let me know