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:
fortells Python: โStart repeating.โiremembers the current round number.range(5)creates five rounds.- The colon
:says that the repeated block comes next. - 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
SyntaxErrornearforโ 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!
๐ฌ Comments (0)
No comments yet
Be the first to share your opinion about this article!