Section 4 Menu
< Section 3
Section 5 >
- Comparison Operators in Python
- Chaining Comparison Operators in Python with Logical Operators
- Quiz
Comparison Operators in Python
https://www.udemy.com/complete-python-bootcamp/learn/lecture/9407954#content
| Operator | Description | Example> |
|---|---|---|
| == | Equals | 1 == 1 (True) 1 == 2 (False) |
| != | Not Equal | 1 != 1 (False) 1 != 2 (True) |
| < | Less Than | 1 < 2 (True) 2 < 1 (False) 1 < 1 (False) |
| <= | Less Than or Equal to | 1 <= 2 (True) 2 <= 1 (False) 1 <= 1 (True) |
| > | Greater Than | 1 > 2 (False) 2 > 1 (True) 1 > 1 (False) |
| >= | Greater Than or Equal to | 1 >= 2 (False) 2 >= 1 (True) 1 >= 1 (True) |
Chaining Comparison Operators in Python with Logical Operators
https://www.udemy.com/complete-python-bootcamp/learn/lecture/9407956#content
- and
- or
- not
and
Results of both sides of the ‘and’ must be True. If one side fails, the result is False.
1 < 2 < 3
True
“is 1 less than 2 and is 2 less than 3”
* This format is not recommended because it is less readable.
1 < 2 > 3
False
“is 1 less than 2 and is 2 greater than 3”
1 < 2 and 2 < 3
True
(1 < 2) and (2 < 3)
True
or
One one side of the ‘or’ comparison needs to be True. Only if both conditions are False, the result is False.
1 == 1 or 2 == 2
True
1 == 2 or 2 == 2
True
1 == 2 or 2 == 3
False
not
Returns the reverse of the result
not 1 == 1
False
not (1 == 1)
False
not True
False
not (1 == 2)
True
not (False)
True