For Loop in Python
A for loop processes each value from an iterable such as a list, string, or range.
What is For Loop?
A for loop processes each value from an iterable such as a list, string, or range.
Learn Python for loops with range, enumerate, and a runnable example.
When should you use it?
- Learn Python syntax through a practical example.
- Build a foundation for larger programming problems.
- Test an idea quickly without a local setup.
Example code
main.py
languages = ["Python", "JavaScript", "Go"]
for position, language in enumerate(languages, start=1):
print(f"{position}. {language}")
Expected output
1. Python
2. JavaScript
3. Go
How it works
Enumerate supplies both a zero-based index and the current item. The start argument changes the displayed position to begin at 1.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.