Python For Loops - Repeat Code Like a Pro

Publié le: 05 juin 2026
sur la chaîne: Arohas Code Lab
0

Repeat Any List, String, or Range with Python For Loops

Learn how to use Python for loops to repeat actions over lists, strings, and ranges — without copying the same line of code five times.

By the end of this video you will be able to loop over a mission checklist, spell out a mission code character by character, and run a countdown from any number — all in just two lines.

⏱ Chapters:
0:00 Intro — Arohas Code Lab
0:03 Five Print Statements — and Why That Number Will Keep Growing
0:12 How a For Loop Visits Every Item — The Core Mechanism
0:21 Looping Over the Mission Checklist — Lists in Action
0:31 Strings and Ranges — Two More Sequences Python Can Loop Over
0:42 The range() Surprise — Off-by-One Errors in Mission Numbering
0:53 For Loop Mental Model — and Your Planet Challenge

---

Challenge Solution

*Task:* Given `planets = ['Mercury', 'Venus', 'Earth']`, print each planet with its 1-based position number.

*Expected output:*
```
1. Mercury
2. Venus
3. Earth
```

*Solution:*
```python
planets = ['Mercury', 'Venus', 'Earth']
for i in range(len(planets)):
print(f'{i + 1}. {planets[i]}')
```

*How it works:*
`range(len(planets))` produces 0, 1, 2 — one index per planet.
`i + 1` shifts each index to human-readable 1-based numbering.
`planets[i]` looks up the planet at that position.

*Alternative using `enumerate`* (bonus — not covered in the video):
```python
for i, planet in enumerate(planets, start=1):
print(f'{i}. {planet}')
```

`enumerate(planets, start=1)` pairs each planet with a counter starting at 1, skipping the `i + 1` adjustment entirely.

---

#Python #ForLoops #PythonBeginner #LearnPython #CodingForBeginners #PythonTutorial #ArohasCodeLab


Sur cette page du site, vous pouvez voir la vidéo en ligne Python For Loops - Repeat Code Like a Pro durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Arohas Code Lab 05 juin 2026, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée fois et il a aimé 0 téléspectateurs. Bon visionnage!