What is a Loop?
In computer programming, a loop is a structure that repeats a block of code over and over again. Instead of typing the same statement 10 or 100 times, you write it once inside a loop and let Python handle the repetition!
Real-world analogy
Repetition
Executes a block of statements repeatedly without copy-pasting code.
Efficiency
Saves time and keeps your program short, clean, and easy to maintain.
Control
Run code a fixed number of times (for loop) or until a condition changes (while loop).
DRY Principle
Follows 'Don't Repeat Yourself' โ write code once and reuse it.
Key vocabulary
- Iteration โ a single cycle or turn through a loop.
- Loop Counter โ a variable that tracks which iteration the loop is currently on.
- Sequence โ an ordered group of items (like a range of numbers, a string, or a list).
- Infinite Loop โ a loop that never stops running because its stopping condition is never met.
๐งช Compact Loop Playground
Run the script below to watch how Python repeats the print statement 3 times, incrementing i on each step:
๐งช Compact Loop Playground
Ready to run
No variables yet.
Run your Python code to see them appear here live!
No variables yet.
Run your Python code to see them appear here live!