0% found this document useful (0 votes)
13 views16 pages

Python Looping and Iteration Explained

The document explains iteration statements in Python, which allow repeated execution of instructions until a condition is met. It covers the 'for' loop, including its syntax and usage with the 'range()' function, as well as the 'while' loop and its structure. Additionally, it discusses jump statements like 'break' and 'continue' that control loop execution.

Uploaded by

Parul Kapoor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views16 pages

Python Looping and Iteration Explained

The document explains iteration statements in Python, which allow repeated execution of instructions until a condition is met. It covers the 'for' loop, including its syntax and usage with the 'range()' function, as well as the 'while' loop and its structure. Additionally, it discusses jump statements like 'break' and 'continue' that control loop execution.

Uploaded by

Parul Kapoor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Looping / Iteration

• The iteration statements or repetition


statements allow a set of instructions to be
performed repeatedly until a certain condition
is fulfilled.
• The iteration statements are also called as
Loops or Looping Statements.
Conditional
Counter Loop
Loop
(For loop)
(While Loop)
For Loop
The for loop of a Python is designed to
process the items of any sequence, such
as a list or a string, one by one.
range( ) function
range( ) function returns a sequence
of numbers, starting from 0 by
default, increments by 1 (by
default), and stops before a
specified number.
Syntax
range(Start, Stop, Step)
Optional
Argument. Required
Optional
An integer Argument.
Argument.
number An integer
An integer
specifying at number
number
which specifying
specifying
position to at which
the
start. position to
incrementat
Default is 0. stop. Last
ion. Default
. value not
is 1.
included.
Examples
Statements Values Generated
range(10) 0,1,2,3,4,5,6,7,8,9

range(5,10) 5,6,7,8,9

range(3,7) 3,4,5,6

range(5,15,3) 5,8,11,14

range(9,3,-1) 9,8,7,6,5,4

range(10,1,-2) 10,8,7,6,4,2
for Loop
The for loop of a Python is designed to process
the items of any sequence, such as a list or a
string, one by one.
for variable in
Colon (:)
<sequence>:
Statements to repeat
This determines
Here the how many times
statements the loop will get
to be repeated.
repeated
will be
placed
Examples of range( )
0
1
for i in range Start = 0 (by 2
(5): Default) 3
Stop = 5 (not 4
print (i) included) >>
Step =1 (by >
default) 2
3
4
for i in range Start = 2
5
Stop = 10 (not
(2,10): included) 6
print (i) Step =1 (by 7
default) 8
9
>>
>
Examples
2
4
for i in range Start = 2 6
(2,19,2): Stop = 19 (not
included)
8
print (i) Step = 2 10
12
14
16
18
>>
P >
S=‘Python’ Traverse each y
for i in S: character of a t
print (i) string one by h
o
one. n
>>
>
Examples

10
S=[10,20,30,40, Traverse each 20
50] element of a 30
for i in S: list one by 40
print (i) one. 50
>>
>
While Loop
A while loop is a conditional loop that will
repeat the instructions within itself as long as
the condition remains True.

Conditi False Exit from


on ? the loop
True

Statement 1

Statement 2
Anatomy of a While Loop
A while loop has four elements that have
Initializat
different
ion
purposes.
Test
Body of
Update of
Expressio Expressio
Expressio Loop
n n
n
Syntax of a While Loop
This
Initialization condition is
While <logical tested, if it
expression>: is True, the
body of loop
body of loop is executed,
……………. The loop
……………. ends, when
the condition
update counter evaluates to
variable False
Example of While Loop

N=1 Initialization
While Expression
Test Expression
N<=10:
Body of Loop
print(N)
Update Expression
N+=1
Jump Statements

Python offers two jump statements to be used


within loop to jump out of loop iterations.

CONTIN
BREAK
UE
Break Statements
• The break statement enables a program
skip over a part of the code.
• A break statement terminates the loop it
lies within.
• Execution resumes at the statement
immediately following
for <var> the body of
in <sequence>:
terminated statement.
statement 1
if<condition>:
break
statement 2
statement 3
statement 4 Loop
statement 5 Terminates
Example of break statement
i=1
While i<=10:
if i==4:
print(“Statement in if”, i)
break
else:
print(“Statement in else:”,i)
i+=1
Print(“Statement outside while”)
Continue Statements

• Instead of forcing termination, the continue


statement forces the next iteration of the loop to
take place, skipping any code in between.
• The continue statement skips the rest of the
statements and causes the next iteration of the
loopfor <var>
to take in <sequence>:
place.
statement 1 Continue
if<condition>: will cause
skipping of
continue statement
statement 2 2 and 3 in
statement 3 the current
iteration
statement 4 and next
statement 5 iteration
will start.
Example

for i in range(1,10):
if i%2==0:
continue
else:
print(“Statement in else:”, i)
Print(“Statement outside for..”)

You might also like