Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

while loop & nested loop

Uploaded by

Saksham Goel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

while loop & nested loop

Uploaded by

Saksham Goel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Iterations(Loops)in Python

INFORMATICS PRACTICES
CLASS XI

AISMV-----INFORMATICS PRACTICES-----XI
Recap of for loop

AISMV-----INFORMATICS PRACTICES-----XI
WAP to find sum of the series:
s = 1 + x + x2 + x3 + …… +xn

x = float(input("Enter value of x "))


n = int(input("Enter value of n (for x**n) :"))
s=0
for a in range(n+1):
s += x**a
print("Sum of first", n," terms : ",s)

AISMV------INFORMATICS PRACTICES -----POOJA THAKUR


WAP to find sum of geometric series given below:
s = a+ ar + ar2 + ar3 + …… +arn

r = float(input("Enter ratio "))


n = int(input("Enter max value of the series:"))
a = int(input(“enter the first term”))
Sum=a
for i in range(1,n+1):
term=a*(r**i)
Sum=Sum+term
print("Sum of the series is ”,Sum)

AISMV------INFORMATICS PRACTICES -----POOJA THAKUR


WAP to find sum of the series:
s = 1 - x + x2 - x3 + ……. xn

x = float(input("Enter value of x "))


n = int(input("Enter value of n (for x**n) :"))
s=0
sign = +1
for a in range(n+1):
term = (x**a) * sign
s += term
sign *= -1
print("Sum of first", n," terms : ",s)

AISMV------INFORMATICS PRACTICES -----POOJA THAKUR


Learning Objectives

Control
While Loop statements in
a loop

AISMV-----INFORMATICS PRACTICES-----XI
while loop : Entry controlled Loop
Syntax:

while expression:
statement(s)

Necessary Conditions:

1) Initialization
2) Terminating Condition
3) Iteration

AISMV-----INFORMATICS PRACTICES-----XI
while loop allows multiple ways of iteration
Fixed number of steps

i=1
while(i<=10):
print(i)
i=i+1

AISMV-----INFORMATICS PRACTICES-----XI
While loop also works when number of steps are not known

Example

i=100
while(i>0):
print(i)
i=i/3

AISMV-----INFORMATICS PRACTICES-----XI
To count number of digits
n=int(input('Enter a number'))
c=0
while(n>0):
c=c+1
n=n//10
print(c)

AISMV-----INFORMATICS PRACTICES-----XI
While loop to do digit extraction
n=int(input('Enter a number'))
while(n>0):
print(n%10)
n=n//10

AISMV-----INFORMATICS PRACTICES-----XI
WAP to input a number and find the sum of
digits of the number.
n=int(input('Enter a number'))
s=0
while(n>0):
s=s+n%10
print(n%10)
n=n//10
print(“The sum of digits are = “,s)

AISMV-----INFORMATICS PRACTICES-----XI
To do: To reverse a number using digit
extraction
n=int(input('Enter a number'))
s=0
while(n>0):
s=s*10+n%10
n=n//10
print(s)

AISMV-----INFORMATICS PRACTICES-----XI
To do activity: Find if a number is
palindrome by using digit extraction
n=int(input('Enter a number'))
s=0
t=n
while(n>0):
s=s*10+n%10
n=n//10
print(s)
if(s==t):
print("Palindrome")
else:
print("Not a Palindrome")
AISMV-----INFORMATICS PRACTICES-----XI
Create menu driven program using while
loop
while(True):
print("1. Add")
print("2. Subtract")
print('3. Multiply')
print('4. Divide')
ch=int(input('Enter your choice'))
x=int(input('Enter the first number'))
y=int(input('Enter the second number'))
if(ch==1):
print(x+y)
elif(ch==2):
print(x-y)
elif(ch==3):
print(x*y)
elif(ch==4):
print(x/y)
else:
print('Invalid choice')
AISMV------INFORMATICS PRACTICES -----POOJA THAKUR
Write a program to display the menu for calculating the area of a circle, square, triangle and
sphere. Depending on the choice of the user, the inputs should be taken and the are area
should be displayed.

Sample Output:
Calculate Area:
1. Circle
2. Square
3. Triangle
4. Sphere
Enter your choice: 2
Enter the side: 4
The Area is 16

AISMV------INFORMATICS PRACTICES -----POOJA THAKUR


Find if a number is an Armstrong Number.
A positive integer of n digits is called an Armstrong number of order n (order is number of digits)
If abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....
153 = is an Armstrong number. 1*1*1 + 5*5*5 + 3*3*3 = 153
120 is not a Armstrong number. 1*1*1 + 2*2*2 + 0*0*0 = 9

