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

TopAnswer

stoPy
thonI
nter
viewQuest
ions

1.Compar
ebet
weenJav
aandPy
thon.

Cr
it
eri
a Jav
a Py
thon

Easeofuse Good Ver


yGood

Speedofcodi
ng Av
erage Excel
l
ent

Dat
aty
pes St
ati
cty
ped Dy
nami
cal
l
yty
ped

Dat
aSci
ence&machi
nel
ear
ningappl
i
cat
ions Av
erage Ver
yGood

2.Whati
sPy
thon?

Pyt
honisahigh-
lev
el,int
erpr
eted,i
nter
acti
ve,andobj
ect-
ori
entedscri
pti
nglanguage.Pyt
hon
i
sdesignedt
obehighlyreadable.I
tusesEngli
shkeywordsfr
equently
,whereasother
l
anguagesusepunctuati
on,andithasfewersynt
acti
cal
construct
ionsthanotherl
anguages.

3.Whatar
ethekeyf
eat
uresofPy
thon?

Pyt
honisanint
erpr
etedl
anguage,
soi
tdoesn’
tneedt
obecompi
l
edbef
oreexecut
ionunl
i
ke
l
anguagesl
ikeC.

Py
thoni
sdynamical
l
ytyped,sother
eisnoneedtodecl
areavar
iabl
ewit
ht hedatatype.
Py
thonI
nter
pret
erwil
li
denti
fythedatat
ypeonthebasi
softheval
ueofthevari
able.

Forexampl
e,i
fyour
unt
hef
oll
owi
ngcodel
i
ne,
itwi
l
lrunwi
thoutanyer
ror
:

a=100

a="
Int
ell
i
paat
"

Pythonfoll
owsobject
-or
ient
edprogrammingpar
adi
gm wi
thanexcept
ionofhav
ingaccess
specif
ier
s.Othert
hanaccessspecif
ier
s(publ
i
candpri
vat
ekeywords)
,Pyt
honhasclasses,
i
nherit
ance,andal
l ot
herusual
OOPsconcepts.

Py
t honi
sacross-
platf
ormlanguagewhichmeanst
hataPyt
honpr
ogr
am writ
tenona
Windowssy
stem will
alsor
unonaLi nuxsyst
em wi
thl
it
tl
eornomodi
fi
cat
ionsatal
l.

Pythonisli
ter
all
yageneral
-pur
poselanguagewhi
chmeansthatPyt
honfi
ndsi
tswayin
vari
ousdomai nssuchasWebApplicati
onDevel
opment
,Aut
omat i
on,Dat
aSci
ence,
Machi
ne
Learni
ng,andmor e.

CTA

Checkoutt
hisv
ideoonPy
thonTut
ori
alf
orBegi
nner
s:

<i
framesr c='
//www. youtube.com/embed/pJ3IPRqiD2M?r
el=0'
wi dt
h='100%'
fr
amebor der='
0'all
owf ul
l
screen='al
lowf
ull
screen'encr
ypt
ed-media;gyr
oscope;pi
ctur
e-i
n-
pi
cture'
allowful
lscreen></if
rame>

Lear
nforf
ree!Subscr
ibet
ooury
out
ubeChannel
.

4.Whati
sthepur
poseofPYTHONPATHenv
ironmentv
ari
abl
e?
PYTHONPATHhasar olesi
milartoPATH.Thi svari
abletel
l
sPy thonI
nter
pret
erwher
eto
l
ocatethemodulefil
esi mport
edint oaprogram.Itshoul
dincludePyt
honsourcel
ibr
ary
di
rect
oryandthedirectori
escontainingPyt
honsour cecode.PYTHONPATHi ssometi
mes
pr
esetbyPythonInstall
er.

5.WhatisthepurposeofPYTHONSTARTUP,
PYTHONCASEOK,
andPYTHONHOME
envi
ronmentvari
ables?

PYTHONSTARTUP:Itcont
ainsthepathofaninit
ial
izati
onfi
l
econtai
ningPythonsour
cecode.
Iti
sexecut
edev
eryti
mey oustartthei
nter
pret
er.Itisnamedas.pyt
honrc.
pyinUnix,
andit
contai
nscommandsthatl
oadut i
li
ti
esormodifyPYTHONPATH.

PYTHONCASEOK:I
tisusedi
nWi ndowstoinstr
uctPy
thont
ofi
ndt
hef i
rstcase-
insensi
ti
ve
matchi
nanimpor
tstat
ement.Setthi
svari
ablewit
hanyval
uet
oact
ivateit
.

PYTHONHOME:Iti
sanalt
ernat
ivemodul
esearchpat
h.I
tisusuall
yembeddedi
n
PYTHONSTARTUPorPYTHONPATHdi r
ector
iest
omakeswitchingofmodul
eli
brar
ieseasy
.

