0% found this document useful (0 votes)
44 views

Unit 2 Python Operators and Control Flow Statements Part2

The document discusses different types of loops in Python including while, for, and nested loops. It provides examples of how to use these loops and also covers loop manipulation techniques using keywords like continue, break, pass, and else.

Uploaded by

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

Unit 2 Python Operators and Control Flow Statements Part2

The document discusses different types of loops in Python including while, for, and nested loops. It provides examples of how to use these loops and also covers loop manipulation techniques using keywords like continue, break, pass, and else.

Uploaded by

Sakshi Bhosale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Proqram

print" Enter 3 numberS")


nihtinput)2)
n gint (input o
n3 int (input O)
if n n 2
ifniz ha:_
print(n1," i9 larqes t ")_

print (na," is ar qest")|


If ngnj
if ng ng
print (n3 ," is largest '

O/p
fnter3 numbers
15
25
35
35is larqest

1Looping in Python
loopinqtatemen tS care to execute
block
Ihe
of Statement.Sepeabeelly for severatimes

or until speciPied remauns tue


condition

-Python provides twa types gf leaping


Stctements

while loap
for loap
1The while loop
Same that
Python'S while loop 1S eractl as
in Jay4
we usedn an d C C t t .
The only Suntax is chanqed
- Syntaz
initialization
whilecondition
Statement-1

Statem ent-2-

Statement-pL
increment/decrement
willepeat
After nitializinq, the while oop
condition vem ans
its iteroations until Specified
every iteratOn it will per form
Irueand oO
9pecified increment decyement.
increment operator
-Python does not Support
t ) a n d decremen E operator =
to pTint"Hello all"
Python progrGrm
messaqe l o timeg.

i-1
while iK : 10:
print (Hello all)
:it1

Sum of all mumbers


Pythan_ Proqrom
Pind to
iS
entere
entere d by
1 t o num where hum
from

the uSer
a
value: "2
pint("Enter
num int(input ())
Sum O

while iK:num
Sum:Sum t
ii+1
print(Sum i9:, Sum

o/p
Enter a value:10
Sum iS55

2The for loopi


The for- loop 1S not at al ike uwe uSed it
inCctt and Java
Syntaz
tor counter i n sequence
Statement-1
Statem ent-2

Statem ent - n
fallawinq progYam wheze weinclude rangec
Pram to Lo:
func tion t o prIDt alL the numberS
print ("manarea 8tarts")

far i in 1anqe(i,11):
printCi)
a r e a ends")
print ( majn
Start Writing From Here

3 Neste d LoopS
-Python also allows to us the cancepb et
nestedloops, where we use one loop inside
anotheer loop
The very
-Ihe veny- famoug_ patternproqrams and
man
ather Situations ta use ne sted loopg.
Pythonprogram to print fottowing pattern'

for i in range (L,6)


forj in range(1,i+1)
print(" +", end=" ").
rint)
Here
Here in case ot
nested loopsinner loopuill
iterate and pTint*" where as_cutey
locp
makethe inner oap to iter ate
-And by default _print function will PTint
in new ne. iessaqe
Loop Manipulation :
-Like other programminq Ianguages, Pythan
ta pertormn oqical
loqca
also prOvides keywordS
and ezecution
manipulation in behaviour
af an loap
- Python provide9 four Keyworcdg to perfom

loop mani puations.


Keyward "Continue
. Keyword"break"

3 keyword pass"
"
else"
4 Key word

Marupudationg uSingkeuwod "continue"


.Loop back
o1d Continue" takes the control
-Keyw
next
to the condition and performs
loop's
iteration by gkippinq StatementS of cuent
iteratioon Prom the occurenceof
Conti nue.

for £zam ple


for letter in puthon"
i f letter "h"
Continue
print (CuYrent Letter is letterL

o/P Curtent letter iSp


Current Letter is: y
Curent Letterist
Cure nt Letter is : 0
Current Letter iS:n
2Loop Manipuationg usinq Keqwasd" break"
-keyword"breaK Immediateu terminate e
,

the ezecuti.on of an1oop and tokes the


Controls out af the l0Gp
Puthon Progam

while 1K:5
printSEaIt'1
fi-3:
bresk
print ( End" i)
ii+
O/p
Start
End

End:2

Lasp Manipulation usina Keqward"pass"


KEwrd pais is used uhen a Stctementis
Facically or logically reqw red bu
we do
nor want_any code to ezecute for it
*and Tava it is ike emptypaîr
o fCurly bracesE})
Puthan Program
retter in Puthan
ifletter h
pass

el$e
print("Current Letter:", letter)
0/P
Current Letter:P_
CuTrent Letter Y-
Current Letter: t
Currenb Letter , O
Current LetteY,n
00P Manipwation usinq keyword "else "
nPython , a loop_Stctemen tg may have
o nelse clauge l t S e1ecuted hen he
100p ter munat e 8 throuah exhaus tign of iStt
-or when the condition esult9 false
Ciowhile- loop)
Pythan program
whlei :5 :
Punt C)
i2i+1
elSe
pint ("1oop ended ")_
O/P
5
loop ended

You might also like