Conditional Statements: if, elif, else
Conditional structures allow a program to execute different blocks of code depending on the result of one or more conditions.
Basic Syntax:
if condition:
# Code block if the condition is true
elif another_condition:
# Block if the first was false and this one is true
else:
# Block if none of the conditions were true
Example:
age = 20
if age < 18:
print("You are a minor")
elif age >= 18 and age < 65:
print("You are an adult")
else:
print("You are a senior citizen")
Best Practices:
- Use 4 spaces for indentation by convention (avoid mixing tabs and spaces).
- Evaluate simple conditions first.
- Avoid excessive nested
elifstatements when possible.