Got
hrought
hePy
thonCour
sei
nLondont
ogetcl
earunder
standi
ngofPy
thon.

6.Whatar
ethesuppor
teddat
aty
pesi
nPy
thon?

Py
thonhasf
ivest
andar
ddat
aty
pes:

Number
s

St
ri
ngs

Li
sts

Tupl
es

Di
cti
onar
ies

7.Whati
sthedi
ff
erencebet
weenl
i
stsandt
upl
es?

Li
sts Tupl
es

Li
stsaremut
abl
e,i
.e.
,theycanbe Tupl
esareimmut
abl
e(Tupl
esar
eli
stswhi
chcannot
edi
ted. beedi
ted)
.

Li
stsar
eusual
l
ysl
owert
hant
upl
es. Tupl
esar
efast
ert
hanl
i
sts.

Sy
ntax:
li
st_
1=[
10,
‘I
ntel
l
ipaat
’,
20] Sy
ntax:
tup_
1=(
10,
‘I
ntel
l
ipaat
’,20)

8.Howi
smemor
ymanagedi
nPy
thon?

Memor yinPythoni smanagedbyPythonpr


ivat
eheapspace.AllPyt
honobject
sanddat
a
str
ucturesarel ocatedi
napr i
vat
eheap.Thi
sprivat
eheapistakencareofbyPython
Int
erpreteri
tself,andaprogr
ammerdoesn’thaveanyaccesstothi
spriv
ateheap.

Py
thonmemor
ymanagert
akescar
eoft
heal
l
ocat
ionofPy
thonpr
ivat
eheapspace.

Memoryf
orPyt
honpriv
ateheapspacei
smadeav ai
l
abl
ebyPyt
hon’
sinbui
l
tgar
bage
col
l
ect
orwhi
chrecy
clesandfreesupal
ltheunusedmemory
.

9.Expl
ainI
nher
it
ancei
nPy
thonwi
thanexampl
e.
AsPy thonf ol
lowsanobj ect-ori
entedprogrammi ngparadigm, cl
assesinPyt
honhav ethe
abil
it
yofi nherit
ingtheproper t
iesofanotherclass.Thisprocessisknownasinheri
tance.
Inher
it
ancepr ov idesthecoder eusabi
li
tyfeature.Theclassthatisbei
nginheri
tedi
scal l
eda
super-
cl assandt heclassthatinheri
tstheotherclassiscalledaderiv
edorchildcl
ass.
Foll
owi ngtypesofi nheri
tancearesuppor t
edi nPython:

Si
ngl
einher
it
ance:
Whenacl
assi
nher
it
sonl
yonesupercl
ass

Mul
ti
plei
nher
it
ance:
Whenacl
assi
nher
it
smul
ti
plesupercl
asses

Mult
il
eveli
nherit
ance:Whenacl
assinher
itsasuperclassandthenanothercl
assi
nher
it
s
t
hisderi
vedclassformi
nga‘par
ent
,chil
d,andgrandchil
d’cl
assstr
uctur
e

Hi
erar
chi
cal
inher
it
ance:
Whenonesupercl
assi
sinher
it
edbymul
ti
pleder
ivedcl
asses

Wantt
obecomemast
eri
nPy
thonpr
ogr
ammi
ngcheckoutt
hisPy
thont
rai
ning?

10.Whati
sadi
cti
onar
yinPy
thon?

Pyt
hondict
ionar
yisoneoft
hesuppor
teddatat
ypesinPyt
hon.I
tisanunor
deredcoll
ect
ion
ofel
ements.Theel
ementsi
ndi
cti
onari
esarest
oredaskey–val
uepai
rs.Di
cti
onar
iesare
i
ndexedbykeys.

Forexample,
wehav eadi
cti
onarynamed‘
dict
’.I
tcontai
nssomekey
s:Count
ry,
Capi
tal
along
wit
htheircor
respondi
ngv
alues:
Indi
aandNewDel hi
.

di
ct={
‘Count
ry’
:
’I
ndi
a’
,’
Capi
tal

:’
NewDel
hi’
,}

CTA

Checkoutt
hisv
ideoonPy
thonTut
ori
al

<i
framesrc='
//www.yout
ube.com/embed/XwcJ9_hi
j
dk?rel
=0'
width='
100%'fr
amebor
der
='0'
al
lowful
l
screen='
al
lowful
l
screen'encr
ypt
ed-medi
a;gyr
oscope;
pict
ure-
in-
pict
ure'
al
lowful
l
screen></
ifr
ame>

1.Cany
ouwr
it
eanef
fi
cientcodet
ocountt
henumberofcapi
tal
let
ter
sinaf
il
e?

