In this Python tutorial, we will learn about For Loop statement, its syntax, and cover examples with For Loop where it is used to iterate over different types of sequences and collections.
Python for loop for iterating over iterable objects
Python For loop is used to execute a set of statements iteratively over a sequence of elements.
A Python for loop is commonly used with lists, strings, tuples, dictionaries, sets, and range(). In each iteration, Python takes the next item from the iterable, assigns it to the loop variable, and runs the statements inside the loop body.
We shall also discuss Nested For Loop in this tutorial.
Python for loop syntax with iterable and loop variable
The syntax of For Loop in Python is
for <variable> in <iterable>:
statement(s)
where an iterable could be a sequence or collection or any custom Python class object that implements __iter__() method.
The colon : after the for statement is required. The statements inside the loop must be indented consistently. Python repeats the indented block once for each item available in the iterable.
Python for loop execution flow
The flow of program execution in a Python For Loop is shown in the following picture.

Python first checks whether another item is available in the iterable. If an item is available, it is assigned to the loop variable and the loop body runs. When no more items are available, Python exits the for loop and continues with the next statement after the loop.
Python for loop with list items
In this example, we will take a list of numbers and use for loop to iterate over each of the element in this list.
Example.py
myList = [1, 2, 3, 5, 6, 10, 11, 12]
for item in myList:
print(item)
Output
1
2
3
5
6
10
11
12
In this list example, item takes the values 1, 2, 3, and so on until the list is exhausted.
Python for loop with range()
In this example, we will take a range object, and use for loop to iterate over each number in this range.
Example.py
myRange = range(1, 5)
for number in myRange:
print(number)
Output
1
2
3
4
The call range(1, 5) produces numbers starting from 1 up to, but not including, 5. Therefore, the output stops at 4.
Python range() with start, stop, and step in a for loop
You can pass a third argument to range() to control the step value. This is useful when you want to skip numbers in a fixed pattern.
Example.py
for number in range(2, 11, 2):
print(number)
Output
2
4
6
8
10
Here, the loop starts at 2, stops before crossing 11, and increases the value by 2 after every iteration.
Python for loop with String characters
In this example, we will take a String and use for loop to iterate over each character of the string.
Example.py
myString = 'tutorialkart'
for ch in myString:
print(ch)
Output
t
u
t
o
r
i
a
l
k
a
r
t
A string is iterable in Python. Therefore, the for loop reads one character at a time from left to right.
Python string slicing with step before using a for loop
A slice such as [::3] means: start from the beginning, go up to the end, and take every third character. You can loop over the resulting sliced string.
Example.py
text = 'tutorialkart'
for ch in text[::3]:
print(ch)
Output
t
o
l
a
Python for loop with Tuple values
In this example, we will take a tuple and use for loop to iterate over each item in the tuple.
Example.py
myTuple = ('abc', 'bcd', 'xyz')
for item in myTuple:
print(item)
Output
abc
bcd
xyz
Python for loop with dictionary keys and values
When a dictionary is used directly in a for loop, Python iterates over its keys. To read both keys and values, use the items() method.
Example.py
student_marks = {
'Arun': 82,
'Bala': 75,
'Charan': 91
}
for name, marks in student_marks.items():
print(f'{name}: {marks}')
Output
Arun: 82
Bala: 75
Charan: 91
In this example, name receives the dictionary key and marks receives the corresponding value in each iteration.
Python for loop with enumerate() for index and item
Use enumerate() when you need both the position and the item while looping over a sequence.
Example.py
colors = ['red', 'green', 'blue']
for index, color in enumerate(colors):
print(index, color)
Output
0 red
1 green
2 blue
By default, enumerate() starts counting from 0. You can pass a start value if you want numbering to begin from another value.
Python for loop with break and continue
The break statement exits a for loop immediately. The continue statement skips the remaining statements in the current iteration and moves to the next item.
Example.py
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number == 3:
continue
if number == 5:
break
print(number)
Output
1
2
4
Here, 3 is skipped because of continue. The loop stops before printing 5 because of break.
Python for-else loop when no break occurs
Python allows an else block after a for loop. The loop else block runs only when the for loop finishes normally without a break.
Example.py
numbers = [2, 4, 6, 8]
for number in numbers:
if number % 2 != 0:
print('Odd number found')
break
else:
print('No odd number found')
Output
No odd number found
This pattern is useful when searching for an item and printing or returning a fallback result only if the search completes without finding a match.
Nested For Loop in Python
Any statement inside the for loop could be another for loop, if block, if-else block or any such.
An example program for nested for loop with embedded if-else blocks is given below.
Example.py
for greeting in ['hello', 'hi', 'hola']:
for ch in greeting:
print(ch)
if greeting=='hello':
print("hello buddy!")
else:
if greeting=='hi':
print("hi friend!")
else:
print("hola gola!")
Output
h
e
l
l
o
hello buddy!
h
i
hi friend!
h
o
l
a
hola gola!
In a nested for loop, the inner loop completes all its iterations for each single iteration of the outer loop. In this example, Python prints all characters of one greeting before moving to the next greeting.
Common Python for loop mistakes and fixes
- Missing colon after the for statement: Write
for item in items:, notfor item in items. - Wrong indentation: Keep all statements that belong to the loop indented under the
forline. - Confusing the loop variable with the iterable: In
for item in items:,itemis one value at a time, whileitemsis the complete iterable. - Expecting range stop value to be included:
range(1, 5)produces1to4, not1to5. - Using a for loop when a direct membership test is clearer: For simple checks,
if value in items:may be easier to read than looping manually.
Python for loop editorial QA checklist
- Does every Python for loop example include a valid iterable after the
inkeyword? - Are the statements inside each for loop indented consistently?
- Do output blocks match the exact values printed by the corresponding Python code?
- Is the behavior of
range()explained clearly, especially the excluded stop value? - Are nested for loop examples clear about which loop is outer and which loop is inner?
Frequently asked questions about Python for loops
What is a for loop in Python?
A for loop in Python is a loop that iterates over an iterable object such as a list, string, tuple, dictionary, set, or range object. It runs the loop body once for each item in the iterable.
What is a Python for loop used for?
A Python for loop is used for repeated tasks such as reading each item in a list, printing characters of a string, processing dictionary values, generating number patterns with range(), and searching through collections.
What does range(1, 5) print in a Python for loop?
range(1, 5) gives the values 1, 2, 3, and 4. The start value is included, but the stop value is excluded.
What is [::3] in Python when used before a for loop?
[::3] is a slice with a step value of 3. It selects every third element from a sequence. If you write for ch in text[::3]:, the loop iterates only over those selected characters.
Can a Python for loop contain another for loop?
Yes. A for loop can contain another for loop. This is called a nested for loop, and it is useful for working with rows and columns, tables, matrices, and grouped data.
Conclusion: Python for loop over lists, range, strings, tuples, and nested loops
In this Python Tutorial, we have learn about for loop execution flow, for loop syntax in python, nested for loop, for loops that iterate over list, range, string and tuple. As an exercise, try out the for loop that iterates over buffer.
Use a Python for loop when you want to process each item in an iterable. Use range() for repeated numeric loops, enumerate() when an index is needed, items() for dictionary key-value pairs, and nested for loops when each item contains another iterable structure.
TutorialKart.com