DMPP Loops
DMPP Loops
MDI Gurgaon
1
Agenda
● Loop types
● While variants
● For variants
● Examples and Discussion
● Assignment
2
Loops
● A loop is a way to execute a statement repeatedly with appropriate flow control
3
Loop-while
4
Simple while implementation
● import random ● if guess == number:
● ● print('number is correct')
● number = random.randint(1,100) ● elif guess > number :
● print('guess a number between 1 and ● print('guess is higher')
100') ● else :
● ● print('guess is lower')
● guess =-1 ● count += 1
● count = 0
● while guess != number and count <5 : # read and implement listing 5.4,
● guess = int(input("enter the Subtraction quiz in class
number: "))
5
Sentinel controlled loop with while
● Often user can't predict accurately ● # SentinelValue.py
the number of times a statement or ● data = int(input("enter an integer
set of statements need be be or 0 to exit: "))
repeated
● sum =0
● In such cases user confirmation or
● while data != 0:
sentinel value is used to close the
○ sum += data
loop
○ data = int(input("enter an integer
● Syntax is as follows
or 0 to exit: "))
● contWork = ‘Y’
● while contWork == ‘Y’ ● print('the sum is ', sum)
○ Execute the loop
○ contWork = input(‘enter Y to
continue and N to quit’)
6
Try!!!!
● Please write a while based loop/s to display multiplication table as follows:
● 1 2 3 4 5 6 7 8 9 10
● 2 4 6 8 10 12 14 16 18 20
● 3 6 9 12 15 18 21 24 27 30
● 4 8 12 16 20 24 28 32 36 40
● 5 10 15 20 25 30 35 40 45 50
● 6 12 18 24 30 36 42 48 54 60
● 7 14 21 28 35 42 49 56 63 70
● 8 16 24 32 40 48 56 64 72 80
● 9 18 27 36 45 54 63 72 81 90
● 10 20 30 40 50 60 70 80 90 100
7
For loop
● A python for loop iterates through ● In general one can think of for as
each value in sequence ● for var in sequence:
● For is handy when you have a ○ Statements
clear idea of how many times you ● A sequence could be any kind of
want to run the loop collection
● So a control variable can be used ● for i in range(1,5):
to count the executions ● print(i)
● Such a loop is also called ●
counter-controlled loop ● 1
● Syntax is ● 2
● for i in range(start, stop): ● 3
○ statements ● 4
8
The range() function
● The parameters of the range function ● for i in range(1,6,2):
are start, stop, and step. ● print(i)
● start is an integer; beginning of the ●
sequence of integers that must be ● 1
returned. ● 3
● stop is an integer that is fixed ● 5
beforehand and indicates the last
●
● for i in range(6,2,-1):
number in the sequence of integers that
● print(i)
must be returned.
●
● step is an integer value that limits the ● 6
increment amongst each integer in the ● 5
sequence. ● 4
● 3
9
Try!!!!
● Please write a for based loop/s to display multiplication table as follows:
● 1 2 3 4 5 6 7 8 9 10
● 2 4 6 8 10 12 14 16 18 20
● 3 6 9 12 15 18 21 24 27 30
● 4 8 12 16 20 24 28 32 36 40
● 5 10 15 20 25 30 35 40 45 50
● 6 12 18 24 30 36 42 48 54 60
● 7 14 21 28 35 42 49 56 63 70
● 8 16 24 32 40 48 56 64 72 80
● 9 18 27 36 45 54 63 72 81 90
● 10 20 30 40 50 60 70 80 90 100
10
Use while and for to calculate GCD
● GCD of a number is the highest Common factor
● Approach??
● How to tackle the problem??
● In class exercise
11
Case studies
● GCD using while ● GCD using for
● def calgcd(n1,n2): ● def calgdcf(n1,n2):
● k=2 ● gcd=1
● gcd =1 ● k1 = min(n1,n2)
● while(n1>=k and n2>=k): ● for k in range(2,k1+1):
● if n1 % k==0 and n2 % k ==0 : ● if n1 % k ==0 and n2 % k ==0:
● gcd = k ● gcd =k
● k += 1 ● return gcd
● return gcd ●
● b1 = calgdcf(5,3)
●
● b1
● a=calgcd(5,3) ● 1
● a ● b2 = calgdcf(20,5)
● 1 ● b2
● a = calgcd(20,5) ● 5
● a
● 5 12
Keywords break and continue
● There may be times during the execution of the code when a pause or
termination of the code is necessary (also known as an abnormal loop
termination)
● Generally, to stop or pause the looping statements, special control statements
are used
● Code performance deviates from its usual pattern due to these iteration loop
control statements
● To instantaneously exit the body of a loop or recheck the condition(s) from the
inside of the loop, these control statements are used
● These include break, continue and pass
13
Case Study: Years to double the tution
● def yearsToDouble(tution): ● Please solve the problem using
● counter = 0 for loop
● while(counter < 20):
● if tution > 20000:
● print(counter)
● break
● tution *= 1.07
● counter += 1
● return counter
●
● n1 = yearsToDouble(10000)
● 11
14
Using continue
● # add all numbers till n but for 10,11 ● def addallf(n):
● def addall(n): ● total =0
● counter=0 ● for counter in range(1,n+1):
● total =0 ● if counter ==10 or counter ==
● while counter < n:
11:
● counter += 1
● if counter ==10 or counter == 11: ● continue
● continue ● total += counter
● total += counter ● print(total, counter)
● print(total, counter) ●
● ● addallf(11)
● ● 45 11
● addall(11) ● addallf(12)
● 45 11 ● 57 12
● addall(12)
● 57 12 15
Use while or for to identify palindromes
● Palindrome is a string which is reversible, such as 121, 1331,LOL etc
● Approach?? (we discussed)
● How to tackle the problem??
● In class exercise
16
Case Study : checking palindromes
● def palin(t1): ● Use while for solving the problem
● palindrome=True
● for counter in range(0,len(t1)//2):
● if
t1[counter]!=t1[len(t1)-counter-1]:
● palindrome = False
● print('palindrome is', palindrome)
●
● palin('1331')
● palindrome is True
● palin('mom')
● palindrome is True
17
Case Study :
● Print first 50 Prime number ● Assignment for coming
● In class exercise class
● 5.19,5.20,5.21, 5.23, 5.26,
5.30, 5.32, 5.33, 5.59
18
pass Statement
19
Thanks !!!
Q and A
20
References
● Sridhar,S., Indumathi,J., Hariharan, V.M., “Python Programming”, Pearson,
2023, First Impression
● Liang, Y.D., “Introduction to Python Programming and Data Structures”,
Pearson, Third Edition, Second Impression, 2024
● Bibliography
●
21