In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There may be a situation when you need to execute a block of code several number of times.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times. The following diagram illustrates a loop statement −
Loops are required when there is a need to execute a piece of code one or more times. Just like any other language, python too supports loops. Various types of loops can be defined as follows:
-While loop :
Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. It helps in repeating a block of code till a particular condition holds true.
Related Page: Introduction To Python Programming
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. It uses a loop variable to repeat a block of code. Here are some points that you must keep in mind while working with for loop:
It must reference a list or range
For loop closes with a colon
The code that needs to be executed must be indented by one tab space
a=[‘Sunday’,’Monday’,’Tuesday’,’Wednesday’,’Thursday’,’Friday’,’Saturday’] for item in a:
Related Page: Lists Concepts In Python
You can use one or more loop inside any other “while, for or do..while loop.” They are used in a scenario where there is a need to use one loop inside the other.
Nested for loop
for item in l:
for element in item:
-Nested While loop
while x print ‘x= ‘,x
print ‘x is less than y’
while y print ‘y= ‘,y
print ‘y is greater than x but less than z’
x= 1; x is less than y; y= 10; y is greater than x but less than z; y= 14; y is greater than x but less than z; y= 18; y is greater than x but less than z; x= 6; x is less than y; x= 11; x is less than y; x= 16; x is less than y; x= 21; x is less than y.
Normal execution of loops can be controlled with the help of control statements. The different types of control statements are described below:
Terminates the loop execution and jumps to statement just after the loop
print ‘x is even ‘,x
print ‘x is odd’,x
print ‘I will not go further’
x is odd 1;x is even 2;x is odd 3;x is even 4;I will not go further
Related Page: Condition Objects - Python
-Continue Statement :
Skips the execution of remaining statements of the loop and jumps to the loop’s header line.
x = 10
if x%2!=0: continue
print(x,’is even and less than 10′)
(8, ‘is even and less than 10’); (6, ‘is even and less than 10’); (4, ‘is even and less than 10’);(2, ‘is even and less than 10’);(0, ‘is even and less than 10’)
–Pass Statement :
The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.
for letter in ‘Python’:
if letter == ‘h’:
print ‘This is pass block’
print ‘Current Letter :’, letter
print “Good bye!”
Get Updates on Tech posts, Interview & Certification questions and training schedules