Thenor
mal
sol
uti
onf
ort
hispr
obl
em st
atementwoul
dbeasf
oll
ows:

wi
thopen(
SOME_
LARGE_
FILE)ascount
let
ter
:

count=0

t
ext=count
let
ter
.r
ead(
)

f
orchar
act
eri
ntext
:

i
fchar
act
er.
isupper
():

count+=1

Tomakethiscodemoreeffi
cient
,thewhol
ecodeblockcanbeconvert
edi
ntoaone-l
i
ner
codeusi
nggenerat
orexpr
ession.Then,
theequi
val
entcodel
inefort
heabovecodebl
ock
wouldbe:

countsum(
1forl
i
nei
ncount
let
terf
orchar
act
eri
nli
nei
fchar
act
er.
isupper
())
12.Wr
it
eacodet
osor
tanumer
ical
li
sti
nPy
thon.

Thef
oll
owi
ngcodecanbeusedt
osor
tanumer
ical
li
sti
nPy
thon:

l
i
st=[
“2”
,“5”
,“7”
,“8”
,“1”
]

l
i
st=[
int
(i
)fori
inl
i
st]

l
i
st.
sor
t()

pr
int(
li
st)

13.Howwi
l
lyour
ever
seal
i
sti
nPy
thon?

l
i
st.
rev
erse(
):Thi
sfunct
ionr
ever
sesobj
ect
sofl
i
st.

CTA

<i
mgcl ass="al
igncent
erwp-
image-
198474si
ze-ful
l"
src="htt
ps://i
ntell
i
paat.
com/medi
aFil
es/
2015/09/Python-
2.j
pg"al
t="
"wi
dth="
970"
height="250"/>

14.Howwi
l
lyour
emov
ethel
astobj
ectf
rom al
i
st?

l
i
st.
pop(
obj
=li
st[
-1]
):

Her
e,‘−1’represent
sthelastelementoft
hel
i
st.Thepopf
unct
ionr
emov
esandr
etur
nst
he
l
astobject(orobj)f
rom t
hel i
st.

15.Whatar
enegat
ivei
ndexesandwhyar
etheyused?

Toaccessanelementfr
om or
deredsequences,
wesimplyusetheindexoftheel
ement
,
whi
chi st
henumberoftheposit
ionoft
hatelement
.Theindexusuall
ystar
tsfr
om 0,
meaningthatt
hefi
rstel
ementhasthei
ndex0andt hesecondhas1,andsoon.

Whenweuset heindextoaccesselementsfrom theendofali


st,
it’
scal
l
edrever
seindexi
ng.
I
nr ev
erseindexi
ng,theindexi
ngofelementsstartfr
om thel
astel
ementwit
htheindex
numberbeing‘−1’
.Thesecondl astel
ementhast heindex‘
−2’
,andsoon.Thesei
ndexes
usedinrev
er sei
ndexingarecall
ednegati
veindexes.

16.Whatar
espl
i
t()
,sub(
),andsubn(
)met
hodsi
nPy
thon?
Thesemet
hodsbel
ongt
oPy
thonRegEx‘
re’
modul
eandar
eusedt
omodi
fyst
ri
ngs.

spl
i
t()
:Thi
smet
hodi
susedt
ospl
i
tagi
venst
ri
ngi
ntoal
i
st.

sub()
:Thi
smethodisusedtofi
ndasubst
ri
ngwherearegexpat
ter
nmat
chesandt
heni
t
repl
acest
hatmatchedsubst
ri
ngwit
hadiff
erentst
ri
ng.

subn(
):Thi
smethodissi
milart
othesub(
)met
hod,
buti
tal
sor
etur
nst
henewst
ri
ng,
along
wit
hthenumberofrepl
acements.

Lear
nmor
eaboutPy
thoni
nthi
sPy
thont
rai
ningi
nNewYor
ktogetaheadi
nyourcar
eer
!

17.Howar
erangeandxr
angedi
ff
erentf
rom oneanot
her
?

Funct
ionsi
nPy t
hon,r
ange()andxrange()areusedtoiter
ateafi
xednumberoft i
mesi
naf
or
l
oop.Funct
ional
it
y-wi
se,bot
hofthesef uncti
onsarethesame.Diff
erencecomeswhen
tal
ki
ngaboutPythonver
sionsupportforthesefunct
ionsandthei
rretur
nvalues.

Ther
ange(
)Met
hod Thexr
ange(
)Met
hod

I
nPython3,xr
ange()i
snotsupport
ed;
inst
ead
Thexrange(
)funct
ioni
susedi
nPy
thon2t
o
t
herange(
)funct
ionisusedt
oiter
atei
nfor
i
ter
ateinforl
oops.
l
oops.

