Iterative Statements in Python, Lecture notes of Computer science

These notes whill help you understand about the concepts of looping in Python.

Typology: Lecture notes

2018/2019

Available from 02/10/2023

probalkar
probalkar 🇮🇳

7 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
alasSMAte
Date
Page
Repeatrtion or Hetction
isHhe ptogiamingconghruchaPthon
oheresetot
statemehsare
teratedbut
upto alimit
gthon suppaa aypesoloops
ifor
ibahile
orlosphereisaheeod ota specialunction
which is callelasrauae he songe tuncchionaPthon
ist wh en SpecialsequenceOstule.Aseuene
yaluesbaund-toetherbya
Singlenane,Seuence dupes_conbe stringlist,
uples
ete
koruSin
8enerates
InPahouisa6ccessiono
Tangelauaerliait,upperlinad2
range 1,
10)
,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
TangeL1,10,22
,5,7,9
Largeo,25,
4)
lo,14,1,12
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Iterative Statements in Python and more Lecture notes Computer science in PDF only on Docsity!

alasSMAte

Date

Page

Repeatrtion

or

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

kor uSin

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

stadeend,

aullo_a set

o

insdtuctions do be

pertormedrepeatedl until

eertain

sondtionia ulkiled Ihe iterotioh

sta emeuisarealso

Cclled loapsor lopiag giatexe rts. Pdhon suppocis

tuo

inads

o

loops

ounting-

loops

Ihe

keps dhatsepeats

certeiuset

statenMesaor c

potticula

nUnberot times

csekhaanas

LeUding

loepsE ar laaa

Coaditional Loop lhe loaphei tepeats sekot stade buente

Until

certaihdthing

happens e fllhe

conelition renas

dtoe ER

while loop

orloops

for loop in PuAhon is

desiened to

process the itehns or oa-

sequence

cuch_as list or Ctiin

One oy One

dor

variable

in

sequence

Chet ot

GAatement(s)

elasSMAte

Date

Page.

Aor ia£cnge C29,56,2)

prit

()

OR

range

k

S6)

arind

Ci

wtea Pchoncodewhihprixds_no.et"1oo 300 whuish

aredivisilaleh3 lbut hatoivisilaleS

dor

n Yonae100,

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

inputsn

ano printa

suno Isd-

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

S SK 1O

.wridea Pthoncode toprixtS o lefiouoturalhos.vaika

orloop

S=O

in

ronal

)

SEStiL

pri

(S)

neo

elassmate

Date.

(

Page

hle

Loop

A uhile

loapis

a conditional

locpha

i)

repea

heinstrue

wthinksel as

long

as he

condition

remaias

true Syntaxtor

whileloop is

inidialisation

whilecOnditioaa

odya

loap

updatestade

mae

Ea

while

i<11:

priati)

lheloop bady mc containone or maose sladermedsoreven

passstadermendeWhen dhe conditionexpressiakaecoestake

He

cotol

dransiersor pss

es do the

lineaterlaop

lbode her

ore

aany lonp

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

to

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:

e

intKpu

Enler

ce

22

cCt

Print

CTstaleligiLle

votersore,c)

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

cuerloop

Suatax

lor

lcop

or

loopi

alassmate

Date

Page

Oriaranac ,6l-

Aorin

fanae(1i) L2-

Prid

eno

))

3

print

)

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=)

print

OL

20

or nsangeA5)

n rans.e(L,E)

prind

Lend=

printc)

elssmute Date Poge

HM

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

ronge

(1, 6)_ horin

fange

(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

H

1Qk= arin Lange 4) darj infonge(15) primK, end=

k:k+ print) dump Stademet Pthenreles woup sicdeCuds tolkeused Ldhi laspsa mp cut c locpiHescicAS

hese

ae oreah

dnd cachaue

stadement

d break stedevnend

The oreak stedement enahesaprogramdoshipcverapasis ade

A bceak siademe

derinaes hever locp dliesthin. Execuiesesuaesd heskeneniaaoocciadelydellowingthe uea he Aetinadedstademed.

elansmste

Date

Suahax

inihalischio

os

ysicoein

seauEnte

olale

lo.op

siahemerdes)

stodemenlls)

itondiien

icoaditicni

OR oreak

break sadenenils)

stadeneh (e)

shate

atatl£)

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

thecctinuedake

_placeskippin

Ony code n lbehoeen