Looping constructs in Python

Learn: What is Loop and Looping? How many types of looping construct are provided by python? We'll also see their syntax and difference between them? By Abhishek Jain Last updated : December 18, 2023

How Many Looping Constructs Exist in Python?

Loops are used to repeatedly execute the same code in a program. Python provides two types of looping constructs:

  1. The while loop construct
  2. The for loop construct

1. The while loop construct

Syntax

# Condition is Boolean expression
while condition:
    # returning True or False
    STATEMENTs BLOCK 1
[else: 
    # optional part of while
    STATEMENTs BLOCK 2]

We can see that while looks like if statement. The statement begins with keyword while followed by Boolean condition followed by colon (:). What follows next is block of statement(s).

The statement(s) in BLOCK 1 keeps on executing till condition in while remains True; once the condition becomes False and if the else clause is written in while, then else will get executed.

Example of Python while loop construct

A loop to print nos. from 1 to 10

# Initialize loop counter
i = 1

# The loop
while i <= 10:
    print(i)
    i = i + 1  # could be written as i+=1

Output

1
2
3
4
5
6
7
8
9
10

In the above example,The first statement initialized the variable (controlling loop) and then while evaluates the condition, which is True so the block of statements written next will be executed.

Last statement in the block ensures that, with every execution of loop, loop control variable moves near to the termination point. If this does not happen then the loop willkeep on executing infinitely.

As soon as i becomes 11, condition in while will evaluate to False and this will terminate the loop.

Note: As there is ',' after print i all the values will be printed in the same line.

2. The for loop construct

Syntax

For TARGET- LIST in EXPRESSION-LIST:
    STATEMENT BLOCK 1
[else: # optional block
    STATEMENT BLOCK 2]

Example of Python for loop construct

A loop to print nos. from 1 to 10

for i in range(1, 11, 1):
    print(i)

Output

1
2
3
4
5
6
7
8
9
10

Let's understand the flow of execution of the statement:

The statement introduces a function range(), its syntax is: range(start, stop, [step]), where [step] is optional

The range() method generates a list of values starting from start till stop-1. Step if given is added to the value generated, to get next value in the list.

Let's move back to the for statement: i is the variable, which keeps on getting a value generated by range() function, and the block of statement (s) are worked on for each value of i. As the last value is assigned to i, the loop block is executed last time and control is returned to next statement.

Now we can easily understand the result of for statement. range() generates a list from 1, 2, 3, 4, 5, ..., 10 as the step mentioned is 1, i keeps on getting a value at a time, which is then printed on screen.

Note:Apart from range() i (loop control variable) can take values from string, list, dictionary, etc.

Example

for letter in "Python":
  print "Current Letter:",letter
else:
  print "Coming out of loop"

Output

Current Letter: P
Current Letter: y
Current Letter: t
Current Letter: h
Current Letter: o
Current Letter: n
Coming out of loop

By now, you must have realized that, Syntax of for statement is also same as if statement or while statement.

Let's look at the equivalence of the two looping construct:

while and for looping statements

Python Tutorial


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.