1.6.THEMAGICOFPYTHON 5
(Program)
Compiler Machine
Code
Running
Inputs
Outputs
Source
Code
Program
Figure1.2:Compilinga High-Level Language
Code Computer
Inputs
Outputs
Source
(Program)
Running an
Interpreter
Figure1.3:Interpretinga High-Level Language.
Eachkindofcomputerhasitsownmachinelanguage.A programfora PentiumCPUwon’t runona Mac-
intoshthatsportsa PowerPC.Ontheotherhand,a programwrittenina high-level languagecanberunon
many differentkindsofcomputersaslongasthereis a suitablecompilerorinterpreter(whichis justanother
program).Forexample,if I designa new computer, I canalsoprograma Pythoninterpreterforit,andthen
any programwritteninPythoncanberunonmynew computer, asis.
1.6 TheMagicofPython
Nowthatyouhave allthetechnicaldetails,it’s timetostarthavingfunwithPython.Theultimategoalis
tomake thecomputerdoourbidding.To thisend,wewillwriteprogramsthatcontrolthecomputational
processesinsidethemachine.Youhave alreadyseenthatthereis nomagicinthisprocess,butinsomeways
programmingfeelslike magic.
Thecomputationalprocessesinsidethecomputerarelike magicalspiritsthatwecanharnessforour
work.Unfortunately, thosespiritsonlyunderstanda veryarcanelanguagethatwedonotknow. Whatwe
needis a friendlyGeniethatcandirectthespiritstofulfillourwishes.OurGenieis a Pythoninterpreter. We
cangive instructionstothePythoninterpreter, andit directstheunderlyingspiritstocarryoutourdemands.
We communicatewiththeGeniethrougha speciallanguageofspellsandincantations(i.e.,Python).The
bestwaytostartlearningaboutPythonis toletourGenieoutofthebottleandtrysomespells.
YoucanstartthePythoninterpreterinaninteractive modeandtypeinsomecommandstoseewhat
happens.Whenyoufirststarttheinterpreterprogram,youmayseesomethinglike thefollowing:
Python 2.1 (#1, Jun 21 2001,11:39:00)
[GCC pgcc-2.91.66 19990314 (egcs-1.1.2 release)] on linux
Type "copyright", "credits"or "license" for more information.
The is a PythonpromptindicatingthattheGenieis waitingforusto give it a command.Inprogramming
languages,a completecommandis calledastatement.
Hereis a sampleinteractionwiththePythoninterpreter.