Loops in Python
Loop
What is a loop ?
Loop is a feature used to execute a particular part of a program
repeatedly if a given condition evaluates to be True.
Types of Loop: Loop work flow:
• For Loop
• While Loop
• Do – While Loop
Loop
For Loop:
o The for loop is used to iterate a given set of statements multiple
times.
o The for loop functions by running a section of code repeatedly until a
certain condition has been satisfied.
Syntax of For loop:
for iterator_variable in sequence:
statements
Loop
Flow chart:
Loop
Example: Output:
Loop
range() function:
o The range() function returns a sequence of numbers, starting from 0 by
default and increments by 1 by default and stops before a specified number.
Syntax:
range(start, stop, step)
Parameter values:
Start: Optional. An integer number specifying at which position to start. Default is 0.
Stop: Required. An integer number specifying at which position to stop.
Step: Optional. An integer number specifying the incrementations. Default is 1.
Loop
Example: Output:
Loop
Sequence datatypes in for loop:
Example: Output:
Loop
Else statement with for loop in python:
o We can also combine else statement with for loop.
o As there is no condition in for loop based on which the execution will
terminate so the else block will be executed immediately after for block
finishes execution.
Syntax:
for iterator_variable in sequence:
statements
else:
statements
Loop
Example: Output:
Loop
Nested for loop:
o Python programming language allows to use one for loop inside
another for loop.
Syntax:
for iterator_var in sequence:
for iterator_var in sequence:
statements
statements
Loop
Example: output:
Loop
While Loop:
• The While loop is used to iterate a part of a program repeatedly until
the specified Boolean Condition is True. As soon as the Boolean
condition turns False , The loop automatically stops.
• The while loop is considered as a repeating if statement.
• If the number of iterations is not fixed, it is recommended to use
while loop.
Syntax:
while condition:
statement
increment/decrement
Loop
Flow chart:
Loop
Example: Output:
Loop
Example: Output:
Loop
Else statement in while loop:
o The else clause is only executed when your while condition becomes
false.
o If you break out of the loop or if an exception is raised, it won’t be
executed.
Syntax:
while condition:
statements
else:
statements
Loop
Example: Output:
Loop
Nested while loop:
o Python programming language allows to use one while loop inside
another while loop.
Syntax:
while expression:
while expression:
statement(s)
statement(s)
Loop
Example: Output:
Loop
Loop control statements:
o Loop control statements change execution from their normal
sequence.
o When execution leaves a scope, all automatic objects that were
created in that scope are destroyed.
Three control statements:
Continue statement
Break statement
Pass statement
Loop
Continue Statement:
o The continue statement in python returns the control to the beginning
of the loop.
Example: Output:
Loop
break Statement:
o The break statement in python brings the control out of the loop.
Example: Output:
Loop
pass Statement:
o We use pass statement in python to write empty loops.
o pass is also used for empty control statements, functions and classes.
Example: Output:
Thank You