๐Ÿ“ Python

The for Loop: Make Python Work for You ๐Ÿ”„

P
Author
PyLand Team
๐Ÿ“…
Published
30.03.2026
โฑ๏ธ
Reading time
2 min
๐Ÿ‘๏ธ
Views
434
๐ŸŒฑ
Level
Beginner

Imagine that you need to display a rocket five times. You could copy print(rocket) five times, but programmers do not like repeating the same work by hand. Python has a for loop for that.

A loop takes a command and repeats it as many times as you ask.

Meet the for Loop

for i in range(5):
    print('Starting engine!')

This code prints the message exactly five times. Let us take it apart:

  1. for tells Python: โ€œStart repeating.โ€
  2. i remembers the current round number.
  3. range(5) creates five rounds.
  4. The colon : says that the repeated block comes next.
  5. Indentation shows which commands are inside the loop.

How to Indent Correctly

Write the loop line with its colon, press Enter, and then press Tab once. The command moves to the right and becomes part of the loop:

for i in range(3):
    print('Checking systems')

The editor usually turns Tab into four spaces. You do not need to count them manually โ€” just press Tab once. Commands in the same block should start at the same level.

for i in range(3):
    print('Ignition')
    print('Engine running')

Both commands repeat three times because they have equal indentation.

This command runs only once, after the loop:

for i in range(3):
    print('Ignition')

print('Check complete')

Do not manually mix tabs and spaces in one block. Inconsistent indentation can confuse Python.

What Does i Store?

On each round, Python puts a new number into i. Counting starts at zero:

for i in range(3):
    print(i)

Output:

0
1
2

There are still three rounds. Programming simply often starts counting at zero.

To display familiar numbers from 1 through 3, add one:

for i in range(3):
    print(f'Frame {i + 1}')

Building Rocket Frames

A loop is perfect for the first frames of an animation:

import time

for i in range(5):
    print(f'Frame {i + 1}: rocket ready ๐Ÿš€')
    time.sleep(0.3)

Python displays five frames and pauses briefly after each one. Later, you can change each frame’s contents โ€” that is how motion begins.

enumerate โ€” index and value at once

When looping through a list, you often need both the index and the value itself. You can count manually with i + 1, but Python has a cleaner option โ€” enumerate():

planets = ['Mercury', 'Venus', 'Earth']

for i, planet in enumerate(planets):
    print(f'{i}: {planet}')

Output:

0: Mercury
1: Venus
2: Earth

enumerate() produces a pair on every round โ€” that is why the loop has two names separated by a comma: i for the index, planet for the element.

The start argument

Counting starts at zero by default. To begin at a different number, pass the start argument:

for i, planet in enumerate(planets, start=1):
    print(f'{i}. {planet}')

Output:

1. Mercury
2. Venus
3. Earth

This is cleaner than writing i + 1 inside every print.

If Something Does Not Work

  • SyntaxError near for โ€” check the colon at the end of the line.
  • IndentationError โ€” after pressing Enter, press Tab before the command inside the loop.
  • A command ran only once โ€” it probably has no indentation and is outside the loop.
  • Extra code repeated โ€” remove indentation from a command that should run after the loop.

Remember the simple pattern: write for, choose the number of repetitions with range(), add a colon, press Enter, and press Tab. Done โ€” Python now handles the repetitive work!

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Event Loop in Python: How asyncio Enables Concurrโ€ฆ

Event loop is the heart of asyncio. It doesn't run code in parallel across multiple...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 386
๐Ÿ“

The while Loop: Repeat While a Condition Holds ๐Ÿ”

The for loop works great when you know how many times to repeat an action....

๐Ÿ“… 30.03.2026 ๐Ÿ‘๏ธ 406
๐Ÿ“

OOP: Program Like a World Builder ๐ŸŒ

Imagine: you're building a zombie-apocalypse game. You need zombies, humans, weapons. Every zombie has a...

๐Ÿ“… 03.04.2026 ๐Ÿ‘๏ธ 414