Professional Documents
Culture Documents
Python Revision Sheet: Control Structures
Python Revision Sheet: Control Structures
1. If-Else Statements:
if condition:
# Code if condition is true
else:
# Code if condition is false
2. If-Elif-Else Statements:
if condition1:
# Code if condition1 is true
elif condition2:
# Code if condition2 is true and condition1 is false
else:
# Code if both condition1 and condition2 are false
3. For Loop:
4. While Loop:
while condition:
# Code while condition is true
Key Points:
- Indentation is crucial.
- Conditions must evaluate to True/False.
- Colon (:) marks the start of a code block.
- range() generates a sequence for loops.
- Avoid infinite loops with while; ensure condition becomes false.
Tips:
- Use meaningful variable names.
- Comment code to explain logic.
- Test with different inputs.
Examples:
# If-Else Example
age = 16
if age >= 18:
print("You can vote!")
else:
print("You cannot vote yet.")