0% found this document useful (0 votes)
82 views59 pages

Loops

Here are the steps to solve this problem using nested loops: 1. Outer loop to print the rows from 1 to 5 2. Inner loop to print the number of #/stars based on the row number 3. Inner loop to print the numbers from 1 to row number with spacing 4. Increment the number to be printed by the row number in each iteration So the code would be: for i in range(1,6): for j in range(i): print("#",end="") for k in range(1,i+1): print(k,end=" ") if i>1: print(end="+") print(i*(i-

Uploaded by

Vasu Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views59 pages

Loops

Here are the steps to solve this problem using nested loops: 1. Outer loop to print the rows from 1 to 5 2. Inner loop to print the number of #/stars based on the row number 3. Inner loop to print the numbers from 1 to row number with spacing 4. Increment the number to be printed by the row number in each iteration So the code would be: for i in range(1,6): for j in range(i): print("#",end="") for k in range(1,i+1): print(k,end=" ") if i>1: print(end="+") print(i*(i-

Uploaded by

Vasu Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

ITERATION

Repetition of statements certain


number of times till a particular condition
1. STARTING VALUE
2. END VALUE / TESTING CONDITION
3. WORKING/BODY OF THE LOOP
4. INCREMENT / DECREMENT PART
A special variable : loop variable is used on which loop revolves.
REVISION OF FOR LOOP
Eg 1 : Loop variable
for i in range(1,10):
print(i)
End value
i will go upto 9
Because of
start value range function
i will start from 1
Eg 2 : what is this
for i in range(2,11,2):
print(i)
Step value
i will increase by 2
Eg 3 :
for i in range(12,1,-1):
print(i)
Step value
i will decrease by 1
Every time loop rotates
Find the output :
a,b=6,4
for i in range(0,a+1):
print(i,end=" ")
print()
for j in range(0,a+1):
b=a
a=a+1
b=b+1
print(a,"-",b)
Output:
0123456
7-7
8-8
9-9
10 - 10
11 - 11
12 - 12
13 - 13
Find the output :

v1,v2=5,10
for x in range(1,4):
v1=v1+1
v2=v2-1
print(v1,v2)
print(v2-1,v1+1)
Output:
69
87
78
78
87
69
CONCEPT OF
BREAK
AND
CONTINUE STATEMENT
IN PYTHON
Break statement is used to exit
immediately from a loop.
This work for both for and while
loops.
If you are using nested loops then
break statement will stop the
execution of
innermost loop And start executing
the next line of code after the block.
Continue : this statement is used to
skip the block of the code in a for
And while loops.
A continue is shortcut which take the
Control to the loop without executing
the rest statement inside the loop
Please watch this video for the practical
implementation of
break and continue….
Find the output :

a=int(input("enter num 1"))