I
tret
urnsagener
atorobj
ectasitdoesn’
t
I
tret
urnsal
i
st.
r
eall
ygener
ateastat
icli
statt
herunt i
me.

I
ttakesmorememoryasitkeepst
heent
ir
e I
ttakesl
essmemor
yasi
tkeepsonl
yone
l
i
stofiter
ati
ngnumber
sinmemor y
. numberatat
imei
nmemory.

18.Def
inepi
ckl
i
ngandunpi
ckl
i
ngi
nPy
thon.

Pi
ckli
ngist
heprocessofconv
ert
ingPyt
honobj
ect
ssuchasli
sts,
dict
s,etc.i
ntoachar
act
er
st
ream.Thi
sisdoneusingamodulenamedpi
ckl
e,hencet
henamepickli
ng.

Theprocessofretr
iev
ingtheori
ginal
Pythonobj
ectfr
om t
hestor
edst
ri
ngr
epr
esent
ati
on,
whichi
st her
everseofthepickl
i
ngprocess,i
scal
ledunpi
ckl
i
ng.

19.Whati
samapf
unct
ioni
nPy
thon?

Themap( )funct
ioninPythonhastwoparameters,functi
onanditer
able.Themap(
)funct
ion
takesafuncti
onasanar gumentandthenappli
est hatfuncti
ontoallt
heelement
sofan
i
terabl
e,passedtoitasanotherar
gument.I
treturnsanobj ectl
i
stofresult
s.

Forexampl
e:

defcal
cul
ateSq(
n):

r
etur
nn*
n

number
s=(
2,3,
4,5)

r
esul
t=map(cal
cul
ateSq,
number
s)

pr
int
(resul
t)

20.Wr
it
eacodet
ogeti
ndi
cesofNmaxi
mum v
aluesi
naNumPyar
ray
.
Wecangett
hei
ndi
cesofNmaxi
mum v
aluesi
naNumPyar
rayusi
ngt
hebel
owcode:

i
mpor
tnumpyasnp

ar=np.
arr
ay(
[1,
3,2,
4,5,
6])

pr
int
(ar
.ar
gsor
t()
[-
3:]
[:
:-
1])

I
nter
est
edi
nlear
ningPy
thon?Cl
i
ckher
etol
ear
nmor
eint
hisPy
thonTr
aini
ngi
nSy
dney
!

21.Whati
saPy
thonmodul
e?

ModulesareindependentPy t
honscript
swi th.pyextensi
onthatcanbereusedi nother
Pythoncodesorscriptsusingtheimportst
at ement.Amodul ecanconsistoffunct
ions,
cl
asses,andvari
ables,orsomer unnabl
ecode.Modul esnotonlyhel
pinkeepingPyt hon
codesorgani
zedbutal soinmakingcodesl esscompl exandmor eeff
ici
ent.Thesynt axt
o
i
mpor tmodulesinPy t
honcodesi sasfoll
ows:

i
mpor
tmodul
e_name 
 #i
ncl
udet
hiscodel
i
neont
opoft
hescr
ipt

Wat
cht
hisPy
thonPr
oject
sVi
deof
orBegi
nner
s:

<i
framesrc='
//www.yout
ube.com/embed/g6i
Vdi
lJNMQ? r
el=0'width='
100%' f
ramebor
der
='0'
al
lowful
l
screen='
al
lowful
l
screen'encr
ypt
ed-medi
a;gy
roscope;picture-
in-
pict
ure'
al
lowful
l
screen></
ifr
ame>

22.Whatdof
il
e-r
elat
edmodul
esi
nPy
thondo?Cany
ounamesomef
il
e-r
elat
edmodul
esi
n
Python?

Pythoncomeswithsomefil
e-r
elat
edmodulesthathav
efunct
ionstomanipul
atet
extfi
les
andbinaryf
il
esonafil
esystem.Thesemodulescanbeusedtocreat
etextorbi
naryf
il
es,
updatethei
rcont
ent
,copy
,delete,
andmore.

Somef i
le-
rel
atedmodul
esareos,os.
path,andshut
il
.os.Theos.
pathmodulehasfuncti
ons
toaccessthefil
esy
stem,whi
letheshut
il.
osmodulecanbeusedt ocopyordelet
efil
es.

23.Expl
aint
heuseofwi
thst
atementandi
tssy
ntax
.

InPyt
hon,
usi
ngthe‘wi
th’
stat
ement,wecanopenafil
eandcloseitassoonast
hebl
ockof
code,
where‘
wit
h’i
sused,exi
ts,
wit
houthav
ingtousethecl
ose()method.

wi
thopen(
"fi
l
ename"
,"mode"
)asf
il
e_v
ar:

24.Expl
ainal
lfi
l
epr
ocessi
ngmodessuppor
tedi
nPy
thon.

Py
thonhasv
ari
ousf
il
epr
ocessi
ngmodes.

Foropeni
ngf
il
es,
ther
ear
ethr
eemodes:

r
ead-
onl
ymode(
r)

wr
it
e-onl
ymode(
w)

r
ead–wr
it
emode(
rw)

Toopenat
extf
il
eusi
ngt
hesemodes,
wewi
l
lhav
etoappend‘
t’
wit
hthem asf
oll
ows:
r
ead-
onl
ymode(
rt)

wr
it
e-onl
ymode(
wt)

r
ead–wr
it
emode(
rwt
)

Si
mil
arl
y,abi
nar
yfi
l
ecanbeopenedbyappendi
ng‘
b’wi
tht
hem asf
oll
ows:

r
ead-
onl
ymode(
rb)

wr
it
e-onl
ymode(
wb)

r
ead–wr
it
emode(
rwb)

Toappendcontenti
nfi
les,
wecanuset
heappendmode(
a).Agai
n,f
ort
extf
il
es,
themode
woul
dbe‘at’
,andforbi
naryfi
l
esi
twoul
dbe‘ab’
.

BecomeMast
erofPy
thonbygoi
ngt
hrought
hisonl
i
nePy
thoncour
sei
nTor
ont
o.

25.I
sindent
ati
onopt
ional
inPy
thon?

I
ndent
ati
oninPythoniscompul sor
yandispartofi
tssynt
ax.Al
lprogramminglanguages
hav
esomewayofdef i
ningthescopeandextentoft
heblockofcodes;inPyt
hon,iti
s
i
ndent
ati
on.I
ndentati
onprov i
desbett
err
eadabil
it
ytothecode,
whichi spr
obablywhy
Pyt
honhasmadei tcompulsory.

26.Howar
ePy
thonar
ray
sandPy
thonl
i
stsdi
ff
erentf
rom eachot
her
?

I
nt hecaseofPy thon, whenpeoplesayar
ray
s,theyareusuall
ytalki
ngaboutl
ist
s.I
tis
becauselist
saref undament altoPyt
honj
ustasarraysarefundamentalt
omostofthelow-
l
evellanguages.But,thereisindeedamodul
enamedar rayi
nPy t
honwhichisusedor
mentionedveryrarely.Foll
owingaresomeofthediff
erencesbetweenarr
aysandli
sts.

Ar
ray
s Li
sts

Arrayscanonlyst
orehomogeneousdat
a Li
stscanst
orehet
erogenousandar
bit
rar
y
(dataofthesametype)
. dat
a.

Si
nceonlyonet y
peofdat acanbestor
ed,
arr
aysusememor yf
oronlyonetypeof Li
stscanstor
edat
aofmul
ti
pledat
aty
pes
obj
ects.Thus,
most l
y,ar
raysusel
esser andthusr
equir
emorememoryt
hanar
ray
s.
memor ythanl
ist
s.

Lengt
hofanarr
ayi
spre-
fi
xedwhil
ecreat
ing Sincet
helengthofali
sti
snotfixed,
i
t,somor
eelement
scannotbeadded. appendi
ngitemsiniti
spossi
ble.

27.Wr
it
eacodet
odi
spl
ayt
hecont
ent
sofaf
il
einr
ever
se.

f
orl
i
nei
nrev
ersed(
li
st(
open(
fi
lename.
txt
)))
:

pr
int
(l
ine.
rst
ri
p()
)

28.Di
ff
erent
iat
ebet
weenNumPyandSci
Py.

NumPy Sci
Py

NumPyst
andsf
orNumer
ical
Pyt
hon. Sci
Pyst
andsf
orSci
ent
if
icPy
thon.
Iti
susedf oreffi
cientandgeneralnumer
ic
Thismoduleisacollect
ionoft
oolsi
nPython
comput ati
onsonnumer i
caldatasavedi
n
toperf
orm operat
ionssuchasint
egr
ati
on,
arrays.Forexample,sort
ing,i
ndexi
ng,
dif
fer
enti
ati
on,andmor e.
reshaping,andmor e.

Exceptforthegeneralcomputing,therear
e
Ful
l
yfledgedal
gebr
aicfunct
ionsareav
ail
abl
e
somel i
nearalgebrai
cfuncti
onsav ail
ablei
n
i
nSciPyforal
gebr
aiccomputati
ons.
thi
smodul e,buttheyarenotfull
yfledged.

29.Whi
choft
hef
oll
owi
ngi
sani
nval
i
dst
atement
?

a)xy
z=1,
000,
000

b)xyz=100020003000

c)x,
y,
z=1000,
2000,
3000

d)x_
y_z=1,
000,
000

Answer
:b

30.Canwemakemul
ti
-l
inecomment
sinPy
thon?

