- Home
- /
- Python for Loop
Python Tutorial
Introduction
Python Data Types & Oper...
Python Strings
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Conditional Statements
Python Loops
Python Functions
Python Modules
Python OOPS
Advanced Topics
File Handling
- Home
- /
- Python for Loop
for Loop in Python
In many situations, we need to run a group of statements repeatedly. This is where loops come in. Python supports multiple types of loops, and one of the most commonly used is the for loop.
The for loop is ideal when you’re working with things like strings, lists, tuples, sets, or dictionaries—any data you can iterate over.
Looping Through a String
You can use a for loop to go through each character in a string:
name = 'Arshyan'
for letter in name:
print(letter, end=", ")
Output:
A, r, s, h, y, a, n,
Looping Through a Tuple
colors = ("Red", "Green", "Blue", "Yellow")
for color in colors:
print(color)
Output:
Red
Green
Blue
Yellow
You can apply the same logic to lists, sets, and even dictionaries.
Using range() with a for Loop
Sometimes, we want the loop to run a specific number of times without using an existing sequence. That’s when range() becomes helpful.
Example 1: Looping from 0 to 4
for i in range(5):
print(i)
Output:
0
1
2
3
4
Example 2: Custom Start and Stop Values
for i in range(4, 9):
print(i)
Output:
4
5
6
7
8
Summary
Use
forloops to repeat actions over sequences (like strings or lists).Use
range()to loop a specific number of times.range(start, stop)gives numbers fromstartup to—but not including—stop.