if, else, and elif Statements
Read this for more on conditional statements.
3. Equality and relational operators
Equality and relational operators
Equality operators
An equality operator checks whether two operands' values are the same (==) or different (!=).
Note that equality is ==, not just =.
Equality operators | Description | Example (assume x is 3) |
---|---|---|
== |
a == b means a is equal to b |
x == 3 is truex == 4 is false |
!= |
a != b means a is not equal to b |
x != 3 is falsex != 4 is true |
An expression evaluates to a Boolean value.
A Boolean is a type that has just two values: True or False
Relational operators
A relational operator checks how one operand's value relates to another, like being greater than.
Relational operators | Description | Example (assume x is 3) |
---|---|---|
< |
a < b means a is less than b |
x < 4 is truex < 3 is false |
> |
a > b means a is greater than b |
x > 2 is truex >3 is false |
<= |
a <= b means a is less than or equal to b |
x <= 4 is truex <= 3 is truex <= 2 is false |
>= |
a >= b means a is greater than or equal to b |
x >= 2 is truex >= 3 is truex >= 4 is false |
Operator chaining
Python supports operator chaining.Example:
a < b < c
determines whether b is greater-than a but less-than c.
Chaining performs comparisons left to right,
evaluating
a < b
first. - If the result is true, then
b < c
is evaluated next. - If the result of the first comparison
a < b
is false, then there is - no need to continue evaluating the rest of the expression.