Python Interview Questions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

1)Whati

sPy
thon?Whatar
ethebenef
it
sofusi
ngPy
thon?

Pyt
honisaprogr
ammi nglanguagewithobj
ects,modules,
thr
eads,excepti
onsand
aut
omaticmemorymanagement .Thebenefi
tsofpythonsarethati
tissimpleandeasy
,
por
tabl
e,ext
ensi
ble,
buil
d-i
ndatastruct
ureanditi
sanopensour ce.

2)Whati
sPEP8?

PEP8isacodi
ngconvent
ion,
asetofr
ecommendat
ion,
abouthowt
owr
it
eyourPy
thon
codemor
ereadabl
e.

3)Whati
spi
ckl
i
ngandunpi
ckl
i
ng?

Pickl
emodul eacceptsanyPy
thonobjectandconv er
tsiti
ntoastri
ngrepresent
ationand
dumpsi ti
ntoafil
ebyusingdumpf unct
ion,thi
sprocessiscall
edpickl
ing.Whil
etheprocess
ofret
rievi
ngori
ginalPyt
honobject
sfrom thestor
edstri
ngr epr
esentat
ioniscal
ledunpickl
i
ng.

4)HowPy
thoni
sint
erpr
eted?

Pythonlanguageisaninterpr
etedlanguage.Pyt
honpr ogr
am runsdi
rect
lyf
rom t
hesource
code.Itconvert
sthesourcecodet hati
swr i
tt
enbyt heprogr
ammerintoanint
ermediat
e
l
anguage, whichisagai
ntranslat
edintomachinelanguagethathast
obeexecuted.

5)Howmemor
yismanagedi
nPy
thon?

Pyt
honmemor yismanagedbyPythonpri
vat
eheapspace.AllPy
thonobj
ect
sanddata
st
ruct
uresar
elocat
edinapri
vateheap.Theprogrammerdoesnothav
eanaccesst
othis
pr
ivat
eheapandint
erpr
etert
akescareofthi
sPy t
honpri
vat
eheap.

Theal
locat
ionofPyt
honheapspaceforPyt
honobject
sisdonebyPyt
honmemor
ymanager
.
ThecoreAPIgi
vesaccesst
osomet ool
sforthepr
ogrammertocode.

Pythonal
sohav
eaninbui
l
tgar
bagecol
l
ect
or,whichr
ecycl
eall
theunusedmemor
yand
fr
eesthememoryandmakesi
tavai
l
abl
etotheheapspace.

6)Whatar
ethet
ool
sthathel
ptof
indbugsorper
for
m st
ati
canal
ysi
s?

PyCheckeri
sast at
icanal
ysi
stoolthatdet
ect
st hebugsinPythonsourcecodeandwarns
aboutthest
yleandcomplexit
yofthebug.Pyl
intisanot
hertoolthatv
erif
ieswhet
hert
he
modulemeetsthecodingstandar
d.

7)Whatar
ePy
thondecor
ator
s?

APythondecor
atori
saspeci
fi
cchanget
hatwemakei
nPy
thonsy
ntaxt
oal
terf
unct
ions
easi
l
y.

8)Whati
sthedi
ff
erencebet
weenl
i
standt
upl
e?

Thedif
ferencebet
weenl
istandtupl
eisthatl
i
sti
smut
abl
ewhi
l
etupl
eisnot
.Tupl
ecanbe
hashedfore.gasakeyf
ordict
ionar
ies.

9)Howar
ear
gument
spassedbyv
alueorbyr
efer
ence?

Everyt
hinginPythonisanobjectandal
lvari
ablesholdrefer
encestotheobject
s.The
ref
erencesvaluesareaccordi
ngtothefunct
ions;asar esul
tyoucannotchangetheval
ueof
theref
erences.Howev er
,youcanchangetheobjectsifiti
smutable.
10)Whati
sDi
ctandLi
stcompr
ehensi
onsar
e?

Theyaresy
ntaxconst
ruct
ionst
oeaset
hecr
eat
ionofaDi
cti
onar
yorLi
stbasedonexi
sti
ng
i
terabl
e.

11)Whatar
ethebui
l
t-i
nty
pedoespy
thonpr
ovi
des?

Ther
ear
emut
abl
eandI
mmut
abl
ety
pesofPy
thonsbui
l
tint
ypesMut
abl
ebui
l
t-i
nty
pes

Li
st

Set
s

Di
cti
onar
ies

I
mmut
abl
ebui
l
t-i
nty
pes

St
ri
ngs

Tupl
es

Number
s

12)Whati
snamespacei
nPy
thon?

InPyt
hon,ever
ynamei nt
roducedhasaplacewhereitli
vesandcanbehookedf or.Thi
sis
knownasnamespace.Itisl
ikeaboxwhereav ar
iablenamei smappedtot
heobj ectpl
aced.
Wheneverthevar
iabl
eissearchedout
,thi
sboxwillbesearched,
togetcor
respondingobj
ect.

13)Whati
slambdai
nPy
thon?

I
tisasi
ngl
eexpr
essi
onanony
mousf
unct
ionof
tenusedasi
nli
nef
unct
ion.

14)Whyl
ambdaf
ormsi
npy
thondoesnothav
est
atement
s?

Alambdaforminpyt
hondoesnothavest
atement
sasi
tisusedt
omakenewf
unct
ion
obj
ectandthenr
etur
nthem atr
unt
ime.

15)Whati
spassi
nPy
thon?

Passmeans,
no-oper
ationPyt
honst
atement,
ori
notherwor
dsitisapl
acehol
deri
n
compoundst
atement,wheret
her
eshouldbeabl
anklef
tandnothi
nghastobewri
tt
ent
her
e.

16)I
nPy
thonwhatar
eit
erat
ors?

I
nPy
thon,
iter
ator
sar
eusedt
oit
erat
eagr
oupofel
ement
s,cont
ainer
sli
kel
i
st.

17)Whati
suni
tt
esti
nPy
thon?

Aunitt
esti
ngf r
ameworki
nPyt
honisknownasunit
test
.Itsupportsshar
ingofsetups,
aut
omationtesti
ng,
shut
downcodefort
est
s,aggr
egati
onoft est
sintocoll
ect
ionsetc.

18)I
nPy
thonwhati
ssl
i
cing?

Amechani
sm tosel
ectar
angeofi
temsf
rom sequencet
ypesl
i
kel
i
st,
tupl
e,st
ri
ngset
c.i
s
knownassl
ici
ng.

19)Whatar
egener
ator
sinPy
thon?
Thewayofi mplementi
ngit
erat
orsareknownasgener
ator
s.I
tisanor
mal
funct
ionexcept
thati
tyi
eldsexpressi
onint
hefuncti
on.

20)Whati
sdocst
ri
ngi
nPy
thon?

APyt
hondocument
ati
onst
ri
ngisknownasdocst
ri
ng,
iti
sawayofdocument
ingPy
thon
f
unct
ions,
modul
esandcl
asses.

21)Howcany
oucopyanobj
ecti
nPy
thon?

Tocopyanobj ecti
nPython,youcantr
ycopy.copy(
)orcopy
.deepcopy
()f
ort
hegener
al
case.Youcannotcopyallobj
ectsbutmostofthem.

22)Whati
snegat
ivei
ndexi
nPy
thon?

Pythonsequencescanbeindexi
nposit
iveandnegativenumbers.Forposit
iveindex,0i
sthe
fi
rstindex,
1isthesecondi
ndexandsoforth.Fornegati
vei
ndex,(-
1)isthelastindexand(-
2)
i
st hesecondlasti
ndexandsofor
th.

23)Howy
oucanconv
ertanumbert
oast
ri
ng?

I
nor dert
oconver
tanumberint
oastr
ing,
usetheinbui
l
tfunct
ionstr
().I
fyouwantaoct
alor
hexadeci
malrepr
esent
ati
on,
usethei
nbui
ltf
unct
ionoct(
)orhex()
.

24)Whati
sthedi
ff
erencebet
weenXr
angeandr
ange?

Xranger
etur
nsthexr
angeobjectwhi
ler
anger
etur
nst
hel
i
st,
andusest
hesamememor
y
andnomat t
erwhatt
herangesizei
s.

25)Whati
smodul
eandpackagei
nPy
thon?

I
nPython,
modul
eist
hewaytostr
uctur
eprogr
am.EachPyt
honpr
ogr
am f
il
eisamodul
e,
whi
chimport
sot
hermodul
esl
ikeobj
ectsandat
tri
but
es.

ThefolderofPy
thonpr
ogr
am i
sapackageofmodul
es.Apackagecanhav
emodul
esor
subf
olders.

26)Ment
ionwhatar
ether
ulesf
orl
ocal
andgl
obal
var
iabl
esi
nPy
thon?

Localv
ari
abl
es:
Ifavar
iabl
eisassi
gnedanewv
alueany
wher
ewi
thi
nthef
unct
ion'
sbody
,it
'
s
assumedtobel
ocal
.

Gl
obalvar
iabl
es:
Thosev
ari
abl
est
hatar
eonl
yref
erencedi
nsi
deaf
unct
ionar
eimpl
i
cit
ly
gl
obal
.

27)Howcany
oushar
egl
obal
var
iabl
esacr
ossmodul
es?

Toshar
eglobalvar
iabl
esacr
ossmoduleswi
thi
nasingl
eprogram,
createaspeci
almodule.
I
mporttheconf
igmoduleinal
lmodul
esofyourappl
i
cat
ion.Themodulewil
lbeavai
labl
eas
agl
obalvar
iabl
eacrossmodules.

28)Expl
ainhowcany
oumakeaPy
thonScr
iptexecut
abl
eonUni
x?

TomakeaPy
thonScr
iptexecut
abl
eonUni
x,y
ouneedt
odot
wot
hings,

Scr
iptf
il
e'
smodemustbeexecut
abl
eand

t
hef
ir
stl
i
nemustbegi
nwi
th#(#!
/usr
/local
/bi
n/py
thon)
29)Expl
ainhowt
odel
eteaf
il
einPy
thon?

Byusi
ngacommandos.
remov
e(f
il
ename)oros.
unl
i
nk(
fi
lename)

30)Expl
ainhowcany
ougener
ater
andom number
sinPy
thon?

Togener
ater
andom number
sinPy
thon,
youneedt
oimpor
tcommandas

i
mpor
trandom

r
andom.
random(
)

Thi
sret
urnsar
andom f
loat
ingpoi
ntnumberi
nther
ange[
0,1)

31)Expl
ainhowcany
ouaccessamodul
ewr
it
teni
nPy
thonf
rom C?

Youcanaccessamodul
ewr
it
teni
nPy
thonf
rom Cbyf
oll
owi
ngmet
hod,

Modul
e==Py
Impor
t_I
mpor
tModul
e("
<modul
ename>"
);

32)Ment
iont
heuseof/
/oper
atori
nPy
thon?

I
tisaFloorDiv
isi
onoper
ator,whi
chi
susedfordivi
dingtwooper
andswitht
her
esul
tas
quoti
entshowi
ngonlydi
gitsbef
oret
hedeci
mal poi
nt.Fori
nst
ance,10/
/5=2and10.
0//5.
0
=2.0.

33)Ment
ionf
ivebenef
it
sofusi
ngPy
thon?

Pyt
honcompr
isesofahugest
andar
dli
brar
yformostI
nter
netpl
atf
ormsl
i
keEmai
l
,HTML,
et
c.

Py
thondoesnotr
equir
eexpl
ici
tmemorymanagementast
hei
nter
pret
eri
tsel
fal
l
ocat
est
he
memorytonewvari
abl
esandfreet
hem aut
omati
cal
l
y

Pr
ovi
deeasyr
eadabi
l
ityduet
ouseofsquar
ebr
acket
s

Easy
-to-
lear
nforbegi
nner
s

Hav
ingt
hebui
l
t-i
ndat
aty
pessav
espr
ogr
ammi
ngt
imeandef
for
tfr
om decl
ari
ngv
ari
abl
es

34)Ment
iont
heuseoft
hespl
i
tfunct
ioni
nPy
thon?

Theuseofthespl
itfuncti
oni
nPythonist
hati
tbr
eaksastr
ingint
oshor
terst
ri
ngsusi
ngt
he
def
inedsepar
ator
.Itgivesal
i
stofall
wordspr
esenti
nthestr
ing.

35)Expl
ainwhati
sFl
ask&i
tsbenef
it
s?

Fl
askisawebmi cr
oframeworkforPyt
honbasedon"Wer
kzeug,Ji
nja2andgood
i
ntent
ions"BSDl
i
censed.Werkzeugandji
ngj
aaretwoofi
tsdependenci
es.

Flaskispartofthemicro-
fr
amework.Whichmeansitwil
lhav
elit
tl
etonodependenci
eson
external
li
brari
es.Itmakesthef
rameworkli
ghtwhi
lether
eisli
tt
ledependencyt
oupdateand
l
esssecur i
tybugs.

36)Ment
ionwhati
sthedi
ff
erencebet
weenDj
ango,
Pyr
ami
d,andFl
ask?

Fl
askisa"micr
ofr
amework"pri
maril
ybuil
dforasmallappl
i
cati
onwi
thsi
mpl
err
equi
rement
s.
I
nflask,
youhavetouseext
ernall
i
brari
es.Fl
aski
sreadytouse.
Pyramidarebuil
dforlargerappli
cat
ions.Itpr
ovidesf
lexi
bil
i
tyandletst
hedevel
operuset
he
ri
ghttoolsf
orthei
rproject.Thedevelopercanchoosethedatabase,
URLstr
uctur
e,
templat
ingsty
leandmor e.Pyr
amidisheav yconfi
gur
able.

Li
kePy
rami
d,Dj
angocanal
sousedf
orl
argerappl
i
cat
ions.I
tincl
udesanORM.

37)Ment
ionwhati
sFl
ask-
WTFandwhatar
ethei
rfeat
ures?

Fl
ask-
WTFof
fer
ssi
mpl
eint
egr
ati
onwi
thWTFor
ms.Feat
uresi
ncl
udef
orFl
askWTFar
e

I
ntegr
ati
onwi
thwt
for
ms

Secur
efor
m wi
thcsr
ftoken

Gl
obal
csr
fpr
otect
ion

I
nter
nat
ional
i
zat
ioni
ntegr
ati
on

Recapt
chasuppor
ti
ng

Fi
l
eupl
oadt
hatwor
kswi
thFl
askUpl
oads

38)Expl
ainwhati
sthecommonwayf
ort
heFl
askscr
iptt
owor
k?

Thecommonwayf
ort
hef
laskscr
iptt
owor
kis

Ei
theri
tshoul
dbet
hei
mpor
tpat
hfory
ourappl
i
cat
ion

Ort
hepat
htoaPy
thonf
il
e

39)Expl
ainhowy
oucanaccesssessi
onsi
nFl
ask?

Asessionbasi
callyal
l
owsy outorememberinfor
mat i
onfrom onerequestt
oanot
her.I
na
fl
ask,i
tusesasignedcookiesotheusercanlookatthesessi
oncont ent
sandmodif
y.The
usercanmodif
yt hesessi
onifonl
yithasthesecretkeyFl
ask.
secret_key
.

40)I
sFl
askanMVCmodel
andi
fyesgi
veanexampl
eshowi
ngMVCpat
ter
nfory
our
appl
i
cat
ion?

Basi
cal
l
y,Fl
askisami ni
malisti
cfr
amewor kwhi
chbehav
essameasMVCf ramework.So
MVCisaperfectf
itf
orFlask,andthepat
ternf
orMVCwewillconsi
derf
ort
hefol
lowing
exampl
e

I
nthi
scodey
our
,

f
rom f
laski
mpor
tFl
ask Conf
igur
ati
onpar
twi
l
lbe

app=Fl
ask(
_name_
) f
rom f
laski
mpor
tFl
ask

@app.
rout
e("
/") app=Fl
ask(
_name_
)

Defhel
l
o()
: Vi
ewpar
twi
l
lbe

r
etur
n"Hel
l
oWor
ld" @app.
rout
e("
/")

app.
run(
debug=Tr
ue) Defhel
l
o()
:

r
etur
n"Hel
l
oWor
ld"
Whi
l
eyoumodel
ormai
npar
twi
l
lbe

app.
run(
debug=Tr
ue)

41)Expl
aindat
abaseconnect
ioni
nPy
thonFl
ask?

Flasksupportsdatabasepoweredappl
icat
ion(RDBS).Suchsystem r
equi
rescr
eat
inga
schema, whichrequir
espipi
ngtheshema.sqlfi
l
eintoasqli
te3command.Soy ouneedto
i
nst al
lsql
it
e3commandi nordert
ocreateorini
ti
atethedatabasei
nFlask.

Fl
askal
l
owst
orequestdat
abasei
nthr
eeway
s

bef
ore_
request
():
Theyar
ecal
l
edbef
orear
equestandpassnoar
gument
s

aft
er_
request
():
Theyar
ecal
l
edaf
terar
equestandpasst
her
esponset
hatwi
l
lbesentt
o
thecl
ient

tear
down_r
equest (
):Theyarecall
edinsit
uationwhenexcept
ionisraised,
andresponsear
e
notguar
anteed.Theyar ecall
edaft
ertheresponsebeenconstr
ucted.Theyar
enotallowed
tomodif
yther equest,
andt hei
rval
uesareignored.

42)Youar
ehavingmult
ipl
eMemcacheser
ver
srunni
ngPy t
hon,i
nwhichoneofthe
memcacherser
verf
ail
s,andi
thasy
ourdat
a,wil
li
tevert
rytogetkeydat
afrom t
hatone
fai
l
edserv
er?

Thedatainthefai
ledserv
erwon'tgetremoved,butt
herei
sapr ovi
sionf
orauto-
fai
l
ure,
whichyoucanconf i
gur
eformul t
ipl
enodes.Fai
l-
overcanbetr
iggeredduri
nganyki
ndof
socketorMemcachedser verl
eveler
rorsandnotduri
ngnor
mal cl
ienter
rorsl
i
keaddingan
exi
sti
ngkey,etc.

43)Expl
ainhowy
oucanmi
nimi
zet
heMemcachedser
verout
agesi
nyourPy
thon
Devel
opment?

Whenoneinstancef
ail
s,several
ofthem goesdown,thiswil
lputlargerloadonthedatabase
serv
erwhenlostdat
aisreloadedascli
entmakear equest
.Toav oidthis,i
fyourcodehas
beenwri
tt
entomi ni
mizecachestampedest heni
twilll
eaveami nimalimpact

Anot
herwayistobri
ngupani
nst
anceofMemcachedonanewmachi
neusi
ngt
hel
ost
machi
nesIPaddress

Codei
sanotheropti
ontomini
mizeser
verout
agesasi
tgi
vesy
out
hel
i
ber
tyt
ochanget
he
Memcachedserverl
istwi
thmi
nimalwork

Setti
ngti
meoutv
alueisanotheropt
ionthatsomeMemcachedcl ient
simpl
ementfor
Memcachedserverout
age.Wheny ourMemcachedser vergoesdown,t
hecli
entwi
ll
keep
tr
yingtosendar
equestti
ll
thetime-outl
imiti
sreached

44)Expl
ainwhati
sDogpi
l
eef
fect
?Howcany
oupr
eventt
hisef
fect
?

Dogpil
eeffectisref
err
edtotheeventwhencacheexpir
es,
andwebsitesar
ehitbythe
multi
pler
equest smadebythecli
entatthesametime.Thi
seff
ectcanbepreventedbyusi
ng
semaphorelock.Inthi
ssyst
em whenv al
ueexpi
res,f
ir
stpr
ocessacquir
esthelockand
star
tsgenerati
ngnewv al
ue.

45)Expl
ainhowMemcachedshoul
dnotbeusedi
nyourPy
thonpr
oject
?
Memcachedcommonmi
susei
stousei
tasadat
ast
ore,
andnotasacache

NeveruseMemcachedastheonl
ysour
ceoftheinf
ormat
ionyouneedt
orunyour
appl
icat
ion.Dat
ashoul
dal
waysbeavai
labl
ethr
oughanot
hersour
ceaswell

Memcachedi
sjustakeyorv
aluest
oreandcannotper
for
m quer
yov
ert
hedat
aori
ter
ate
ov
ert
hecontent
stoextr
acti
nfor
mation

Memcacheddoesnotof
feranyf
orm ofsecur
it
yei
theri
nencr
ypt
ionoraut
hent
icat
ion

You might also like