b=int(input("enter num 2"))
for i in range(a,0,-1):
if a%i==0 and b%i==0:
print(i)
break
WAP program to takes in the number of
terms and find the sum of series:
1 - x2/2 + x3/3 - … xn/n
n=int(input("Enter the number of terms:"))
x=int(input("Enter the value of x:"))
sum1=1
for i in range(2,n+1):
sum1=sum1+((x**i)/i)
print("The sum of series is“,sum1)
WHILE LOOP
Also known as entry control loop because
it first check the condition then execute The related block.
Starting value

Testing
condition

Body of
The loop

Increment part
This print statement if not part of while loop Of the loop
Program.
WAP to obtain a number
and then generate
even number up to this
number using while loop .
n = int(input("enter a number "))
i=2
while i<=n:
print(i)
i+=2
Converting for to while loop and vice versa

b o a r d e x a m s
p p a r t fo r
v.Im
Video
FIND THE OUTPUT AND CONVERT THE CODE USING FOR LOOP

n1=0
n2=1
c=1
while c<=12:
print(n1,"@",n2)
n1+=n2
n2+=n1
c+=2
print("c = ",c)
n1=0
n2=1
for c in range(1,12,2):
print(n1,"@",n2)
n1+=n2
n2+=n1
print("c = ",c)
FIND THE OUTPUT
w=10
i=0
while w<20:
print(w*i)
i+=2
if i==8:
w=20
else:
w=w-3
FIND THE OUTPUT AND CONVERT THE CODE USING FOR LOOP

i=x=0
while i<20:
if i%5==0:
x=x+1
print(x)
i=i+1
x=x+1
print(x)
FIND THE OUTPUT if n=11 and n=14
n=int (input(“enter a num”))
i=2
while n%i!=0:
i=i+1
print(i)
FIND THE OUTPUT
n=7583241
f=s=0
while n>0:
r=n%10
if r %2!=0:
f+=r
else:
s+=r
n=n//10
print(f)
print(s)
print(f-s)
FIND THE OUTPUT
n=42
a=m=0
c=1
while n>0:
a=n%2
m=m+(a*c)
c=c*10
n=int(n/2)
print(m)
CONCEPT OF INFINITE LOOP :

i=1 Be careful while using a while


while True: loop. Because if you forget to
if i%3 == 0: increment the counter 
variable in python, or write flawed
break logic, the condition may never
print(i) become false. In such a case, the
i += 1 loop will run infinitely, and the
conditions after the loop will
starve. 
PROGRAM FOR PRACTICE : WHILE LOOP

1.WAP to print first N natural numbers


and their sum.
2.WAP to input a number and then print
its digits in reverse order.
3.WAP to find greatest number between
N numbers.
NESTED LOOPS
A loop with in another loop
Outer loop

Inner loop

Outer loop work for 3 times


Inner loop works for 4 times
Then.
Total number of iterations will
12 times
NESTED LOOPS IN PYTHON
Nested for Loops Python
You can also nest a loop inside another.
You can put a for loop inside a while, or a
while inside a for, or a for inside a for, or
a while inside a while. Or you can put a
loop inside a loop inside a loop. You can
go as far as you want.

NESTED LOOP VIDEO


NESTED WHILE LOOP

for i in range(1,6): *
**
for j in range(i): ***
print("*",end=' ') ****
*****
print()
NESTED WHILE LOOP

i=6
while(i>0):
j=6 *
while(j>i): **
print("*",end=' ') ***
j-=1 ****
i-=1 *****
print()
WRITE PYTHON TO CODE PRINT THE FOLLOWING PYRAMID :
PYTHON CODE

for i in range(1,7,2):
for j in range(i):
print("*",end=' ')
print()

Also convert the nested for loop to while loop


WRITE PYTHON TO CODE PRINT THE FOLLOWING PYRAMID :
PYTHON CODE

for i in range(1,6):
for j in range(i):
print(i,end=' ')
print()

Also convert the nested for loop to while loop


WRITE PYTHON TO CODE PRINT THE FOLLOWING PYRAMID :
PYTHON CODE

for i in range(1,6):
for j in range(5,i-1,-1):
print(j,end=' ')
print()

Also convert the nested for loop to while loop


WRITE PYTHON TO CODE PRINT THE FOLLOWING PYRAMID :
PYTHON CODE

for i in range(6,0,-1):
for j in range(1,i):
print(j,end=' ')
print()

Also convert the nested for loop to while loop


FIND THE OUTPUT :
x=0
for i in range(0,5):
for j in range(0,i):
x=x+(i+j-1)
print(x)
print()
print(x)
OUTPUT :
FIND THE OUTPUT :
k=0
x=0
for i in range(0,5):
for j in range(0,i):
k=i+j-1
if k%2==0:
x=x+k
elif k%3==0:
x=x+k-2
print(x,end="")
print(x,end="")
OUTPUT :
FIND THE OUTPUT :
x="Education"
for i in range(len(x)):
for j in range(0,i+1):
print(x[j],end="")
print()
OUTPUT :
REVISION : TOPIC ITERATION
Q-1

Q-2
Q-1

Q-2
Q-3

Q-4
Q-3

Q-4
Q-5
Q-5
Q-6
PROGRAM FOR PRACTICE:

# 12345
1
### 2 4 6 8 10
13
##### 3 6 9 12 15
135
### 4 8 12 16 20
1357
# 5 10 15 20 25

You might also like