hs51GMENT
PrOBLEM
AND
SONING
PyTHON
PROGRAMMING
DONE By
'aRRSHan'1
E -|"
("la) >>pnt
helo) hai>spin(
l! hella hai
)
ellb") ("haihello
s>>pinthai\
hello') ha>>>Ri
i hellont ha?
In
Example Seauence Escape
STRINGS SEQUENCE
IN EscAPE
89 456T 32I O
dgts) (sting Output: Pint
Punctaton) (sti ng RRnt
string Tmport
Example
madule-name împort
SyuTax
def?ned useY- Our împort weasway
ame the modulesmport
ed beCanmodules Standard
as Python
?s ofbay modwle
extendedd *Standard
SBatemens tions, Func
ns. contarig
hn frle amodul
i8 e *A
DeBail Modulus
în 84ing about Explaîn I.
LIST As ARRAy:
An anay is a Collectfon of S?miliax elements.
Elements n he amay an be a ccessed by ?ndex -Tndex
Start wth o. Aray Can be handled n Pyhon by module
hamed array
" To Create array have to Pmport amay module
Sn he piotam
Syntax :Irmport aay
Ex
a= aNay -antay (r? 2,3,4J)
a-’aYYay name
ar1ay Module-name
art ay - huncton-name
>nteger daBaty pe
Example.Frnd Sum of ay elements
Pmport
&um=o
orYay
a= arsay- array (i'. ,233)
&um- Sum tÉ output 10
prînt (sum)
CoNVERT L15T INTO ARRey:
froml?st() functon 9% used to append Irst to
artay- Hence , Hhe
he lst fs8 act Ifke a
aay
SyNTex : atiayname -Fromlist (Ist-name)
Ex Convest lRst into aray
mport asy
L= [6,,8,9,5) output:
35
as acayaray Ii',[)
a.fsomlist ()
3um= 3umt f
Pr?nt (Sum)
METHODS IN ARRAY
a-2.3.4,5)
SyNTAX ExAM PLE
ati ayldata ype value l'st)
append () arrayl?'.[2.3.4.5]
a.append (6)
append) L2,3,45,6]
nsert (îndex, element) afnsert (2,10)
Pop (?ndex) L2,3,10,56)
a popi)
îndex lelement) L2,10,5,6]
ReverSec) a.?ndex C2)
CountC) a-seversel)
Lb5,10 2)
a. countl)
a. Explan about Condttlonal Alternaffve and chaîned cond?tPonal in
detatl.
CoNDITIONAL 8TATEM ENT:
Any objects Can be tested For truth value For
Use ?n an
or while Cond? Ponals as operand oF he
Boolean opexofonal belew
Ex >> 52=O:
False
D
Pe statement
IF statement contafns
a logteal expresston ustng
dota ?s Compoved and dec?sion Ps made based on wieh
of Cornpaison he tesult
Syntax 9F2expessi on s
true statement
FLOW CHART:
Test
Expression
False
bodg of PF statement
True
Ex:
PentC"a is equal to io")
output
ais equal to \0.
2) TF-else CAltesnative) statement:
mere are two &tabenents to be execute d
alternatvely hen PF-elke statement Ps used.
je Te Ae Cond?on îs true , Hhen tue statement axe
otheyuise statement oF else part îs executed
exe uted
SyNTAx: iF expresston
8tatements
else:
8tatements
Flowchart
TeST
false
EXPRESION
BoDy oF else
True
Boy oF PF
FLOW CHART:
Test
Expression
False
bodg of PF statement
True
Ex:
PentC"a is equal to io")
output
ais equal to \0.
2) TF-else CAltesnative) statement:
mere are two &tabenents to be execute d
alternatvely hen PF-elke statement Ps used.
je Te Ae Cond?on îs true , Hhen tue statement axe
otheyuise statement oF else part îs executed
exe uted
SyNTAx: iF expresston
8tatements
else:
8tatements
Flowchart
TeST
false
EXPRESION
BoDy oF else
True
Boy oF PF
Ex: a=|0
Print C" a îs equal to 1o)
el6e
Pint ( a is not eaual to 0)
output
a fs eaual to 1o
3) IE -eltf -else Cchafred condi onal):
* Tt 93 used to test more than one cond?fon.
k DF there are mote Hhoan uo atenaternves fo
Select, hen nested PF statement axe used.
SyuTax
?F expressfon:
body ofiF
elif expess?on :
body of elif
else
body oF else.
FLoWCHART
ErPRESSiN false
ue False
ExpRESS0oN
Tue
BoDy oF elif
Bory oF else
out put
RrRne C"a is egual to io")
Print ( a îs leser han 1o)
elif as10
Pit (a îg qreater Hhan io)
else:
Print( a Ps not equal to i0")
Output: a Ps letter hon io.
3- Expla?n wih an example while loop break 8tatemet and
Contnue 8taBernent în yiton.
· Nhile loop:
entry ontsolled loop
exewtes a block oF Code
Conditfons becomes true Yepeated ly ?| he
ÖyNTAx: while expr ess Pon:
Statement
FLONcHART:
wle expression:
Condiorn
Tyue
CondPtonal code false
Ex Count o
while (c ount 2s)
Pint ( The Count value î s , count)
Count - Countt1
Output:
The count value ?s O
The ount Value ?s
The Count Value is
The count value Rs 3
The Count Value îs Ly
2- CONTINUE STATEMENT
t This Yeacton Cortsol to Hhe begtrng of he loop
* WE re fects all temaming 3tatement Pn the lunent
Ptexatfon of he loop and moves Hhe Contol kack to he top
of the loop
* T Can be used both n while and For Ioop
`yNTAX: contnue
fLONCHART:
Cond?fonal code
True
Continue
Conditon
False
Example:
for x Output
X=2
Conttne X= 3
3. BREAk STATEMENT
Control out oF he loop.
* Teminates the loop Statenent and trans fer exe cution
to the statement Pmmesfately Fotloung the lop
SyNTAx break
fLDNCHART:
Co hditonal code
True
Cond?ton/ break
false
Ex: For x Pn Yange
C115) : output:
bieak
X=2
X=3
X=4
4. frd a Pghn prog sam to Fnd he &quave voot of he hunber
def neuwtonsy1tn):
oot =n2
Toot =Croot tnoot)2
Pr?nt (root)
n= eval Cinput ("enter number to Find
newton sax t n) 34,rt: ) )
Output.
entes number to Find &qxt:9
3.0