โšกFREE Live Master Session: Code Your AI Companion for Kids

    Register for Free โ†’
    Free Worksheet ยท Interactive Python Engine

    Python LoopsFor Loops, While Loops & range()

    Learn how to automate repetition in Python! Master for loops, while loops, the range() function, and loop controls with interactive step-by-step visualizations.

    ๐Ÿ”

    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

    Imagine a music player playing a song playlist on repeat, or an athlete running 4 laps around a track. You perform the exact same action repeatedly until a target is reached (e.g. 4 laps completed or playlist ended). That is exactly what a Python loop does.
    ๐Ÿ”

    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

    Python ScriptEditable
    1
    2
    Variable Inspector

    No variables yet.

    Run your Python code to see them appear here live!

    Output & Console
    Output Console
    Press Run Code above to see Python output here.
    Real Python Execution & Tracing
    Interactive input() Supported
    1 / 6
    ๐Ÿš€ Ready to go further?

    Master Python Loops with Live 1-on-1 Expert Tutors

    Worksheets are a fantastic start! Our Python Programming Course for Kids takes you from loops to building real-world games and projects with personalised expert feedback.