Comparison operators :
there are many Comparison operators you see in below
these comparison operators use to compare the two values.
Operators |
Meaning |
Example |
Result |
== |
Equal to |
5 == 5 |
True |
!= |
Not equal to |
5 != 5 |
False |
> |
Greater than |
5 > 2 |
True |
< |
Less than |
5 < 2 |
False |
>= |
Greater than equal to |
5 >= 2 |
True |
<= |
Less than equal to |
2 <= 5 |
True |
- == check it values are equal
- != Ensure values are not equal
- > left value greater than right
- < left value less than right
- >= left value greater than or equal to right
- <= left value less than or equal to right
y = 5
Output:
True
!= operator :
remember the ATM PIN example ,
if entered pin is incorrect. we will use python not equal to with this operator != .
correct pin 1234
code :
entered_pin = 5555
#check if entered pin is not correct
if entered_pin != 1234:
print("Unauthorized access!")
Output :
Unauthorized access!
Code :
x = 10
y = 10
print(x == y) # Output: True
Output:
True
(Greater than) operator
x = 10
y = 5
print(x > y)
Output:
True
Checks if x is greater than y
< (Less than)
x = 10
y = 20
print(x < y)
Output:
True
Checks if x is less than y.
>= (Greater than or equal to)
x = 10
y = 10
print(x >= y)
Output:
True
Checks if x is greater than or
equal to y.
<= (Less than or equal to)
x = 10
y = 15
print(x <= y)
Output:
True
Checks if x is less than or
equal to y.
- You have to must try in your python compiler use idle avoid online compilers to Errors
- Comparison Operators Quiz :
https://proinpythonlng.blogspot.com/2025/06/comparison-operators-quiz.html
0 Comments