The if
Statement
- Control structure â directs the order of statement execution
- Sequence structure â statements executed in the order they appear
- Decision/Selection structure â executes actions based on a condition
Flowchart Representation
flowchart TD
A[Start] --> B{Condition}
B -- True --> C[...]
B -- False --> D[...]
Python Syntax
if condition:
# statement(s)
Boolean Variables
- References one of two values:
True
or False
- Represented by the
bool
type in Python
- Commonly used for flags, a variable to determine if a condition exists
Boolean Expressions
- Tested by the
if
statement to determine truthiness
- IE:
a > b
â True
if a
is greater than b
, False
otherwise
Relational Operators
>
â greater than
<
â less than
>=
â greater than or equal to
<=
â less than or equal to
==
â equal to
!=
â not equal to
The not
Operator
- Reverses the logical value of a boolean expression
- IE:
not False
â True
(and vise-versa)
Checking Numeric Ranges
- To check within a range, use
and
- To check outside a range, use
or