Control_Structures
Control_Structures
Iterative construct
Python provide two types of looping construct
while
for
While loop
while test expression / condition :
statement(s)
[else:
statement(s)] optional
• When test expression written in while returns false then the else
statement if present, gets executed.
For loop
for var(iteratorVar) in sequence :
statement(s)
[ else :
statement(s) ] optional
• Sequence is any sequence data type viz list, tuple, dictionary.
• Else statement, if given, will execute immediately after for block, always.
• Apart from sequence type, for loop can iterate over range() function, file,
etc.
• range function
➢ range(start, stop, step)
➢ Generates a sequence of numbers beginning from start till
stop-1
➢ Step signifies the interval between the terms
➢ We can have negative value for step when start> stop
➢ First and third arguments are optional
➢ Default value of start is 0 and step is 1