looping in python
looping in python
Note-2: Atleast one parameter must be there in python. If there is only one
parameter, then it will be considered as ‘stop’ value. If there will be two
parameters, then these will be ‘start’ and ‘tart’ respectively.
CONTROL STATEMENTS 49
INFORMATICS PRACTICES (065)
If range() function will generate ‘n’ number of values, then the ‘for’ loop will
execute for ‘n’ times.
In ‘for’ syntax, ‘var’ is the variable that takes the value of the item inside the sequence
on each iteration.
Exit Loop
Statement1
Eg(1):
for i in range(1,6):
print(i)
Output:
1
2
3
4
5
Note: in above example, range generates numbers from ‘1’ upto ‘6’,but not including ‘6’.
Eg(2):
for i in range(1,6):
print(i,end=' ')
Output:
12345
Eg(3):
for i in range(6):
print(i,end=' ')
CONTROL STATEMENTS 50
INFORMATICS PRACTICES (065)
Output:
012345
Note: in above example, there is start value. The range will starts from 0(zero), if there is
no start value in range.
Eg(4):
for i in range(6,0,-1):
print(i,end=' ')
Output:
654321
Eg(5): Write a python program that displays the odd numbers and even numbers separately from
1 to given number.
#Filename: odd_even_nos.py
n=int(input('Enter number:'))
print('The Even Numbers Are:')
for i in range(2,n+1,2):
print(i,end=' ')
print('\nThe Odd Numbers Are:')
for i in range(1,n+1,2):
print(i,end=' ')
Output:
Enter number:10
The Even Numbers Are:
2 4 6 8 10
The Odd Numbers Are:
13579
Eg(6): Write a python program that find and displays the sum of natural numbers from 1 to given
number.
#Filename: sum_natual_nos.py
n=int(input('Enter number:'))
sum=0
for i in range(1,n+1):
sum+=i
print('The sum natural Nos=',sum)
CONTROL STATEMENTS 51
INFORMATICS PRACTICES (065)
Output:
Enter number:10
The sum natural Nos= 55
Eg(7): Write a python program that finds and displays the sum of odd and even numbers from 1
to given number.
#Filename: sum_odd_even_nos.py
n=int(input('Enter number:'))
sum1=0 #to store odd sum
sum2=0 #to store even sum
for i in range(1,n+1):
if i%2!=0: # here, checking whether 'i' value is odd or not
sum1+=i
else:
sum2+=i
print('The sum Odd Nos=',sum1)
print('The sum Even Nos=',sum2)
Output:
Enter number:10
The sum Odd Nos= 25
The sum Even Nos= 30
Eg(8): A simple python example that illustrates ‘for’ loop with sequence.
Output:
60
Eg(9):
s='python' #here, 's' is a string
for i in s:
print(i)
CONTROL STATEMENTS 52
INFORMATICS PRACTICES (065)
Output:
p
y
t
h
o
n
Note: In above example, first ‘i’ value will be ‘p’, then ‘i’ value will be ‘y’ and so on..
(b) while loop (conditional loop)
This is conditional loop, i.e. the while loop will execute again and again until the condition’s result
is False.
Syntax:
while condition:
#whilebody
False
Condition1
True
While body
CONTROL STATEMENTS 53
INFORMATICS PRACTICES (065)
Eg(1):
n=int(input('Enter Number:'))
i=1
while i<=n:
print(i)
i+=1
Output:
Enter Number:5
1
2
3
4
5
Eg(2): Write a python program that find and displays the sum of natural numbers from 1 to given
number (using while loop).
#Filename: sum_natual_nos.py
n=int(input('Enter Number:'))
i=1
sum=0
while i<=n:
sum+=i
i+=1
print('Sum of natural Nos is:',sum)
Output:
Enter Number:10
Sum of natural Nos is: 55
Eg(3): Write a python program that find and displays the series of Fibonacci upto given value.
#Filename: Fibonacci_nos.py
CONTROL STATEMENTS 54
INFORMATICS PRACTICES (065)
while c<n:
print(c,end=' ')
c=a+b
a=b
b=c
Output:
Enter Number to print fibonacci series:100
The fibonacci series is:
0 1 1 2 3 5 8 13 21 34 55 89
3) Jumping Statements
There are two jumping statements in the loops of python. They are:
(a) break statement
(b) continue statement
False
Condition1
True
Yes
break?
No
Remaining body
Exit Loop
Eg(1):
s='uselessfellow'
for i in s:
if i=='f':
break
else:
CONTROL STATEMENTS 55
INFORMATICS PRACTICES (065)
print(i)
Output:
u
s
e
l
e
s
s
Note: in above example, if ‘I’ value is ‘f’ then the ‘break’ will execute. That means, ‘for’ loop
execution will be aborted.
Eg(2):
a=10
for i in range(1,a+1):
if i==5:
break
else:
print(i)
print('hi')
print('EOP')
Output:
1
hi
2
hi
3
hi
4
hi
EOP
(b) continue statement
The continue statement is used to skip the rest of the code inside a loop for the current iteration
only.
Loop does not terminate but continues on with the next iteration.
CONTROL STATEMENTS 56
INFORMATICS PRACTICES (065)
Flowchart of continue
False
`Condition
True
Yes
continue
No
Remaining body
Exit Loop
Eg(1):
s='uselessfellow'
for i in s:
if i=='f':
continue
print('hello')
else:
print(i)
Output:
u
s
e
l
e
s
s
e
l
l
o
w
Note: In above example, if ‘i' value is ‘f’ then ‘continue’ will execute. Once ‘continue’ will execute
then print(‘hello’) doesn’t execute.
CONTROL STATEMENTS 57
INFORMATICS PRACTICES (065)
Output:
Enter number:3
hi
hello
uf
bye
hi
hello
hi
hello
uf
bye
Eg(1):In the example, for loop ‘else’ part will execute, because ‘for’ will execute successfully.
s='python'
for i in s:
if i=='h':
pass
else:
print(i)
else:
print('End of the for loop::')
Output:
CONTROL STATEMENTS 58
INFORMATICS PRACTICES (065)
p
y
t
o
n
End of the for loop::
Eg(2): In the example, for loop ‘else’ part doesn’t execute, because ‘for’ loop execution will be
aborted when ‘i' value in ‘h’.
s='python'
for i in s:
if i=='h':
break;
else:
print(i)
else:
print('End of the for loop::')
Output:
p
y
t
Flowchart
Flowchart is a pictorial representation of an algorithm to solve a problem.
Symbols used in flowchart.
Start/Stop
Process
Condition / Decision
CONTROL STATEMENTS 59
INFORMATICS PRACTICES (065)
Flow Control
Connectors
Input/ Output
Start
Read a
Read b
c=a+b
print c
Stop
CONTROL STATEMENTS 60
INFORMATICS PRACTICES (065)
Eg(2): Write a Flowchart that finds biggest among given two numbers:
Start
Read a
Read b
True False
a>b?
print a print b
is big is big
Stop
Pseudocode
The informal representation of steps to solve a problem is called pseudocode.
Eg(1): Write a pseudocode that finds biggest among given two numbers:
Sol:
Step1: start
Step2: read values into a and b
Step3: if a values is greater than b , then print a as an output, otherwise print ‘b’ as an output.
Step4: Stop
Decision Trees:
This is a tool that represent hierarchy of steps based on decisions.
CONTROL STATEMENTS 61
INFORMATICS PRACTICES (065)
If houseNo!=4
False True
False
Deliver food
True If houseNo!=4
False True
False True
Nested loop:
The loop with in the loop is called nested loop.
The nested loops are used to represent rows and columns.
The outer loop represents number of rows and the inner loop represent number of columns.
Eg (1): Write a python program that prints the pattern as follows.
1
22
333
4444
55555
666666
7777777
88888888
999999999
Solution:
Note: In the given problem, there are 9 rows. That’s why outer loop should execute for 9 times.
In first iteration one 1 should be printed, in second iteration two 2’s , 3rd iteration three 3’s
…should be printed. That’s why the inner loop should execute from 1 to iteration number
of times.
for i in range(1,10):
for j in range(1,i+1):
print(i,end='')
print()
999999999
88888888
7777777
666666
55555
4444
333
22
1
Solution
for i in range(9,0,-1):
for j in range(i,0,-1):
print(i,end='')
print()
Eg (3): Write a python program that prints the pattern as follows.
1
12
123
1234
12345
123456
1234567
12345678
123456789
Solution:
for i in range(1,10):
for j in range(1,i+1):
print(j,end='')
print()
Eg (2): Write a python program that prints the pattern as follows.
123456789
12345678
1234567
123456
12345
1234
123
12
1
Solution:
CONTROL STATEMENTS 63
INFORMATICS PRACTICES (065)
for i in range(9,0,-1):
for j in range(1,i+1):
print(j,end='')
print()
CONTROL STATEMENTS 64