Python for loop iterate over the sequences. It repeats the piece of code n range of times. Consider the following flowchart of for loop.
Flowchart

Python for loop can be used in two ways.
Using Sequence
Using range() function
Using Sequence
The sequence is referred to as list, set, string, tuple, and dictionary in Python. We can extract the factors of the sequence using for loop. Below is the syntax for loop.
Syntax:
for iterating_var in sequence:
statement(s)
Let’s understand the following example.
Example – 1
list1 = [10, 20, 30, 40, 50, 60]
for i in list1:
print(i)
Output:
10
20
30
40
50
60
Example – 2
str = "JavaTpoint"
for i in str:
print(i)
Output:
J
a
v
a
T
p
o
i
n
t
Using the range() function
The vary function() generates the sequence of the numbers. For example, if we execute the range(5) and it will generate the zero to 4 The syntax of the range() characteristic is given below.
Syntax:
range(start, stop, step-size)
It accepts the three arguments.
The start represents the beginning of the iteration.
The stop indicates the end of for loop. It will iterate till stop-1.
The step size skips the specific number of iteration. By default, the step size is 1.
Let’s understand the following example.
Example – 1
for i in range(20):
print(i, end = ' ')
Output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Example – two Traverse the List element using range() feature
list = ['Peter', 'Joseph', 'Ricky', 'Devansh', 'Kevin']
for i in range(len(list)):
print("Hii",list[i])
Output:
Hii Peter
Hii Joseph
Hii Ricky
Hii Devansh
Hii Kevin
Explanation:
The len() characteristic returns the size of the list. The range() acquired the range of elements in the list and printed its elements.
Leave a Review