Sometimes we need to repeat certain things for a particular number of times.
For example,
a program has to display attendance for every student of a class. Here the program has to
execute the print statement for every student. In programming, this kind of repetition is
called looping or iteration
To carry out repetition of statements Python provide 2 loop statements
◦ Conditional loop (while)
◦ Counting loop (for)
for loop
for loop in python is used to create a loop to process items of any sequence like List, Tuple,
Dictionary, String
It can also be used to create loop of fixed number of steps like 5 times, 10 times, n times
etc using range() function.
What is sequence?
Sequence is set of value such as a list or a string. Python has a function range()which holds
set of values or sequences.
Python range()function is used to iterate over a sequence of numbers by specifying a
numeric end value within its parameters. The general format is:
Here,
• Start and step are optional.
• Start is the staring number of range. If the start argument is omitted, it defaults to 0.
• stop is the last number of range. That is the exact end position is: stop –1.
• Step is the increment.
range() function
For e.g.
▪ range(1,10) will generate set of values from 1-9
▪ range(0,7) will generate 0-6
▪ Default step value will be +1 i.e.
▪ range(1,10) means (1,2,3,4,5,6,7,8,9)
To change the step value we can use third parameter in range() which is step value
For e.g.
range(1,10,2) now this will generate value [1,3,5,7,9]
▪ Step value can be in –ve also to generate set of numbers in reverse order.
▪ range(10,0) will generate number as [10,9,8,7,6,5,4,3,2,1]
To create list from 0 we can use
range(10) it will generate [0,1,2,3,4,5,6,7,8,9]
Example: Let us print first 10 natural numbers using for loop.
Program to print table of any number
Operators in and not in
The operator in and not in is used in for loop to check whether the value is in the range / list or not
For e.g.
>>> 5 in [1,2,3,4,5]
True
>>> 5 in [1,2,3,4]
False
>>>”a” in “apple”
True
>>>”national” in “international”
True
Example – for loop
# write output here
# write output here
# write output here
Practical work
1. WAP to enter any number and find its factorial
2. WAP to print the following fibonacci series 0, 1, 1, 2, 3, 5, 8,……..n terms
3. WAP to enter 10 number and find the sum and average.
4. WAP to enter Lower_Limit, Upper_Limit and find the sum of all odds and evens number
between the range separately
Loop Control Statements
Loop control statements are used to transfer the program's control from one location to
another. Means these are used to alter the flow of a loop like -to skip a part of a loop or
terminate a loop .
Control Description
Statement
break Terminates the loop statement and transfers execution to the statement
immediately following the loop.
continue It is used to skip all the remaining statements in the loop and move controls
back to the top of the loop.
pass This statement does nothing. It can be used when a statement is required
syntactically but the program requires no action.
break statement
The break statement is used to terminate the loop.
1
2
3
4
Loop finished
when the value of i reaches to 5 condition will becomes True and loop will terminate and message
“Loop finished” will be printed
Continue
The continue statement does the opposite of the break statement. The continue statement is
used to tell Python to skip the rest of the statements in the current loop block and to continue to
the next iteration of the loop.
1
2
3
4
6
7
8
9
Loop finished
Current value: 9
Current value: 8
Current value: 7
Current value: 6
Current value: 4
Current value: 3
Current value: 2
Current value: 1
pass statement Current value: 0
Pass statement in Python does nothing. You use pass statement when you create a
method that you don't want to implement, yet.
while loop
While loop in python is conditional loop which repeat the instruction as long as condition
remains true.
It is entry-controlled loop i.e. it first check the condition and if it is true then allows to
enter in loop.
while loop contains various loop elements: initialization, test condition, body of loop
and update statement
while loop elements
1. Initialization : it is used to give starting value in a loop variable from where to start
the loop
2. Test condition : it is the condition or last value up to which loop will be executed.
3. Body of loop : it specifies the action/statement to repeat in the loop
4. Update statement : it is the increase or decrease in loop variable to reach the test
condition.
Example of simple while loop
i=1
while i<=10:
print(i)
i+=1
Example: WAP to enter any number and find its reverse
Assignment : Programs of while loop
Convert all ffor loop program with while loop
WAP to enter any number and find its reverse
WAP to enter any number and a digit and count how many times digit is in the number
WAP to enter any number and check it is armstrong or not
WAP to enter any number and check it is palindrome or not
WAP to enter numbers as long as user wishes to enter and find the sum highest and
lowest number entered by user