Python – Loop

After reading this Python loop topic, you will understand loop types and you will know the for loop flowchart, theory, and examples.

In real life, many times we need to perform some task repeated over and over, until a specific goal is reached. That’s why Python provides us a repetitive structure called loop.

Python loop that executes the same statement(s) again and again and it is useful for solving many programming problems. The Python supports basic two types of loops are: –

for loop

Python for loop is used to iterates over a sequence of items. The sequence can be either of a list, a set, a dictionary, or a string. The for loop is suitable when one knows already how many times the loop will be executed.

General Form (Syntax):

The syntax of for loop is given below,

for variable_value in sequence:
  statement(s)

Here, for is the keyword written in the starting and when for loop starts, variable_value is the variable on which the value of item assign on each iteration over the sequence. The for loop executes statement(s) as long as the last item of the sequence reach.

for loop flowchart:

The flow chart of for loop is given below,

Example:

Program (1): To demonstrate how to use for loop over the list.

# storing items 2,3 and 4 in the list
x = [2,3,4]
# use for loop to print each item of the list
for i in x:
        print(i)

Output (1)

2
3
4

for loop using range() function

Python for loop uses range() function to produce a variety of sequences overs numbers.

Examples:

Program (1): To demonstrate how to use for loop using range() function with one argument.

# use for loop using range() function to print i value.
for i in range(4):
    print(i)

Output (1)

0
1
2
3

Explanation: for Program(1) and Output(1)

In the program(1), the value 4 is only one argument inside range() function (i.e. range(4)) describe the end of the range, with 0 being start value, and 1 being the step value. Hence the output(1) shown above is 0, 1, 2 and 3.

Program (2): To demonstrate how to use for loop using range() function with two arguments.

# use for loop using range() function to print each item of the list
for i in range(1,4):
  print(i)

Output (2)

0
1
2
3

Explanation: for Program(1) and Output(1)

In the program(2), the values 1 and 4 are two arguments inside range() function (i.e. range(1, 4)) describe the following as 4 being the end of the range, with 1 starting value, and 1 being the step value.Hence output(2) shown above is 0,1,2 and 3.

Program (3): To demonstrate how to use for loop using range() function with three arguments.

# use for loop using range() function to print each item of the list
for i in range(1,7,2):
 print(i)

Output (3)

1
3
5

Explanation

In the program(3), the values 1, 7 and 2 are three arguments inside range() function (i.e. range(1,7,2)) describes the following as 7 being the end of the range, with 1 starting value, and 2 being the step value. Hence the output(3) shown above is 1,3 and 5.

for loop with else

Python for loop support an optional else block. When a for loop finished, it’s associated else block executes.

Program (1): To print demonstrate how to use for loop with else.

for i in range(1,5,1):
  print("How are you")
else:
    print("Have a nice day")

Output (1)

How are you
How are you
How are you
How are you
How are you
Have a nice day

Explanation: for Program(1) and Output(1)

When for loop finished and print the message “How are you” for five times, it’s associated else block executes and print the message “How are you” at the end.

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *