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

Python Assignment 02

Assignment of python

Uploaded by

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

Python Assignment 02

Assignment of python

Uploaded by

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

Page No.

Date

Ncme tr_RahulR Nisha d


class
Rythan
TYGBA CA

Nhat is a class in Pathan2 a eimple


class deination for a Person cuith athbubes
name and .ag and. a method qreet that prin t
a qreehing mafSaqe
A class in Python is a blue print fo Crea hinq
obiecls (inskaces) t de Fines attribuke (vonlablkal
and methods(Functions that describes the behavju
and State of Es o6jects, 14 alaot for modlelaa
meceulan
beus ableand organized Code i
oaramclass PenSon
definit -- (self, name, qe)
SelEndme name
seIf
def qaee! CsetJ
prtntCP Helo my hame ie self name
and| am iselfaqe yeans old.")
#usage
feiton 1 Pens on calice" 30)
frso n1
Paga No. 2

what is inheni tonre 2


In henitane a featu in Python
is Puthon that
that. alloaJ S
one class_(the child cas) to inherit propenkies &
behaviouns Cmethod) om ano then clas (the paneot
Class). This promote code eHGe and creoteg
hierarchical elafion.ships bekceen clases
EProqyam
class Animal
def sound CselfJ:

class Dog (animal).


def Sound (selA):
print ("Bant< |")
class cat CAimal):
def soundCsef) :
priat (Meow ")

doq: Doqt)
dogSourd()
Cot Cat )
Cat Sourd
Fege Ne 3

Q3 What is, cugtom eKCeption 2 wnte a proy that define


a ustom exception Negativehm ountbro Thit.
eKCepBlon Shauld be raised when trying to
depos it or (withcracs a neqafire anounY in a
bank a(Coun t class.

Acustom exception t a den- cleinedercephlon


class thot inherits Arom fgthon't builts- in- Erception
ctass (t all owsyouto Creat Specifrc. eror type
to Suit the logic of yown proqram

roq ram
class Necgatív eAmount Error (E xception):
pass
class Bonk Accouht
def-initc sel E, balance oJ:
Self bolance balan ce
det deposii (setf, amourtJ:
Yoi se Negot l'e Amount Enor (^ (onnot depocit
aneqatie Qmount M
self balance + amount
del cuithdraw (sel, amountJ:

Yaise NeqaliwA mountEmor( "Canno othrow Q


heg afire amount! ")
if amount self. bcalance
aise watveAmounttror "nsufficient Aunde")
gelfbalance Qmoun
fsagR
accoun t Bankhccoynt ((o)
frg acoin depos'+ C-S
eKp erpPect Negatiepmount Error ase}
Pin (e)
(Page No. s.
Dato

LQshat is the uce of upenC)n python2 wte


demonstrctnghocs Supest) used to.
a metho

uSedto coll methods of epanent


-ea child clags This is ponticalanly
ininheni tan cehen you coant to
exknd o modify the behauio u af panent
classs method

Rrogtam
clas lanent
det --inite (sel f,ngme)
Seiname nome
det Shoc name gel:
print ( " Name: setfname?")
cla ss ehtpchild Panent)
def -init (setf naMe, aqe)
Supe). -- in it hame )
sele-age aqe
Shos- defcuileCsetIfJ:
Sup n() Shoos nameU

ehilde child cAlice , 20)


childshoucleta'l C)
Page No. Os
Date

a prog,demos tratin muliple hhenitanae


Muttiple allows
inhenitan ce allows a class to inhenlt.
from mot than one panen t c lass The
Subctass inher't attrlbutes andnefhods
Prom all the,panentclassee

class engine
de Start- engine Cself) e
prini(engine Standteal "')
class Body i
def build -bodyCset f) :
priotCbodybu'lt:h)
class canl engine Gody) i
def cen assemble (Self) :
Sel Stt enqine()
Self. buid'- body t

Cay a sSEm bleJ


Paga No.
Date
N

Howdodo you handle mutiple excep fiont 2? orite


6HoD

tnpython, youCan h andlemultiple excepti ons


usino
multip>e
single ty excep!6lock byspecifyiny.
erception type This is usefal cohea
you er pect di fPenen ypes of enors.

Pragrami
def devide-andaccess Ait4 Istnde tydiuto)
try
esult = Ast Cinden1/diyisor
prin( p"Result esulf ")
eKCept lndexlxoy !
print (" nder out of range "
eKcep 2exo0iysion Eror
pint( Canno) diyide by 2exo |")
erCepi execpton al e

#usage
narabens a Cio,20,30]
tvide -cndaaress Cnumben, S,2)
divide- anda access (numbens, , a

You might also like