0 ratings0% found this document useful (0 votes) 45 views14 pagesPython Unit - 4-1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Promon onit-y Overs
Rr oO
4. How method overriding works in python?
Explain with an example progran:
Q discuss the implementation a€ yperaly
overloading en python wortee a progon that
overloads the + operaar © Hat (oe add
two objects of Clals proton.
ey consider @ Kectangle class and create Tid
progyan f&
Rectongle objects. Lirite p¥thon
check tohether the Area’ of the forse gectange
res) gieattr than second by ovetloading
> Operabr.
4 Hote about exteption handling
an example using try, else and finaly iz
ce and handle use’ defined
. Enplain wih
& How to create yall
— enteptions 1m python.
g Writ a python prograa to plement
multiple Encept blocks and finally Block).tow wniethed nerrtding wor br tr Potton)
Chplain with aty example pregrant,
) hatred pveNvi dung ae
"FE dapunee Suecame te provide aspecific
tnpltmentaten bo murmed already depiniel
ty Hay tuperciam,
* Matised bvewling i pardeutar ty Libel LA
SCEMOM OS Ucheml o fubclam needs be Lanance
Sy modify thy fon ctienatity Uinhe vited
Fromils tubeians white masntoairg the ovat
SHCA am of interface
Example |
Class Shape : ;
Curput
Aep mura (carp) Ie
pa preaty
Clans Sqyacird Cchape): Sena ia ae
GR oie (set sata):
Self aide « Side: 1
ath once tse4 >i .
vel Geig Side * corp cide
A Drives Code :
> squat Cro) a.
j print [Ares of 4a in, Smeal 7]ui, tee re pie rie Meat ton ooo make Fu heel
Goeth eo" Th han ala hives Ahtaed a
ies dracticrs. pe Ogee
BGuevatey Overleading
PAithm,we can change dye woo Opevaten’s tiov\e
Pee kel tate
ciarvyle, the + cperoden with pertovery achnedtc
of or Jur ruanberSy mag? two \tele, 0
are wD Bling ‘
amare ‘ny fh fallout the carne pe retor
p ‘dtlicse mening +
Bat - add (fay which teva % Fats
= t.d=- (A, £2) eae :
>, dodet the addPion cp “ oil oud the
way we eperttter
yee aly wh: nee, olhes ana asics
hi «Be eS a eR EN ie ECR age TRS Tpke ae oy ee
| Z _Consrder a Rectangle. Class and Create.
Two Rectangles Objects. Liytte fey thov)
program to check bihether the Arca of
the First Rectang le tt Greater. than Second
by overloading > Operator. : ‘
A
ee
Class Rectangle : .
def init (ergy) :
Cue. £=
SELF. 626
af arcatserpe):
‘geturn cele. 4 * Seip. b
def __ gt_— (tf, other):
Yeturn celP.areal) > Other. areal)
£12 cat (input-( enter Aergth 1 sup hows
b1 2 lat (input enter breadtha:")) ~
71 > Rectangle (1,41)
Ld cat (input Center Length 2: y
be me(inputC Enter breadth 20) > oFoutput:
fntcr length 4 2
Grtcr Areadth1 > 3
Giter Lereg th D5
Oittr breadth Q: Ss
R2 har greaterY binite about exception handling. Explain wry,
an example ustng try. ele and finally 9
A: Exception handling: wat
MAN exception (Ss an event, Which Occurt
during the execution of a program, that
disyupts the viormal flow of the program's
Grisiuctons. :
* An exception ws a python obfect that
wpresents AN error.
# When a pythora eript raives an exc Pplin,
& must ether handle the exception |
emmediattly otherwise Ct would terminate
. and come out.
x Pts importorit te handle exception propely
én your code using try- CKxCpl blocks or
other error- handling technig ues, in ordey |
gracefully handle errors and prevent
the program fiom Crashing. ; a* Syntanes (or Cxceptions
1 try... except
ty :
# Cok that may cause exception
except: .
4 Code t run Wher exception OCCUrs
lea Multiple except blocks
ty
H Code that moy couse euceplon
Crcept Exceptiorrt:
Gxception 1 haridiing Code
Except Excephiona:
Gucption 2 handlyig Lode
2 ty , encept, efse
oa
# code that
: May cause er Cop tionH Code that NAY Cate excep tion
pe:
Hop oral block
4 tendling of exceptron
| Cnetue VE 0 exception
Uy :
/ Lome code. .. (Always executed )Cxample:
#- try, else and finally
try
hus 50 )
de ? int (input (enter the denaminagtor: ’
yese (V4 /ae) ' :
priat( RIGTSVOK) EES ote o y
| Chept ‘ZeroDivistor Grrr : ‘)
iol
peat c oo) Oi a@ ZERO ry not allo
Bak:Z .
else =
ae
print( The reruwt of distor) Oper
(nally :
punt (End of pregram oeoutput’
Entcr the ce
norminato
, Yee
. performed Successful
e yesult of diviston otal 3
Gad of program. aWiese toc keyword ie © dumdo-nevt.
ryder, 4het cllows you ae dri
Tris can Lb
| fon te
99er On ercenl
© dime wth berth but
deferred @xteph os
uw" £3ce Pe,
Se cd Yee
hWe cen detre cunty., Excel.
fey Meeting a wes Sess thet ic derived tio, ,
| se Magi -in" Ceeerhom* clase
Synton
Sipe Ba
Clone Cvestunlrs or CExte etn) é
Pes.
Evay!
exter, See toen Crpov -
Hex? , Custcicewer fe VSerceLaed ayy wabyich
imhevs fon the Excepdion clace
&}
hoes Age_ Excembon(Cxce Phen):
Pia=
Oge = ind CABLE.
terAge:*))
Ca CGS < it:
Yor N94 Excepos
else:
Punt (te kg
tole sy Vete*)
SMEPE Age ccoption.
Prt (“Under oge .
CON Uebe 444 Mattipte exec
ept and Ficaty8 lock.
Prxixct (‘Handtin
TOU =SO
de — toe-(.
CES a Ges
Per
PRE Hn te ape denoroinaton')
A] ote)
Division Perforred Success Fully 1)
CxCept 2€70 Division Error:
Prist¢
Denornindtor aS Zes0 js TOE allacsedl!!
ae Cept Vorlere Evrors
ie EComty “Tntegers Should be Entered")
cles
Print C*
The wesult of divi
oaitty ’
S02 Operation is 373)
cD"
Cee Esa ar Progx.