Python For Loop
Python For Loop
Python For loop – In Python, for loop is used when a set of statements are to be executed iteratively over a
sequence of elements.
Nested For Loop is also discussed in this Python Tutorial.
Syntax – Python For Loop
for <variable> in <sequence>:
statement1
statement2
A pictorial representation of for loop is shown below.
Python For Loop Execution Model
Python For Loop Examples
Following examples were provided, based on the iteration over different types of sequences :
For loop with iteration over elements in list
For loop with iteration over elements in range
For loop with iteration over characters in string
For loop with iteration over items in tuple
Nested For Loop
Note : Python 3.6.1 is used for the examples.
Example 1 – Python For Loop with Iterator over List
In this example, we will take a list of numbers and use for loop to iterate over each of the element in the list.
example.py – Python Program
for number in [1, 2, 3, 5, 6, 10, 11, 12]:
print(number)
Output
1
2
3
5
6
10
11
12
Example 2 – Python For Loop with Iterator over Range
In this example, we will take a range, and use for loop to iterate over each number in the range.
example.py – Python Program
for number in range(1, 5):
print(number)
Output
1
2
3
4
Example 3 – Python For loop with Iterator over String
In this example, we will take a String and use for loop to iterate over each character of the string.
example.py – Python Program
for ch in "tutorialkart":
print(ch)
Output
t
u
t
o
r
i
a
l
k
a
r
t
Example 4 – Python For loop with Iterator over Tuple
In this example, we will take a tuple and use for loop to iterate over each item in the tuple.
example.py – Python Program
for name in ('Johny', 'Luciana', 'Christy', 'Gopal', 'Joey'):
print(name)
Output
Johny
Luciana
Christy
Gopal
Joey
Nested For Loop
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 – Python Program
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!
Conclusion
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.
Python Programming
⊩ Python Tutorial
⊩ Install Python
⊩ Install Anaconda Python
⊩ Python HelloWorld Program
⊩ Python Variables
⊩ Python Variable Data Type Conversion
⊩ Python Comments
Control Statements
⊩ Python If
⊩ Python If Else
⊩ Python While Loop
⊩ Python For Loop
Python String
⊩ Python String Methods
⊩ Python String Length
⊩ Python String Replace
⊩ Python Split String
⊩ Python Count Occurrences of Sub-String
⊩ Python Sort List of Strings
Functions
⊩ Python Functions
Python Collections
⊩ Python List
⊩ Python Dictionary
Advanced
⊩ Python Multithreading
Useful Resources
⊩ Python Interview Questions