Pyt
hondoesnothav easpecificsy
ntaxfori
ncludi
ngmulti-l
i
necomment sli
keother
pr
ogramminglanguages,butprogrammerscanuset r
ipl
e-quot
edstr
ings(docst
rings)f
or
makingmult
i-
li
necomment s, aswhendocstr
ingisnotbeingusedasthefir
stst
at ement
i
nsi
deamet hod,i
tgetsignoredbyPy t
honparser.

CTA

<i
mgcl ass="al
igncent
erwp-
image-
185862si
ze-ful
l
"
src="htt
ps://i
ntell
i
paat.
com/medi
aFil
es/
2019/02/Gener
ic_
Banner
.j
pg"al
t="
"wi
dth="
970"
height="250"/>

31.Whatwoul
dbet
heout
puti
fIr
unt
hef
oll
owi
ngcodebl
ock?

l
ist
1=[ 2,33,222,
14,
25]
pri
nt(
li
st1[-
2])

(
A) 
  
  
  
  14
(
B) 
  
  
  
  33
(
C) 
  
  
  
  25
(
D) 
  
  
  
  Er
ror

Ans:
14

32.Wr
it
eacommandt
oopent
hef
il
ec:
\hel
l
o.t
xtf
orwr
it
ing.

f
=open(
“hel
l
o.t
xt”
,“wt
”)

33.Whati
s__
ini
t__i
nPy
thon?

__i
nit
__isar eservedmethodinPythonclasseswhi
chi sequival
enttoconstr
uct orsinOOP
ter
minology.The_ _i
nit
__methodiscalledautomat
icall
ywhenev eranewobj ectisini
ti
ated.
The__ini
t__met hodall
ocatesmemor ytothenewobj ectassoonasi ti
screated.This
methodcanal sobeusedt oini
ti
ali
zev ari
abl
es.
Vi
deo

Checkoutt
hisv
ideoonPy
thonf
orDat
aSci
ence

<i
framesr c='
//www. youtube.com/embed/mNdbcHECGN4?rel
=0'widt
h='
100%'
fr
amebor der='
0'all
owf ul
l
screen='al
lowf
ull
scr
een'
encry
pted-
media;gyr
oscope;
pict
ure-
in-
pi
cture'
allowful
lscreen></if
rame>

Lear
nforf
ree!Subscr
ibet
ooury
out
ubeChannel
.

34.Whatdoy
ouunder
standbyTki
nter
?

Tkinteri
saninbui
ltPythonmodulethatisusedtocreateGUIappl
i
cat
ions.I
t’
sPy
thon’
s
standardtool
ki
tforGUIdevelopment.Tki
ntercomeswi t
hPython,
sotherei
snoi
nstal
l
ati
on
needed.Wecanst artusi
ngitbyimporti
ngitinourscri
pt.

35.I
sPy
thonf
ull
yobj
ector
ient
ed?

Pythondoesfollowanobj ect
-orientedprogrammingparadi
gm andhasallt
hebasicOOPs
conceptssuchasi nheri
tance, polymorphism,andmor e,
witht
heexcepti
onofaccess
specif
ier
s.Pythondoesn’ tsuppor tstr
ongencapsulati
on(addi
ngpri
vatekeywordbeforedata
member s)al
thoughi tdoeshav eaconv enti
onthatcanbeusedfordat
ahiding,
i.e.
,pref
ixi
ng
datamemberwi thtwounder scor es.

36.Whati
slambdaf
unct
ioni
nPy
thon?

Lambdafuncti
onisananony mousfuncti
on( f
unct
ionst
hatdon’thav
enames)inPython.To
defi
neanonymousf unct
ions,weusethelambdakeywordinst
eadofthedefkey
word,hence
thename‘l
ambdaf uncti
on’.Lambdafuncti
onscanhaveanynumberofargumentsbutcan
haveonl
yonest at
ement.

37.Whati
ssel
f-
key
wor
dinPy
thon?

Self-
keywordi susedast hefi
rstparamet erofaf uncti
oninsideaclassthatrepresentsthe
i
nstanceoft hecl ass.Theobjectort heinstanceoft heclassisautomati
call
ypassedt ot he
met hodthatitbelongst oandi sreceivedinthe‘self
-keyword’.User
scanuseanot hername
forthefi
rstpar ameteroft hefuncti
ont hatcatchestheobj ectofthecl
ass,butitis
recommendedt ouset heself
-keywor donlyasitismor eofaPy t
honconvention.

38.Whatar
econt
rol
flowst
atement
sinPy
thon?

