

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
These notes whill help you understand about the concepts of looping in Python.
Typology: Lecture notes
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















alasSMAte
Date
Page
Repeatrtion
Hetction
is
Hhe ptogiamingconghruchaPthon
ohereset ot
statemehsare
teratedbut upto
a
limit
gthon suppaa aypeso loops
ifor
ibahile
orlosphereisa heeod ota specialunction
which is callelasrauae he songe tuncchionaPthon
ist wh en Special sequenceOstule.A seuene
yaluesbaund-toetherbya
Singlenane,Seuence dupes_conbe stringlist,
uples
ete
8enerates
InPahouisa 6ccessiono
Tangelauaerliait,upperlinad
range
1,
,2,3,4,5,,7,8, 9
The Cange kunchion canalsobeused indilherentdecas
where dhe numbersCan be enerated_hauingapsat.
mOse
than one
NadDe-
fan (lowerliait,upperlinit,step_vadu
Tange
L1,10,
,5,7,
Largeo,25,
lo,14,1, 12
alassmate
Date
Page
here isOnemose typect can e unctionwhichprinds
a sequence inrevers.eordler-
Tongellaigherlinib,lowerlisit,ncoahve stepvalue)
ngeC10,
0,-D
Io ,1,a,5,A,3,2,
IGnge(10, 0 )
O, 7 4,
he
Heroction stotement or laopinq
aullo_a set
o
insdtuctions do be
pertormedrepeatedl until
sondtionia ulkiled Ihe iterotioh
sta emeuisarealso
Cclled loapsor lopiag giatexe rts. Pdhon suppocis
inads
loops
ounting-
loops
keps dhatsepeats
certeiuset
statenMesaor c
potticula
loepsE ar laaa
Coaditional Loop lhe loaphei tepeats sekot stade buente
Until
happens e fllhe
conelition renas
dtoe ER
while loop
orloops
for loop in PuAhon is
process the itehns or oa-
sequence
cuch_as list or Ctiin
One oy One
dor
in
sequence
Chet ot
GAatement(s)
elasSMAte
Date
Page.
Aor ia£cnge C29,56,2)
prit
()
OR
k
S6)
arind
Ci
wtea Pchoncodewhihprixds_no.et"1oo 300 whuish
aredivisilaleh3 lbut hatoivisilaleS
dor
0i)
/ 3== and i%5l=o
pria
Ci)_
a tanseLi62,2.01,3)
L=0:
pri
C)
Whea Pthencode uoliehpriutsnasLet Got 15
or inranAR G0,26,-1)
Rrini
CA
QWide a Prihoncode which
ano printa
nnah2ralwmoers
n n Cinpui
Endeynodurnas)
tor iin t@ngeU,nt)
elassMAte
Date
Page
SSti
pcint
(s
Q apt a ho Qnd ind cut dactorialo hathe.
nind CnpuiLuder aLne)
dor ia
Tanaen,
o,)
prid
C
put a no pri uHipli.cciadable..
nit (ipud Aerana)
cange
,
pri
h,x,ie,
t)_
Qnput
a
singlediait
hanpristfhe
s4uptodirst
n_has
QFinl
out he
averoge odrst
naura nosS
QW
He o Pthon
cadle tshich
ihputanth
Hetm
&prie
ollowing Deries
,
11, ll,
1l11,
11,
= in
CinpuiEper
der
ha
oriin
ange
1,
nt)
lassMAtee
Date
Page
naidlinputCEaderasialedig ao
or n tanae ,ni)
S nxn
prindt)
2
Cmpu(Ent nahural
dor iinanaeU,nt
h:
itCinput
Edec
a nahralno2)
oriia
fenge
,n+)
S
il
pria
S)
3
nllhind
CinputEnderth
der2))-
SEO
Hor
iinLanae
n+1)
SS
prind
(s
.wridea Pthoncode toprixtS o lefiouoturalhos.vaika
orloop
S=O
in
ronal
)
SEStiL
pri
(S)
neo
elassmate
Date.
(
Page
hle
A uhile
loapis
a conditional
locpha
i)
repea
heinstrue
wthinksel as
long
as he
condition
remaias
true Syntaxtor
whileloop is
inidialisation
whilecOnditioaa
odya
mae
Ea
while
i<11:
priati)
lheloop bady mc containone or maose sladermedsoreven
passstadermendeWhen dhe conditionexpressiakaecoestake
He
cotol
dransiersor pss
es do the
lbode her
ore
cotol_
elemeds in whileloop wlaich serve
lerert
purposes lheycreas dolloes
bitioli zoion
Pehore
enteringidohelocp lboal itsloa Yariablehusibe
initialised.lhe
iaitialisatienot loopp yarialle takesplace uneder
initialisotionsecAioy A
Is
execuded onl once and generallydeclae
ouisideAhe whilelosp
es
Expression
he ies expressionsoh expressionuhahe truthvalue decide
hether Ahe losphsdy beexezutedarnadIhetes
exptession evduodes
tcuehelos bad Aetsexeauie dher
elasSMAte
Date
Page
We a Pthon code which priniseven hos drom 59 bla
i
63
uhile iKB9 Cind i:16iand i920-
printCi)
i-
OR
i: 59
while
i>: 16
PtinCi)
i-=
QwrHe dactoria
siugwhile loap
nz imimput(tter cno.)
whileiEni
pri Eacerial
is,D_
OR
nainhCinpoiEe
ano)
while
iz=
elassmate
Date
Page
i
pri
(Fcchorid
is
wrie a Phoncodle slaichiaptsa no Apri sm ol
ase
add noslaiehasedivisilleey 7_oa,henas vories
beheen G o 23
naid(inpd(Euderons
uwhilen: 61 and nk33 cnonh7:0ond n2=o
N 63
hile= and e133 and i%0 dnd ih2o:
whilei
K= 133
i 2l=0 end i%1=O
prinCs)
nput dodaleat pespeinaparticolaslacalitEidout
taltan o eligiuleVeierslaeLderinge aanincdividunl
int Cipui(Eter
e nea
people)
C
while iK=n:
intKpu
Enler
ce
22
cCt
elasSMAte
Date
Page
nputa na uhichprintsmutipleteachdliaitseno
ne itCanpuhC Enlerono.2)
S=
whilenl=o_
5S
intCn/1o)
rimp)
5=p
prind
Cp)
Qnpuda no.pcinds reverse
hait inpul nler ana)
revCD
while nl=o
rey reyAl
+t
printReverseis reu
Qp iwonswridea coda inPgthon which prisistno Coisao
Hhe power second nundaer wthoud UsiMa Pa
naiCnpullEnterlsine)
ME in(inputCEer2n a.)
i
p
mEoA
prinf
Cp)
else
while K=m
elasSMAte
Date
Page
C)
ppthn
i+
prial(h raiedn_pauerm,isp
Q rieo Pthoucode hichprindssom d endered hombers bu
he user, alsoaindhe averoge
ch=
wilech== a clh=Y
naintaput(Enterono )
S=S+n
da inptDouOu wa _adldmore nos.CY/N2)
print Sum oh voes iss)
printArercge hosis g
Nesterd
Loops
A locpntCockaactherloepinds hcdy thi,ot lsapi
Calledaestedloeplaancstedleptheianerleejp_ausdderniate
loehore dhe
Suatax
lor
lcop
or
loopi
alassmate
Date
Page
Oriaranac ,6l-
Aorin
fanae(1i) L2-
eno
))
3
)
2
3
4
2
3
45
k=
orin angeU,5)
0ri
fange(,
i+):> 2 3
pridCk.end 2
Printk:k+1_
4 5
78 9 10
prim)
1Q
2222
33 33
2Q 23 4
1234
23 44
dar n fenge (1,4)
inGnge
,
PriniK,
ead=)
20
or nsangeA5)
n rans.e(L,E)
prind
Lend=
printc)
elssmute Date Poge
23 4 5 7 8 2
12 22 forinronge(U,5): Aorjin fonge 1,it1): psint h,end= kk 2 printo 2.Q . 43 2 5 A 32 5 43 5 A O 5 QL iinan ae ,G) derin fonge 5, 4- print k, end 2 kh- print
k terin
(1, 6)_ horin
(1i
lasSMAte Date Page 2 k-o HorirChae CL,A) ar ronge
prindCk, end) k- -x print) Hor iinionse(1,4): Y h JGn ceU, 5): pridi end ) -print
1Qk= arin Lange 4) darj infonge(15) primK, end=
k:k+ print) dump Stademet Pthenreles woup sicdeCuds tolkeused Ldhi laspsa mp cut c locpiHescicAS
ae oreah
stadement
The oreak stedement enahesaprogramdoshipcverapasis ade
derinaes hever locp dliesthin. Execuiesesuaesd heskeneniaaoocciadelydellowingthe uea he Aetinadedstademed.
elansmste
Date
Suahax
inihalischio
os
ysicoein
seauEnte
lo.op
siahemerdes)
stodemenlls)
itondiien
icoaditicni
OR oreak
break sadenenils)
stadeneh (e)
shate
statermeiat(s)
QWrite_oPthaucede skihpridsSu nos beiween 10
0 b asSosn as à prorecossesioothe corolshouldca
odothe
loop
o inronge (10, 30
SS+i
S
break
print(
coinue sdade mend
heCotine steàemend is ancthersdedejomp seiese,ikee
breah stodeaem the ocihthestaienem 16 Scc patocode
u the Ccxinve siademer is Sone hatdiherent dfonbreak
instealo or cina derinahon
_placeskippin
Ony code n lbehoeen