Home » Python

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?
Submitted by Abhishek Jain, on September 27, 2017

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

  1. while statement
  2. for statement

1) while statement

Its syntax is:

while condition: # condition is Boolean expression 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: A loop to print nos. from 1 to 10

i=1
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 sothe 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) for statement

Its syntax is:

For TARGET- LIST in EXPRESSION-LIST:
STATEMENT BLOCK 1
[else: # optional block
STATEMENT 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: 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

range() 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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.