0% found this document useful (0 votes)
1 views5 pages

Chapter-5 - Loops in Python

The document discusses the use of loops in programming, specifically focusing on 'while' and 'for' loops for various tasks such as printing numbers, creating multiplication tables, and searching through lists. It includes code snippets and practice questions to reinforce the concepts. Additionally, it touches on the use of 'break' and 'continue' statements within loops.

Uploaded by

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

Chapter-5 - Loops in Python

The document discusses the use of loops in programming, specifically focusing on 'while' and 'for' loops for various tasks such as printing numbers, creating multiplication tables, and searching through lists. It includes code snippets and practice questions to reinforce the concepts. Additionally, it touches on the use of 'break' and 'continue' statements within loops.

Uploaded by

unnatichowrasia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

chabter5

Loobs
Loobs are used_to reeat. instpuctiors.
while Roops
startng value
nile condHon:
# Statemertti
ncrement

Code:
C=1
while c<=5
print(hello")
Ct=1

codet
# Trint numbers rom 1to 10.
L=1
nhile i<=10:
þrint(1)
Lt=1

þrint (Locp ends


Prachce Auestons.
1. Print numbers from16 100,
2. Trnt number5from 200Lo 101.
3. Print the mulibi cation e table ofanumber n
4. PYunt the elements of rie follonng
4[1,4,9,16, 25,36, 49,64, 81, 100 ist using nhile loop.
5: Search for a number z in the
loop, above tuple usung whle
Code: Code:
*Ques.1 # Ques-2
L=1
200
nhile i<= 1D0: nhle i: 101:
þrint ("i=",2) print ("i2,).
Lt=1
Code: codes
# Ques. 3 # Ques-4
n= Üs int (input ( Enter n:")) L=4,4,9,16,25,36,49,c4,ai 40
i0
hile i<= 10: while iklen (L):
print(1,"x",L,"=", n*i) Arint (L[]).
Lt=1 i+=1

Code
# Ques-5
L= (1,4, 9, 16,25, 36, 49,64,81,100)
= float (input ("Ener ie numiber tb be searched:)
i-0
while i<Den():
print (Found at index",)
it=1

Ereak ed, Continue


Break: used to termiate tie Doob nhen encountered.
Continue: termates execuion n LHe currerit Lteration
andcontinues execution of tie loop wit; tie
next tteraton.
Codes Code:
i= 1
nhilei<=5: hle i<=5:
print () if(i--):
iF (i=-3): Lt1
break
printinue

print ("end of toop ") Qutbut


1

3 4
end of oop 5
for oos ae used for sequential traversal. For
traversing
Smtax:
ist, string ,tuples eto.
for el n list: Code.
# Stotementi #for Loo
N= 2,35, 4.6]
list- [12,3] for v in N:
for el in list: print(v)
prit el)
Cutput

29
3.5
4.6
Code :
str= "Betiune College"
for c in ste:
þrint(c) for Jooh ith elae
Output for el n list.
B # Stalementt
else:.
t # Stalemente

Tractice Quesions
1. Do the problem nos. 4 and 6 in hle loo section
L= ,4, 9, 16, 25,36, 49, 64,81, L00)
= loat (inbut (Enter t+e number to be searched
L=0
fop y in L:

Lt=1
pPint Fownd at index ",)
Rnqe functions retiurns a sequence of mumbers, starting
defaut, and increments by 1(by default),
from O by
mumber.
sþecified
nd stops before a

range (start? ,stop, step)


for el in ranqe (5):
print(eL)
or alprint
in (e)
el range(,5):
fer elþrint
in range4,5,2):
(el)!

Pange(10) # stop condition


eode:
#PrLnt all even mumbers rom 2 to 1O0.
Pange (2,101,2):
for i in Pange
þrint(9
Prociee, Questiens.
Ueing for Loop and rargel)
1. Print mumbers from 1 to 100.
eode'

print (i)
2 Print umbers fron 200to 101,
Code:
for i in ronge (pc0, 100,-):
þrint (
3 Print tie mulibicaton table DF a number n.
Code
e n= int (nput ("Enter number :"))
tOP L Ln
Pong",n*)
þrint (n,
kass Stlement
þass is a null staement tat does noliing is used
as
placeholder for futre code.
for el in range0):
þass

ractice Questions
1 WAP to find the sum. of first n namral numbers using
while eoo.
Code:
n= int (nbut CErnter he value of n:"))
while LK=N
Sum t
þPint Sum ofirst,nnaral numbeps is =",Sum)
2. WAP to ind the factorial of c gven number n
Code
using for Loop
n= int (nut ("Enter te Value of n:)
tact =1
for in Pange (1, n):
fact * 1
þrint (n," fact)

You might also like