Loops in Python
Loops are used to execute a block of code repeatedly based on a condition.
for LoopIterates over a sequence (list, tuple, string, or range).
while LoopRepeats as long as the condition is True.
Place one loop inside another for complex iterations.
| Statement | Description | Example |
|---|---|---|
break | Exit the loop immediately | if i == 3: break |
continue | Skip the current iteration | if i % 2 == 0: continue |
pass | Do nothing, acts as a placeholder | pass |
Example:
else with LoopsExecuted when the loop completes normally (no break).
for loops are sequence-based, while loops are condition-based.
Use nested loops for multi-dimensional data.
break stops the loop, continue skips an iteration, pass is a placeholder.
else with loops runs only if the loop ends naturally.
Take quizzes related to this topic and see where you stand!
Start Quiz Now