You are on page 1of 2

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:

for variable in iterable:


# Code for each value in the iterable

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.")

# For Loop Example


for i in range(1, 4):
print("Iteration:", i)

# While Loop Example


num = 1
while num <= 5:
print(num)
num += 1

Practice and experiment to master these concepts!

You might also like