Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Pythonisahighlevel,interpreted,interactiveandobjectorientedscriptinglanguage.Pythonisdesignedtobehighlyreadable.
ItusesEnglishkeywordsfrequentlywhereasotherlanguagesusepunctuation,andithasfewersyntacticalconstructionsthan
otherlanguages.
NamesomeofthefeaturesofPython.
Followingaresomeofthesalientfeaturesofpython
ItsupportsfunctionalandstructuredprogrammingmethodsaswellasOOP.
Itcanbeusedasascriptinglanguageorcanbecompiledtobytecodeforbuildinglargeapplications.
Itprovidesveryhighleveldynamicdatatypesandsupportsdynamictypechecking.
Itsupportsautomaticgarbagecollection.
ItcanbeeasilyintegratedwithC,C++,COM,ActiveX,CORBA,andJava.
WhatisthepurposeofPYTHONPATHenvironmentvariable?
PYTHONPATHIthasarolesimilartoPATH.ThisvariabletellsthePythoninterpreterwheretolocatethemodulefilesimported
into a program. It should include the Python source library directory and the directories containing Python source code.
PYTHONPATHissometimespresetbythePythoninstaller.
WhatisthepurposeofPYTHONSTARTUPenvironmentvariable?
PYTHONSTARTUPItcontainsthepathofaninitializationfilecontainingPythonsourcecode.Itisexecutedeverytimeyoustart
theinterpreter.Itisnamedas.pythonrc.pyinUnixanditcontainscommandsthatloadutilitiesormodifyPYTHONPATH.
WhatisthepurposeofPYTHONCASEOKenvironmentvariable?
PYTHONCASEOKItisusedinWindowstoinstructPythontofindthefirstcaseinsensitivematchinanimportstatement.Set
thisvariabletoanyvaluetoactivateit.
WhatisthepurposeofPYTHONHOMEenvironmentvariable?
PYTHONHOME It is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH
directoriestomakeswitchingmodulelibrarieseasy.
Ispythonacasesensitivelanguage?
Yes!Pythonisacasesensitiveprogramminglanguage.
WhatarethesupporteddatatypesinPython?
Pythonhasfivestandarddatatypes
Numbers
String
List
Tuple
Dictionary
Whatistheoutputofprintstrifstr='HelloWorld!'?
Itwillprintcompletestring.OutputwouldbeHelloWorld!.
Whatistheoutputofprintstr[0]ifstr='HelloWorld!'?
Itwillprintfirstcharacterofthestring.OutputwouldbeH.
Whatistheoutputofprintstr[2:5]ifstr='HelloWorld!'?
Itwillprintcharactersstartingfrom3rdto5th.Outputwouldbello.
Whatistheoutputofprintstr[2:]ifstr='HelloWorld!'?
Itwillprintcharactersstartingfrom3rdcharacter.OutputwouldbelloWorld!.
Whatistheoutputofprintstr*2ifstr='HelloWorld!'?
Itwillprintstringtwotimes.OutputwouldbeHelloWorld!HelloWorld!.
Whatistheoutputofprintstr+"TEST"ifstr='HelloWorld!'?
Itwillprintconcatenatedstring.OutputwouldbeHelloWorld!TEST.
Whatistheoutputofprintlistiflist=['abcd',786,2.23,'john',70.2]?
Itwillprintconcatenatedlists.Outputwouldbe['abcd',786,2.23,'john',70.2].
Whatistheoutputofprintlist[0]iflist=['abcd',786,2.23,'john',70.2]?
Itwillprintfirstelementofthelist.Outputwouldbeabcd.
Whatistheoutputofprintlist[1:3]iflist=['abcd',786,2.23,'john',70.2]?
Itwillprintelementsstartingfrom2ndtill3rd.Outputwouldbe[786,2.23].
Whatistheoutputofprintlist[2:]iflist=['abcd',786,2.23,'john',70.2]?
Itwillprintelementsstartingfrom3rdelement.Outputwouldbe[2.23,'john',70.200000000000003].
Whatistheoutputofprinttinylist*2iftinylist=[123,'john']?
Itwillprintlisttwotimes.Outputwouldbe[123,'john',123,'john'].
Whatistheoutputofprintlist+tinylist*2iflist=['abcd',786,2.23,'john',70.2]andtinylist=[123,'john']?
Itwillprintconcatenatedlists.Outputwouldbe['abcd',786,2.23,'john',70.2,123,'john',123,'john'].
WhataretuplesinPython?
Atupleisanothersequencedatatypethatissimilartothelist.Atupleconsistsofanumberofvaluesseparatedbycommas.
Unlikelists,however,tuplesareenclosedwithinparentheses.
WhatisthedifferencebetweentuplesandlistsinPython?
ThemaindifferencesbetweenlistsandtuplesareListsareenclosedinbrackets([])andtheirelementsandsizecanbe
changed,whiletuplesareenclosedinparentheses(())andcannotbeupdated.Tuplescanbethoughtofasreadonlylists.
Whatistheoutputofprinttupleiftuple=('abcd',786,2.23,'john',70.2)?
Itwillprintcompletetuple.Outputwouldbe('abcd',786,2.23,'john',70.200000000000003).
Whatistheoutputofprinttuple[0]iftuple=('abcd',786,2.23,'john',70.2)?
Itwillprintfirstelementofthetuple.Outputwouldbeabcd.
Whatistheoutputofprinttuple[1:3]iftuple=('abcd',786,2.23,'john',70.2)?
Itwillprintelementsstartingfrom2ndtill3rd.Outputwouldbe(786,2.23).
Whatistheoutputofprinttuple[2:]iftuple=('abcd',786,2.23,'john',70.2)?
Itwillprintelementsstartingfrom3rdelement.Outputwouldbe(2.23,'john',70.200000000000003).
Whatistheoutputofprinttinytuple*2iftinytuple=(123,'john')?
Itwillprinttupletwotimes.Outputwouldbe(123,'john',123,'john').
Whatistheoutputofprinttuple+tinytupleiftuple=('abcd',786,2.23,'john',70.2)andtinytuple=(123,'john')?
Itwillprintconcatenatedtuples.Outputwouldbe('abcd',786,2.23,'john',70.200000000000003,123,'john').
WhatarePython'sdictionaries?
Python'sdictionariesarekindofhashtabletype.TheyworklikeassociativearraysorhashesfoundinPerlandconsistofkey
valuepairs.AdictionarykeycanbealmostanyPythontype,butareusuallynumbersorstrings.Values,ontheotherhand,can
beanyarbitraryPythonobject.
Howwillyoucreateadictionaryinpython?
Dictionariesareenclosedbycurlybraces({})andvaluescanbeassignedandaccessedusingsquarebraces([]).
dict={}
dict['one']="Thisisone"
dict[2]="Thisistwo"
tinydict={'name':'john','code':6734,'dept':'sales'}
Howwillyougetallthekeysfromthedictionary?
Usingdictionary.keys()function,wecangetallthekeysfromthedictionaryobject.
printdict.keys()#Printsallthekeys
Howwillyougetallthevaluesfromthedictionary?
Usingdictionary.values()function,wecangetallthevaluesfromthedictionaryobject.
printdict.values()#Printsallthevalues
Howwillyouconvertastringtoanintinpython?
int(x[,base])Convertsxtoaninteger.basespecifiesthebaseifxisastring.
Howwillyouconvertastringtoalonginpython?
long(x[,base])Convertsxtoalonginteger.basespecifiesthebaseifxisastring.
Howwillyouconvertastringtoafloatinpython?
float(x)Convertsxtoafloatingpointnumber.
Howwillyouconvertaobjecttoastringinpython?
str(x)Convertsobjectxtoastringrepresentation.
Howwillyouconvertaobjecttoaregularexpressioninpython?
repr(x)Convertsobjectxtoanexpressionstring.
HowwillyouconvertaStringtoanobjectinpython?
eval(str)Evaluatesastringandreturnsanobject.
Howwillyouconvertastringtoatupleinpython?
tuple(s)Convertsstoatuple.
Howwillyouconvertastringtoalistinpython?
list(s)Convertsstoalist.
Howwillyouconvertastringtoasetinpython?
set(s)Convertsstoaset.
Howwillyoucreateadictionaryusingtuplesinpython?
dict(d)Createsadictionary.dmustbeasequenceof(key,value)tuples.
Howwillyouconvertastringtoafrozensetinpython?
frozenset(s)Convertsstoafrozenset.
Howwillyouconvertanintegertoacharacterinpython?
chr(x)Convertsanintegertoacharacter.
Howwillyouconvertanintegertoanunicodecharacterinpython?
unichr(x)ConvertsanintegertoaUnicodecharacter.
Howwillyouconvertasinglecharactertoitsintegervalueinpython?
ord(x)Convertsasinglecharactertoitsintegervalue.
Howwillyouconvertanintegertohexadecimalstringinpython?
hex(x)Convertsanintegertoahexadecimalstring.
Howwillyouconvertanintegertooctalstringinpython?
oct(x)Convertsanintegertoanoctalstring.
Whatisthepurposeof**operator?
**ExponentPerformsexponential(power)calculationonoperators.a**b=10tothepower20ifa=10andb=20.
Whatisthepurposeof//operator?
// Floor Division The division of operands where the result is the quotient in which the digits after the decimal point are
removed.
Whatisthepurposeofisoperator?
isEvaluatestotrueifthevariablesoneithersideoftheoperatorpointtothesameobjectandfalseotherwise.xisy,hereis
resultsin1ifid(x)equalsid(y).
Whatisthepurposeofnotinoperator?
notinEvaluatestotrueifitdoesnotfindsavariableinthespecifiedsequenceandfalseotherwise.xnotiny,herenotin
resultsina1ifxisnotamemberofsequencey.
Whatisthepurposebreakstatementinpython?
breakstatementTerminatestheloopstatementandtransfersexecutiontothestatementimmediatelyfollowingtheloop.
Whatisthepurposecontinuestatementinpython?
continuestatementCausesthelooptoskiptheremainderofitsbodyandimmediatelyretestitsconditionpriortoreiterating.
Whatisthepurposepassstatementinpython?
passstatementThepassstatementinPythonisusedwhenastatementisrequiredsyntacticallybutyoudonotwantany
commandorcodetoexecute.
Howcanyoupickarandomitemfromalistortuple?
choice(seq)Returnsarandomitemfromalist,tuple,orstring.
Howcanyoupickarandomitemfromarange?
randrange([start,]stop[,step])returnsarandomlyselectedelementfromrange(start,stop,step).
Howcanyougetarandomnumberinpython?
random()returnsarandomfloatr,suchthat0islessthanorequaltorandrislessthan1.
Howwillyousetthestartingvalueingeneratingrandomnumbers?
seed([x]) Sets the integer starting value used in generating random numbers. Call this function before calling any other
randommodulefunction.ReturnsNone.
Howwillyourandomizestheitemsofalistinplace?
shuffle(lst)Randomizestheitemsofalistinplace.ReturnsNone.
Howwillyoucapitalizesfirstletterofstring?
capitalize()Capitalizesfirstletterofstring.
Howwillyoucheckinastringthatallcharactersarealphanumeric?
isalnum()Returnstrueifstringhasatleast1characterandallcharactersarealphanumericandfalseotherwise.
Howwillyoucheckinastringthatallcharactersaredigits?
isdigit()Returnstrueifstringcontainsonlydigitsandfalseotherwise.
Howwillyoucheckinastringthatallcharactersareinlowercase?
islower()Returnstrueifstringhasatleast1casedcharacterandallcasedcharactersareinlowercaseandfalseotherwise.
Howwillyoucheckinastringthatallcharactersarenumerics?
isnumeric()Returnstrueifaunicodestringcontainsonlynumericcharactersandfalseotherwise.
Howwillyoucheckinastringthatallcharactersarewhitespaces?
isspace()Returnstrueifstringcontainsonlywhitespacecharactersandfalseotherwise.
Howwillyoucheckinastringthatitisproperlytitlecased?
istitle()Returnstrueifstringisproperly"titlecased"andfalseotherwise.
Howwillyoucheckinastringthatallcharactersareinuppercase?
isupper()Returnstrueifstringhasatleastonecasedcharacterandallcasedcharactersareinuppercaseandfalseotherwise.
Howwillyoumergeelementsinasequence?
join(seq)Merges(concatenates)thestringrepresentationsofelementsinsequenceseqintoastring,withseparatorstring.
Howwillyougetthelengthofthestring?
len(string)Returnsthelengthofthestring.
Howwillyougetaspacepaddedstringwiththeoriginalstringleftjustifiedtoatotalofwidthcolumns?
ljust(width[,fillchar])Returnsaspacepaddedstringwiththeoriginalstringleftjustifiedtoatotalofwidthcolumns.
Howwillyouconvertastringtoalllowercase?
lower()Convertsalluppercaselettersinstringtolowercase.
Howwillyouremoveallleadingwhitespaceinstring?
lstrip()Removesallleadingwhitespaceinstring.
Howwillyougetthemaxalphabeticalcharacterfromthestring?
max(str)Returnsthemaxalphabeticalcharacterfromthestringstr.
Howwillyougettheminalphabeticalcharacterfromthestring?
min(str)Returnstheminalphabeticalcharacterfromthestringstr.
Howwillyoureplacesalloccurrencesofoldsubstringinstringwithnewstring?
replace(old,new[,max])Replacesalloccurrencesofoldinstringwithneworatmostmaxoccurrencesifmaxgiven.
Howwillyouremoveallleadingandtrailingwhitespaceinstring?
strip([chars])Performsbothlstrip()andrstrip()onstring.
Howwillyouchangecaseforalllettersinstring?
swapcase()Invertscaseforalllettersinstring.
Howwillyougettitlecasedversionofstring?
title()Returns"titlecased"versionofstring,thatis,allwordsbeginwithuppercaseandtherestarelowercase.
Howwillyouconvertastringtoalluppercase?
upper()Convertsalllowercaselettersinstringtouppercase.
Howwillyoucheckinastringthatallcharactersaredecimal?
isdecimal()Returnstrueifaunicodestringcontainsonlydecimalcharactersandfalseotherwise.
Whatisthedifferencebetweendel()andremove()methodsoflist?
Toremovealistelement,youcanuseeitherthedelstatementifyouknowexactlywhichelement(s)youaredeletingorthe
remove()methodifyoudonotknow.
Whatistheoutputoflen([1,2,3])?
3.
Whatistheoutputof[1,2,3]+[4,5,6]?
[1,2,3,4,5,6]
Whatistheoutputof['Hi!']*4?
['Hi!','Hi!','Hi!','Hi!']
Whatistheoutputof3in[1,2,3]?
True
Whatistheoutputofforxin[1,2,3]:printx?
123
WhatistheoutputofL[2]ifL=[1,2,3]?
3,Offsetsstartatzero.
WhatistheoutputofL[2]ifL=[1,2,3]?
L[1]=3,L[2]=2,L[3]=1
WhatistheoutputofL[1:]ifL=[1,2,3]?
2,3,Slicingfetchessections.
Howwillyoucomparetwolists?
cmp(list1,list2)Compareselementsofbothlists.
Howwillyougetthelengthofalist?
len(list)Givesthetotallengthofthelist.
Howwillyougetthemaxvalueditemofalist?
max(list)Returnsitemfromthelistwithmaxvalue.
Howwillyougettheminvalueditemofalist?
min(list)Returnsitemfromthelistwithminvalue.
Howwillyougettheindexofanobjectinalist?
list.index(obj)Returnsthelowestindexinlistthatobjappears.
Howwillyouinsertanobjectatgivenindexinalist?
list.insert(index,obj)Insertsobjectobjintolistatoffsetindex.
Howwillyouremovelastobjectfromalist?
list.pop(obj=list[1])Removesandreturnslastobjectorobjfromlist.
Howwillyouremoveanobjectfromalist?
list.remove(obj)Removesobjectobjfromlist.
Howwillyoureversealist?
list.reverse()Reversesobjectsoflistinplace.
Howwillyousortalist?
list.sort([func])Sortsobjectsoflist,usecomparefuncifgiven.