Python Sem2
Python Sem2
Hello alord
elcom e to Javcl point
JavaTpoint to Welcome
prit p.reado)
tetcThis DeL)
FclOsing the i\e COntert
afereadg ile
ting neo Coret to he
Macos
pa Open(Write _demotd
Sample. oct ") w')
ole welcome to Phottve.com fP,mte (te)
This)s asomple.to
Line 3
PintlDone iing)
y t ecept taemert orth eample.
-’Try 4 eccept stetements ou ujed to cotch handle
e cepttons in python, stetemerts that can raise ecepons
aUe tept instde the try Cauie the Stetem ent that
handle the ecception ove written inside ecept clauue
eannple
(et acces the aT ay elemet cohose indey
of bound e handle the cOTTespomcing
elceptt on.
Python Proqram to handle simple runti me error
a=,2, 3
Second e emert 2
An
Python
Pytho rovdes teyeoomd finaly ,ohic h s alwaus
eeeCut eo after the try and except blocks. The
final blocE afte nomal teming
tÌon of hry bloc afte ry blo ck terin Cttes dur
Some eceoti on
0. Polymor phisro in object oented python.
Poly morp higm conains 2 wordpo ly CMoTphi? Po ly me ans
many, 4morph means shupe. By polymorphis m, we onderstan d
4not
tha one teuk can be pertonedLin olitferent cwayi
for ecample -yOu have a
Specue. Buut they spedk di ffeno oty. tkee theCsPeaE"
be hav ior T3 Polymorphic in a sense d oepens m th
animab (ike doqs t cts) hove a con Crete implementa
of the atton 'speak"
prrint db connectton)
op <usql.conn ectoT . connecon MysoLonnecton obiect at
Ox 000002338A4C6B O0>
Here output shous the Con necthon ected
0 PetchoneC), FetchaliC),fetchmany J methods to redd dasa
Rrorn mysq1taole
fetchong C) -ek Ches +he nexk oo ln the rest of a
Ctch CLII Petrieves
C a auey 4 metuns them as (t st f tupfes
fet chnany) Similar tothe fetchone C) but +
et eves ne nect s ows inthe r e s t
query, instead t a sinqle roo.
db cursor db
COnnetion, cUrsoT)
Here (reting datobaye teble as studet'
CUsor. ececte (ceEATE TABLE Studet (id INT,Dame
VAeCHAe (os))
# ek databaye tab le)
dbcosor, ececute (SHUW ABLES')
Por table in db Cus
Prit (tab le)
olp (lstudent,)
oefinit (oetf)
rint(This s non parrametized conthructe)
dee show (ieC,nane):
rint ('Helo" nam)
Gtudent tudent C)
Student how (Iohn'")
his is nUn
Hello John
+throws ZerobivisinEr0Y or q 3
b= /a-)
tE th0ws NameÃr if d>4
print (C value of b= ", b)
try
fun (3)
funls)
brucesC) a e neCess ars here for
tt mwltiple
ecepti ons,
Coccept Zerobivision erro
Print(zerogivisionError occumed d Handled")
ecept Nam eCrro:
pint "NameError occued d Handled')
ZeroDivsiontm 0ccuUh ed d Hanled
IH you Comn ent on the ine fuol(3), the oupt wrll be
NameGror Occored
a - ,2,3]
try
Print (second element = o/o d oo a 1 )
tt Tbows e m r r s n e there are on ly elemets in
array.
print (" Fowrth element = o(o d" o)s(a l3]))
eg Cept.
Prt t (An e r r occumed)
Secon d elem ent =2
An err oCeured
padding
Piot ( The left aigned shi ng t )
prit Cstri.sjust (40)4'))
tt printing the mght aigned thinq.
)
with
Print he padding
ight aligned shring is")
Print (trl mjust(40,))
Center aigned string orth fillchr:
$$*$$ I love JavaTpoit $44134
The let al'qned strnq is!
Llove TavaTpoirt& 4e&lR444 ¢4
The maht aliqned string Ro!
1 love Tavalpoint
to demon Strat e mehod overTidinq
. Proqrarn
1as$ Aninal
det speat (se1f):
Prit (hpcaeing
Clnss Deq[Arima ):
dle Speak (3e1f):
Print ("Bor kinq)
o.spea
lp Bar Fing
e opercer cver lo adi nq
Proeram to demonstred
C3PrD+ (ta+92)
bpen options