The Ultimate Guide To String Comparison PythonString Comparison Python Top Secrets!
Complete Python Bootcamp From Zero to Hero in Python (String Comparison)
Comparison Operators – String Comparison Python Advantages!
Compare variables and output a Boolean value (True or False).
Operator | Description | Example |
== | If equal, then the condition becomes true. | (a == b) not true. |
!= | If not equal, then condition becomes true. | (a != b) is true |
> | If greater than, then becomes true. | (1 > 2) not true. |
< | If less than, then condition becomes true. | (1 < 2) is true. |
>= | If greater than or equal, then it’s true. | (1 >= 2) not true. |
<= | If less than or equal then becomes true. | (1 <= 2) is true. |
Note: == is a comparison operator, while = is an assignment operator.
Equal
Not Equal
Greater Than
Less Than
Greater Than or Equal to
Less than or Equal to
Chained Comparison Operators
String Comparison Python List! More info:
Contents
1.1 Equal
1.2 Not Equal
1.3 Greater Than
1.4 Less Than
2. Chained Comparison Operators
Compare variables and output a Boolean value (True or False).
Operator |
Description |
Example |
== |
If equal, then the condition becomes true. |
(a == b) not true. |
!= |
If not equal, then condition becomes true. |
(a != b) is true |
> |
If greater than, then becomes true. |
(1 > 2) not true. |
< |
If less than, then condition becomes true. |
(1 < 2) is true. |
>= |
If greater than or equal, then it’s true. |
(1 >= 2) not true. |
<= |
If less than or equal then becomes true. |
(1 <= 2) is true. |
Note: == is a comparison operator, while = is an assignment operator.
print(2 == 2)
# output: True
—————————————————————–
print(1 == 0)
# output: False
—————————————————————–
print(2 != 1)
# output: True
—————————————————————–
print(2 != 2)
# output: False
—————————————————————–
print(2 > 1)
# output: True
—————————————————————–
print(2 > 4)
# output: False
—————————————————————–
print(2 < 4)
# output: True
—————————————————————–
print(2 < 1)
# output: False
—————————————————————–
print(2 >= 2)
# output: True
—————————————————————–
print(2 >= 1)
# output: True
—————————————————————–
print(2 <= 2)
# output: True
—————————————————————–
print(2 <= 4)
# output: True
—————————————————————–
2 Chained Comparison Operators
print(1 < 2 < 3)
# output: True
—————————————————————–
print((1 < 2) and (2 < 3))
# output: True
—————————————————————–
print((‘h’ == ‘h’) and (2 == 2))
# output: True
—————————————————————–
print(1 < 3 > 2)
# output: True
—————————————————————–
print(1 < 3 and 3 > 2)
# output: True
—————————————————————–
print(1 == 2 or 2 < 3)
# output: True
—————————————————————–
print(1 == 1 or 100 == 1)
# output: True
—————————————————————–
print((100 == 1) or (2 == 200))
# output: False
—————————————————————–
print(not(1 == 1))
# output: False
—————————————————————–
print(1 != 1)
# output: False
—————————————————————–
print(400 > 500)
# output: False
—————————————————————–
print(not(400 > 500))
# output: True