Sie sind auf Seite 1von 4

TopPythonInterviewQuestionsMostAsked

Herearetop30objectivetypesamplePythonInterviewquestionsandtheiranswersaregivenjustbelowto
them.ThesesamplequestionsareframedbyexpertsfromIntellipaatwhotrainsforPythontrainingtogiveyou
anideaoftypeofquestionswhichmaybeaskedininterview.Wehavetakenfullcaretogivecorrectanswers
forallthequestions.DocommentyourthoughtsHappyJobHunting!
TopAnswerstoPythonInterviewQuestions
1.WhatisPython?
Pythonisanobjectorientedandopensourceprogramminglanguage,whichsupportsstructuredandfunctional
builtindatastructures.Withaplacidandeasytounderstandsyntax,Pythonallowscodereuseandmodularity
ofprograms.ThebuiltinDSinPythonmakesitawonderfuloptionforRapidApplicationDevelopment(RAD).
Thecodinglanguagealsoencouragesfasterediting,testinganddebuggingwithnocompilationsteps.
2.WhatarethestandarddatatypessupportedbyPython?
Itsupportssixdatatypes:
1.Number:objectstoredasnumericvalue
2.String:objectstoredasstring
3.Tuple:datastoredintheformofsequenceofimmutableobjects
4.Dictionary(dicts):associatesonethingtoanotherirrespectiveofthetypeofdata,mostusefulcontainer
(calledhashesinCandJava)
5.List:datastoredintheformofalistsequence
6.Set(frozenset):unorderedcollectionofdistinctobjects
3.ExplainbuiltinsequencetypesinPythonProgramming?
Itprovidestwobuiltinsequencetypes
1.MutableType:objectswhosevaluecanbechangedaftercreation,example:sets,itemsinthelist,dictionary
2.Immutabletype:objectswhosevaluecannotbechangedoncecreated,example:number,Boolean,tuple,
string
4.ExplaintheuseofiteratorinPython?
PythoncodingusesIteratortoimplementtheiteratorprotocol,whichenablestraversingtroughcontainersand
groupofelementslikelist.Thetwoimportantmethodsinclude_iter_()returningtheiteratorobjectandnext()
methodfortraversal.
5.DefinePythonslicing?
Theprocessofextractingarangeofelementsfromlists,arrays,tuplesandcustomPythondatastructuresas
well.Itworksonageneralstartandstopmethod:slice(start,stop,increment)
6.HowcanyoucomparetwolistsinPython?
Wecansimplyperformitusingcomparefunctioncmp(intellipaatlist1,intellipaatlist2)
defcmp(intellipaatlist1,intellipaatlist2):
forvalinintellipaatlist1:
ifvalinintellipaatlist2:
returnTrue
returnFalse
7.Whatistheuseof//operator?
//isaFloorDivisionoperator,whichdividestwooperandswiththeresultasquotientshowingonlydigits
beforedecimalpoint.Forinstance,6//3=2and6.0//3.0=2.0
8.DefinedocstringinPythonwithexample.
Astringliteraloccurringasthefirststatement(likeacomment)inanymodule,class,functionormethodis
referredasdocstringinPython.Thiskindofstringbecomesthe_doc_specialattributeoftheobjectand
providesaneasywaytodocumentaparticularcodesegment.Mostmodulesdocontaindocstringsandthus,the
functionsandclassesextractedfromthemodulealsoconsistofdocstrings.
9.Whatfunctionrandomizestheitemsofalistinplace?
Usingshuffle()function
Forinstance:
importrandomize
lst=[2,18,8,4];
randomize.shuffle(lst)
printShuffledlist:,lst
random.shuffle(list)

