Highlights from this chapter include:
- Booleans represent a value of
True
orFalse
. - Comparison operators compare values and produce
True
orFalse
. - Logical operators take condition operand(s) and produce
True
orFalse
. - Operators are evaluated in order according to precedence and associativity.
- Conditions are expressions that evaluate to
True
orFalse
. - Decision statements allow different paths of execution (branches) through code based on conditions.
- Decision statements can be nested inside other decision statements.
- Conditional expressions are single-line versions of
if-else
statements.
At this point, you should be able to write programs that evaluate conditions and execute code statements accordingly with the correct order of operations. The programming practice below ties together most topics presented in the chapter.
Function | Description |
---|---|
bool(x) |
Converts |
Operator | Description |
|
Compares the values of |
|
Compares the values of |
|
Compares the values of |
|
Compares the values of |
|
Compares the values of |
|
Compares the values of |
|
Evaluates the Boolean values of |
|
Evaluates the Boolean values of |
|
Evaluates the Boolean value of |
Decision statement | Description |
|
# Statements before
if condition:
# Body
# Statements after |
|
# Statements before
if condition:
# Body
else:
# Body
# Statements after |
|
# Statements before
if condition:
# Body
elif condition:
# Body
else:
# Body
# Statements after |
Nested |
# Statements before
if condition:
if condition:
# Body
else:
# Body
else:
if condition:
# Body
else:
# Body
# Statements after |
Conditional expression |
|