Loops with for and while
Use a for loop, a while loop, and range() to walk a short list of sample log file names.
Based on the official Python documentation. Date checked: November 27, 2026. Script tested on Python 3.13.5.
Series: Python for small scripts, Part 4 of 10. Previous: Part 3: Decisions with if, elif, and else. Next: Part 5: Lists: Store and Filter a Set of Items.
TL;DR
- A
forloop runs once for each item in a collection. - A
whileloop repeats as long as a condition is true. The body must change something so the loop can stop. range(3)gives the numbers 0, 1, and 2.- Tested on Python 3.13.5. This is Part 4 of 10.
Before you start
- Python 3.11 or newer. This script was tested on Python 3.13.5. Check with
python3 --versionorpython --version. - Work in the
python-scriptsfolder from Part 1. - Run with
python3 loop.py. On Windows,python loop.pyis the usual form.
The finished script
Save this as loop.py in that folder:
names = ["app.log", "auth.log", "cron.log"]
for name in names:
print("Checking", name)
index = 0
while index < len(names):
print(index, names[index])
index += 1
for number in range(3):
print("range:", number)
When you run it, you should see:
Checking app.log
Checking auth.log
Checking cron.log
0 app.log
1 auth.log
2 cron.log
range: 0
range: 1
range: 2
Steps
- Save and run the script. Change into
python-scriptsand runpython3 loop.py. Why: you can see all three styles of loop in one output. You should see: the nine lines above. - Read the
forloop.namesis a list of three sample file names.for name in names:setsnameto each item in turn and runs the indentedprintline. Why: you can process every item without writingprintthree times. - Read the
whileloop.len(names)is 3. The loop starts atindex = 0and continues whileindexis less than 3.index += 1adds one after each print, so the condition later becomes false. Why:whileis useful when you stop on a condition, but you must change that condition inside the loop or it never ends. - Read the
range(3)loop.range(3)produces 0, 1, and 2. It does not include 3. Why:rangeis the usual way to walk a sequence of numbers. You should see:range: 0, then1, then2.
If it fails
IndentationError: expected an indented block after 'for' statement(or afterwhile). The line after the colon is not indented. Indent the loop body and run the script again.- The script keeps running and prints nothing, or it never returns to the prompt. A
whileloop may have no line that changes the condition. Stop it with Ctrl+C. Python reportsKeyboardInterruptand points at thewhileline. Then add a line such asindex += 1inside the loop, save, and run it again. NameError: name 'names' is not defined. The list assignment is missing, or the name is spelled differently from the name in the loop.
Stay safe
- The list holds sample file names only. This script does not read or delete files on your disk.
- Do not start a
whileloop unless the body can make the condition false. - Do not put a delete or overwrite command inside a loop until you have a dry run that only prints the names.
Next
Part 5: Lists: Store and Filter a Set of Items
Sources
- Python docs, “for Statements” and “The range() Function” (docs.python.org/3/tutorial/controlflow.html#for-statements), Python 3.15.2 documentation, checked November 27, 2026.
- Python docs, “First Steps Towards Programming” (docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming), checked November 27, 2026.
Spot something wrong or outdated? Leave a comment below. Every comment is reviewed to keep these posts accurate.
By commenting, you agree to our Terms of use and confirm this is your own work. Short quotes are fine with a credit and link. Copied, unsafe, or abusive comments are removed. See something copied? Say so here.