While Loops
Control Flow If Statements
Python’s if
statements control the execution flow based on conditions.
Basic Syntax:
if condition:
# Code to execute if condition is True
elif condition2: #Optional
# Code to execute if condition2 is True and condition is False
else: #Optional
# Code to execute if all above conditions are False
Example:
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
Important Notes:
- Conditions are evaluated as boolean values (True or False).
- Indentation is crucial; it defines the code blocks within each
if
,elif
, andelse
branch. - You can have multiple
elif
blocks. - The
else
block is optional.
Boolean Logic - This needs a separate note explaining boolean operators (and, or, not) and truth tables.
Comparison Operators - This should cover operators like ==
, !=
, >
, <
, >=
, <=
.
Nested If Statements - Explaining how to have if
statements inside other if
statements.