printReshuffledlist:,list
10.ListfivebenefitsofusingPython?
1.Havingthebuiltindatatypes,Pythonsavesprogrammerstimeandeffortfromdeclaringvariables.Ithasa
powerfuldictionaryandpolymorphiclistforautomaticdeclaration.Italsoensuresbettercodereusability
2.HighlyaccessibleandeasytolearnforbeginnersandastrongglueforadvancedProfessionalsconsistingfo
severalhighlevelmodulesandoperationsnotperformedbyotherprogramminglanguages.
3.Allowseasyreadabilityduetouseofsquarebracketsformostfunctionsandindexes
4.Pythonrequiresnoexplicitmemorymanagementastheinterpreteritselfallocatesthememorytonew
variablesandfreethemautomatically.
5.PythoncomprisesahugestandardlibraryformostInternetplatformslikeEmail,HTML,FTPandother
WWWplatforms.
11.WhatarethedisadvantagesofusingPython?
1.Pythonisslowascomparedtootherprogramminglanguages.Although,thisslowpacedoesntmattermuch,
attimes,weneedotherlanguagetohandleperformancecriticalsituations.
2.Itisineffectiveonmobileplatforms;fewermobileapplicationsaredevelopedusingpython.Themainreason
behinditsinstabilityonsmartphonesisPythonsweakestsecurity.Therearenogoodsecurecasesavailablefor
Pythonuntilnow
3.Duetodynamictyping,Programmersfacedesignrestrictionswhileusingthelanguage.Thecodeneedsmore
andmoretestingbeforeputtingitintoactionsincetheerrorspopuponlyduringruntime.
4.UnlikeJavaScript,Pythonsfeatureslikeconcurrencyandparallelismarenotdevelopedforelegantuse.
12.Explaintheuseofsplitfunction?
Thesplit()functioninPythonbreaksastringintoshorterstringsusingthedefinedseparator.Itrendersalistof
allwordspresentinthestring.
>>>y=true,false,none
>>>y.split(,)
Result:(true,false,none)
WhatistheuseofgeneratorsinPython?
Generatorsareprimarilyusedtoreturnmultipleitemsbutoneaftertheother.Theyareusedforiterationin
Pythonandforcalculatinglargeresultsets.Thegeneratorfunctionhaltsuntilthenexttimerequestisplaced.
OneofthebestusesofgeneratorsinPythoncodingisimplementingcallbackoperationwithreducedeffortand
time.Theyreplacecallbackwithiteration.Throughthegeneratorapproach,programmersaresavedfromwriting
aseparatecallbackfunctionandpassittoworkfunctionasitcanapplyingforlooparoundthegenerator.
13.HowtocreateamultidimensionallistinPython?
Asthenamesuggests,amultidimensionallististheconceptofalistholdinganotherlist,applyingtomanysuch
lists.Itcanbeoneeasilydonebycreatingsingledimensionallistandfillingeachelementwithanewlycreated
list.
14.Whatislambda?
lambdaisapowerfulconceptusedinconjunctionwithotherfunctionslikefilter(),map(),reduce().Themajor
useoflambdaconstructis
tocreateanonymousfunctionsduringruntime,whichcanbeusedwheretheyarecreated.Suchfunctionsare
actuallyknownasthrowawayfunctionsinPython.Thegeneralsyntaxislambdaargument_list:expression.
Forinstance:
>>>defintellipaat1=lambdai,n:i+n
>>>intellipaat(2,2)
4
Usingfilter()
>>intellipaat=[1,6,11,21,29,18,24]
>>printfilter(lambdax:x%3==0,intellipaat)
[6,21,18,24]
15.DefinePassinPython?
ThepassstatementinPythonisequivalenttoanulloperationandaplaceholder,whereinnothingtakesplace
afteritsexecution.Itismostlyusedatplaceswhereyoucanletyourcodegoevenifitisntwrittenyet.
Ifyouwouldsetoutapassafterthecode,itwontrun.Thesyntaxispass
16.HowtoperformUnitTestinginPython?
ReferredtoasPyUnit,thepythonUnittestingframeworkunittestsupportsautomatedtesting,seggregatingtest
intocollections,shutdowntestingcodeandtestingindependencefromreportingframework.Theunittestmodule
makesuseofTestCaseclassforholdingandpreparingtestroutinesandclearingthemafterthesuccessful