n=int(input('Enter a number'))
d=len(str(n))
t=n
s=0
while(n>0):
r=n%10
s=s+r**d
n=n//10
if(s==t):
print("Armstrong")
else:
print("Not an Armstrong number")
AISMV------INFORMATICS PRACTICES -----POOJA THAKUR
Practice Question
Neon number:
A number is said to be a Neon Number if the sum of
digits of the square of the number is equal to the
number itself. Example 9 is a Neon Number. 9*9=81 and
8+1=9.Hence it is a Neon Number.

AISMV------INFORMATICS PRACTICES -----POOJA THAKUR


Infinite Loop
i=10
while(i>0):
print(i)

while(True):
print('Hello')

i=15
while("i"):
print(i)
i=i-1
Find the output:
i=0
while i < 3:
print(i)
i += 1
else:
print(0)

x = "abcdef"
while i in x:
print(i, end=" ")

x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")
❖Loop control statements change
execution from its normal sequence.
Loop Control
Statements ❖When execution leaves a scope, all
automatic objects that were created
in that scope are destroyed.
Loop Control Statements

break continue pass


Loop Control Statement : continue
It returns the control to the beginning of the loop.
Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.
Here print() statement is
for letter in 'Python’: condition
if letter == 'y' or letter == 'o’:
continue
print('Current Letter :', letter)
When variables are y or o
print will be skipped
Output:
Current Letter : P
Current Letter : t
Any statements below
Current Letter : h
continue inside the loop will
Current Letter : n be skipped
continue statement : Find the output
i=11 Output:
s=0
10
while(i>0): 8
i=i-1 7
5
if(i%3==0): 4
2
continue 1
print(i) Sum 37
s=s+i
print("Sum",s)
Loop Control Statement: break
Terminates the loop statement and transfers execution to the statement immediately following
the loop.

for letter in 'Python’:


if letter == 'y' or letter == 'o’: Here the loop will terminate
break as soon as ‘y’ or ‘o’ character
print('Current Letter :', letter) is iterated

Current Letter : P
break statement : Find the output
i=1
while True:
if i%2 == 0:
break
print(i)
i += 2

i=0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)
Using break statement in Programming
Finding a program to find if a number is prime

n=int(input('Enter a number'))
srt=int(n**0.5)
for i in range(2,srt+1):
if(n%i==0):
print("Not Prime")
break
else:
print('Prime')
In Python programming, the pass statement is a null statement.

The difference between a comment and a pass statement in


Loop control
Python is that while the interpreter ignores a comment
entirely, pass is not ignored.
statements:
pass
pass means do nothing
Example of pass statement
for i in range(1,10):
pass
print(i)

for i in range(1,10):
if(i%2==0):
pass
else:
print(i)
Iterations in Python
(Nested Loops)
INFORMATICS PRACTICES
CLASS XI
Loop within a
loop is called
Nested Loop

Nested Loops

Used in Used in
generating creating
sequences patterns
#Output
11
Nested loops for a sequence 12
13
14
for i in range(1,5): 21
for j in range(1,5): 22
print(i,j) 23
24
31
32
33
34
41
42
43
44
To Generate a pattern
for i in range(1,5):
for j in range(1,5):
print('*',end=’’)
print()
#Output
****
****
****
****
Triangle of ‘*’
#Output
for i in range(5): *
for j in range(i+1): **
print('*',end=’’) ***
print() ****
*****
Exercise: Generate a pattern
#Output
1
for i in range(1,6): 12
for j in range(1,i+1): 123
print(j,end='') 1234
12345
print()
Exercise: Generate a pattern
#Output
1
for i in range(1,6): 22
for j in range(1,i+1): 333
print(i,end='') 4444
55555
print()
Exercise: Generate a pattern
#Output
54321
n=int(input('enter a number')) 5432
for i in range(1,n+1): 543
for j in range(n,i-1,-1): 54
print(j,end=’’) 5
print()
Exercise : Generate a pattern
y=input('Enter the alphabet')
y=ord(y) #Pattern
for i in range(65,y+1): A
for j in range(65,i+1): AB
print(chr(j),end=’’) ABC
print() ABCD
ABCDE
ABCDEF
ABCDEFG
Practice Question
#Output
Write a program to display the triangle of *
‘*’ given as output * *
* * *
* * * *
* * *
* *
*
Find the Output
x=0 #Output
for i in range(10): 9
for j in range(-1, -10, -1): 18
x += 1 27
36
print(x) 45
54
63
72
81
90
WAP to input the value of x and n and print
the sum of the series:
s=
x = int(input("Enter value of x "))
n = int(input("Enter value of n (for x**n) :"))
s=x
sign = +1
for a in range(2,n+1):
f=1
for i in range(1,a+1):
f *= i
term = ((x**a) * sign)/f
s += term
sign *= -1
print("Sum of first", n," terms : ",s)

You might also like