Contr
olfl
owst atementsareusedt omanipulateorchangetheexecutionflowofaprogr
am.
General
l
y,theflowofexecutionofaprogram runsfrom toptobott
om, butcert
ain
st
atementsinPy thoncanbreakthist
opt obot t
om orderofexecut
ion.Cont r
olfl
ow
st
atementsincludedecisi
on-making,
looping,andmor e.

39.Whati
sthedi
ff
erencebet
weenappend(
)andext
end(
)met
hods?

Bothappend()andext
end(
)methodsar
emet
hodsusedf
orl
i
sts.Thesemet
hodsar
eusedt
o
addelementsattheendofal
i
st.

append(
element
):Addst
hegi
venel
ementatt
heendoft
hel
i
stwhi
chcal
l
edt
hismet
hod

ext
end(
another
-l
ist
):Addst
heel
ement
sofanot
her
-l
istatt
heendoft
hel
i
stwhi
chcal
l
edt
he
ext
endmethod
40.Whatar
eloopi
nter
rupt
ionst
atement
sinPy
thon?

Therearet
woty
pesofloopi
nterrupt
ionst
atement
sinPythonthatl
etuser
ster
minat
eal
oop
i
terat
ionpr
emat
urel
y,i
.e.
,wi
thoutlet
ti
ngtheloopr
unit
sfulli
ter
ati
ons.

Fol
l
owi
ngar
ethet
wol
oopi
nter
rupt
ionst
atement
s:

Pythonbr eakst
atement
:Thi
sst
atementimmediatel
yterminat
estheloopent
ir
ely
,andt
he
control
flowoftheprogr
am i
sshi
fteddi
rect
lyt
ot heoutsi
deoftheloop.

Pyt
honconti
nuestatement:Conti
nuestatementter
minatest
hecurr
entl
oopiterat
ionand
movesthecontr
olfl
owoft heprogram t
ot henexti
ter
ati
onofthel
oop,l
ett
ingtheuserski
p
onl
ythecurr
entit
erati
on.

41.Whati
sdocst
ri
ngi
nPy
thon?

Pythonl
etstheuseri ncludeadescr i
pti
onorqui cknotesfortheirmethodsusing
documentati
onst ri
ngsordocst r
ings.Docstr
ingsar edif
ferentfrom r
egularcommentsin
Pythonasratherthanbei ngcompl et
elyi
gnoredbyPy thonI nt
erpret
erli
keinthecaseof
commentsPy thonst r
ingscanact uall
ybeaccessedatt her untimeusingthedotoperat
or
whendocstri
ngi sthef i
rstst
atementi namet hodorfunction.

Ar
eyoui
nter
est
edi
nlear
ningPy
thoncour
sei
nBangal
oref
rom Exper
ts?

42.Whati
stheout
putoft
hef
oll
owi
ng?

x=[

ab’
,‘
cd’
]

pr
int
(l
en(
li
st(
map(
li
st,
x))
))

Out
put
:[[

a’,
‘b’
]
,[‘
c’
,‘
d’
]]
.

Expl
anat
ion:
Eachel
ementofxi
sconv
ert
edi
ntol
i
st

43.Whi
choneoft
hef
oll
owi
ngi
snott
hecor
rectsy
ntaxf
orcr
eat
ingaset
?

(a)set
([
[1,
2],
[
3,4]
,
[4,
5]])
(b)set
([
1,2,
2,
3,
4,
5])
(c){
1,2,
3,
4}(
d)set
((1,
2,
3,
4))
Answer
:(a)
Expl
anat
ion:
Theargumentgivenforthesetmustbeanit
erabl
e.

44.Whati
sfunct
ional
programming?DoesPythonf
ollowafuncti
onal
progr
ammingst
yle?I
f
yes,
li
staf
ewmet hodstoimplementf
uncti
onall
yor
ientedpr
ogramminginPyt
hon.

Funct
ionalpr
ogrammi ngi
sacodi
ngst
ylewher
ethemai
nsour
ceofl
ogi
cinapr
ogr
am
comesf r
om funct
ions.

I
ncor
por
ati
ngf
unct
ional
progr
ammi
ngi
nourcodesmeanswr
it
ingpur
efunct
ions.

Purefuncti
onsarefuncti
onst
hatcauseli
tt
leornochangesoutsidethescopeofthe
funct
ion.Thesechangesarer
efer
redtoassideeff
ects.Toreducesideeff
ect
s,pure
funct
ionsareused,whichmakesthecodeeasytofol
l
ow, test
,ordebug.

Pythondoesfol
l
owafunct
ionalpr
ogr
ammi
ngst
yle.Fol
l
owi
ngar
esomeoft
heexampl
esof
functi
onal
progr
ammi
nginPy t
hon.

f
il
ter
():
Fil
terl
etsusf
il
tersomev
aluesbasedonacondi
ti
onal
logi
c.
 