execution.
17.DefinePythontoolsforfindingbugsandperformingstaticanalysis?
.PyCheckerisanexcellentbugfindertoolinPython,whichperformsstaticanalysisunlikeC/C++andJava.It
alsonotifiestheprogrammersaboutthecomplexityandstyleofthecode.Inaddition,thereisanothertool,
PyLintforcheckingthecodingstandardsincludingthecodelinelength,variablenamesandwhetherthe
interfacesdeclaredarefullyexecutedornot.
18.Howtoconvertastringintolist?
Usingthefunctionlist(string).Forinstance:
>>>list(intellipaat)inyourlinesofcodewillreturn
[i,n,t,e,l,l,i,p,a,a,t]
InPython,stringsbehavelikelistinvariousways.Like,youcanaccessindividualcharactersofastring
>>>y=intellipaat
>>>s[2]
t
19.WhatOSdoPythonsupport?
Linux,Windows,MacOSX,IRIX,Compaq,Solaris
20.NametheJavaimplementationofPython?
Jython
21.DefinedocstringinPython.
Astringliteraloccurringasthefirststatement(likeacomment)inanymodule,class,functionormethodis
referredasdocstringinPython.Thiskindofstringbecomesthe_doc_specialattributeoftheobjectand
providesaneasywaytodocumentaparticularcodesegment.Mostmodulesdocontaindocstringsandthus,the
functionsandclassesextractedfromthemodulealsoconsistofdocstrings.
22.NametheoptionalclausesusedinatryexceptstatementinPython?
WhilePythonexceptionhandlingisabitdifferentfromJava,theformerprovidesanoptionofusingatryexcept
clausewheretheprogrammerreceivesadetailederrormessagewithoutterminationtheprogram.Sometimes,
alongwiththeproblem,thistryexceptstatementoffersasolutiontodealwiththeerror.
Thelanguagealsoprovidestryexceptfinallyandtryexceptelseblocks.
23.HowtousePYTHOPATH?
PYTHONPATHistheenvironmentvariableconsistingofdirectories.$PYTHONPATHisusedforsearchingthe
actuallistoffoldersforlibraries.
24.DefineselfinPython?
selfisareferencetothecurrentinstanceoftheclass.ItisjustlikethisinJavaScript.Whilewecreatean
instanceofaclass,thatinstancehasitsdata,whichinternallypassesareferencetoitself
25.DefineCGI?
CommonGatewayInterfacesupportinPythonisanexternalgatewaytointeractwithHTTPserverandother
informationservers.Itconsistsofaseriesofstandardsandinstructionsdefiningtheexchangeofinformation
betweenacustomscriptandwebserver.TheHTTPserverputsallimportantandusefulinformationconcerning
therequestinthescriptenvironmentandthenrunthescriptandsendsitbackintheformofoutputtotheclient.
26.WhatisPYTHONSTARTUPandhowisitused?
PYTHONSTARTUPisyetanotherenvironmentvariabletotestthePythonfileintheinterpreterusing
interactivemode.Thescriptfileisexecutedevenbeforethefirstpromptisseen.Additionally,italsoallows
reloadingofthesamescriptfileafterbeingmodifiedintheexternaleditor.
27.Whatisthereturnvalueoftrunc()inPython?
truc()returnsintegervalue.Usesthe_trunc_method
>>>importintellipaat
intellipaat.trunc(4.34)
4
28.HowtoconvertastringtoanobjectinPython?
Toconvertstringintoobject,Pythonprovidesafunctioneval(string).ItallowsthePythoncodetoruninitself
29.Isthereanyfunctiontochangecaseofalllettersinthestring?
Yes,Pythonsupportsafunctionswapcase(),whichswapsthecurrentlettercaseofthestring.Thismethod
returnsacopyofthestringwiththestringcaseswapped.
30.WhatispicklingandunpicklinginPython?
TheprocessofPicklingrelatestothePicklemodule.Pickleisageneralmodulethatacquiresapythonobjectand
convertsitintostring.Itfurtherdumpsthatstringobjectintoafilebyusingdump()function.

Picklecomprisestwomethods:
Dump():dumpsanobjecttoafileobject
andLoad():loadsanobjectfromafileobject
UnpicklingisthereacquiringprocesstoperformretrievaloftheoriginalPythonobjectfromthestoredstringfor
reuse.

Das könnte Ihnen auch gefallen