>>>l
i
st(
fi
lt
er(
lambdax:
x>6,
range(
9))
)[7,
8]

map(
):Mapappl
i
esaf
unct
iont
oev
eryel
ementi
nani
ter
abl
e.

>>>l
i
st(
map(
lambdax:
x**
2,r
ange(
5))
)[0,
1,4,
9,16,
25]

r
educe(
):Reducer
epeat
edl
yreducesasequencepai
r-
wiseunt
ili
treachesasi
ngl
eval
ue.

>>>f
rom f
unct
ool
simpor
treduce>>>r
educe(
lambdax,
y:
x-y
,[
1,2,
3,
4,
5])-
13

45.HowdoesPy
thonFl
askhandl
edat
abaser
equest
s?

Flasksupportsadatabase-
poweredappl
i
cat
ion(RDBS).Suchasystem r
equi
rescr
eat
inga
schema, whichneedspipi
ngtheschema.
sqlf
il
eint
oasql i
te3command.So, y
ouneedto
i
nst al
lthesqli
te3commandi nor
dertocr
eat
eorinit
iat
ethedatabaseinFl
ask.

Fl
askal
l
owst
orequestf
ordat
abasei
nthr
eeway
s:

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

af
ter
_request
():
Theyar
ecal
l
edaf
terar
equestandpasst
her
esponset
hatwi
l
lbesentt
othe
cl
i
ent.

teardown_request
():
Theyar ecall
edinasit
uati
onwhenanexcept i
onisrai
sed,and
responsesarenotguar ant
eed.Theyarecall
edaftertheresponsehasbeenconstr
uct
ed.
Theyar enotall
owedt omodi f
ytherequest
,andtheirval
uesareignor
ed.

Getcer
ti
fi
edf
rom t
opPy
thoncour
sei
nSi
ngapor
e.Now

46.Wr
it
eaPy t
honprogram t
ocheckwhetheragi
v enstri
ngisapal
i
ndromeornot,
wit
hout
usi
nganit
erat
ivemethod.Not
e:Apal
indr
omei sawor d,phr
ase,
orsequencet
hatr
eadsthe
samebackwar
dasfor war
d,e.
g.,
madam,nursesrun,etc.

deff
un(
str
ing)
:

s1=st
ri
ng

s=st
ri
ng[
::
-1]

i
f(
s1==s)
:

r
etur
ntr
ue

el
se:

r
etur
nfal
se

pr
int
(fun(
“madam”
))
47.Wr
it
eaPy
thonpr
ogr
am t
ocal
cul
atet
hesum ofal
i
stofnumber
s.

defsum(
num)
:

i
flen(
num)==1:

retur
nnum[
0] 
  
  
  
  
  
  
 #wi
thonl
yoneel
ementi
nthel
i
st,
sum r
esul
twi
l
lbeequal
tot
he
element
.

el
se:

r
etur
nnum[
0]+sum(
num[
1:]
)

pr
int
(sum(
[2,
4,5,
6,7]
))

Sampl
eOut
put
:

24

48.Doweneedt
odecl
arev
ari
abl
eswi
thdat
aty
pesi
nPy
thon?

No.Pythoni
sady namical
l
ytypedlanguage,whichmeansthatPyt
honInt
erpr
eter
aut
omat i
cal
l
yidenti
fi
esthedataty
peofav ar
iablebasedonthet
ypeofval
ueassignedt
othe
var
iabl
e.

Lear
nCompl
etePy
thont
rai
ningatHy
der
abad 
i
n24Hr
s.

49.Howwi
l
lyour
eadar
andom l
i
nei
naf
il
e?

Wecanr
eadar
andom l
i
nei
naf
il
eusi
ngamodul
enamed‘
random’
.

Forexampl
e:

i
mpor
trandom

defr
ead_
random(
fname)
:

l
i
nes=open(
fname)
.r
ead(
).
spl
i
tli
nes(
)

r
etur
nrandom.
choi
ce(
li
nes)

pr
int
(read_
random (
‘hel
l
o.t
xt’
))

50.Wr
it
eaPy
thonpr
ogr
am t
ocountt
het
otal
numberofl
i
nesi
nat
extf
il
e.

deff
il
e_count
(fname)
:

wi
thopen(
fname)asf
:

f
ori
,1i
nenumer
ate(
f):

paas

r
etur
ni+1

pr
int
(“Tot
alnumberofl
i
nesi
nthet
extf
il
e:”
,fi
l
e_count
(“f
il
e.t
xt”
))

<i
mgsrc="
https:/
/cdn.i
ntel
l
ipaat
.com/wp-
cont
ent/uploads/
2015/11/
Pyt
hon-
Pr
ogr
ammi ng.png"al
t="Pyt
honPr ogr
ammingOpportunit
ystat
s"/
>

You might also like