4 CHAPTER1. COMPUTERSANDPROGRAMS
1.5 ProgrammingLanguages
Rememberthata programis justa sequenceofinstructionstellinga computerwhattodo.Obviously, we
needtoprovidethoseinstructionsina languagethata computercanunderstand.It wouldbeniceif wecould
justtella computerwhattodousingournative language,like they doinsciencefictionmovies.(“Computer,
how longwillit take toreachplanetAlphalphaatmaximumwarp?”)Unfortunately, despitethecontinuing
effortsofmany top-flightcomputerscientists(includingyourauthor),designinga computertounderstand
humanlanguageis stillanunsolvedproblem.
Evenif computerscouldunderstandus,humanlanguagesarenotverywellsuitedfordescribingcomplex
algorithms.Naturallanguageis fraughtwithambiguityandimprecision.Forexample,if I say:“Isawthe
manintheparkwiththetelescope,” didI have thetelescope,ordidtheman?Andwhowasinthepark?We
understandeachothermostofthetimeonlybecauseallhumanssharea vaststoreofcommonknowledgeand
experience.Eventhen,miscommunicationis commonplace.
Computerscientistshave gottenaroundthisproblembydesigningnotationsforexpressingcomputa-
tionsinanexact,andunambiguousway. Thesespecialnotationsarecalledprogramminglanguages. Every
structureina programminglanguagehasa preciseform(itssyntax) anda precisemeaning(itssemantics). A
programminglanguageis somethinglike a codeforwritingdowntheinstructionsthata computerwillfollow.
Infact,programmersoftenrefertotheirprogramsascomputercode, andtheprocessofwritinganalgorithm
ina programminglanguageis calledcoding.
Pythonis oneexampleofa programminglanguage. It is thelanguagethatwewillusethroughoutthis
book.Youmayhave heardofsomeotherlanguages,suchasC++,Java, Perl,Scheme,orBASIC.Although
theselanguagesdifferin many details,they allsharethepropertyofhavingwell-defined,unambiguoussyntax
andsemantics.
Allofthelanguagesmentionedabove areexamplesofhigh-levelcomputerlanguages.Althoughthey are
precise,they aredesignedtobeusedandunderstoodbyhumans.Strictlyspeaking,computerhardwarecan
onlyunderstandverylow-level languageknownasmachinelanguage.
Supposewewantthecomputertoaddtwo numbers.TheinstructionsthattheCPUactuallycarriesout
mightbesomethinglike this.
load the number frommemory location 2001 into the CPU
load the number frommemory location 2002 into the CPU
Add the two numbersin the CPU
store the result intolocation 2003
Thisseemslike a lotofworktoaddtwo numbers,doesn’t it?Actually, it’s evenmorecomplicatedthanthis
becausetheinstructionsandnumbersarerepresentedinbinarynotation(assequencesof0sand1s).
Ina high-level languagelike Python,theadditionoftwo numberscanbeexpressedmorenaturally:c =
a + b. That’s a loteasierforustounderstand,butweneedsomewaytotranslatethehigh-level language
intothemachinelanguagethatthecomputercanexecute.Therearetwo waysto dothis:a high-level language
caneitherbecompiledorinterpreted.
Acompileris a complex computerprogramthattakesanotherprogramwrittenina high-level language
andtranslatesit intoanequivalentprograminthemachinelanguageofsomecomputer. Figure1.2shows
a blockdiagramofthecompilingprocess.Thehigh-level programis calledsourcecode, andtheresulting
machinecodeis a programthatthecomputercandirectlyexecute.Thedashedlineinthediagramrepresents
theexecutionofthemachinecode.
Aninterpreteris a programthatsimulatesa computerthatunderstandsa high-level language.Ratherthan
translatingthesourceprogramintoa machinelanguageequivalent,theinterpreteranalyzesandexecutesthe
sourcecodeinstructionbyinstructionasnecessary. Figure1.3illustratestheprocess.
Thedifferencebetweeninterpretingandcompilingisthatcompilingisa one-shottranslation;oncea
programis compiled,it mayberunoverandoveragainwithoutfurtherneedforthecompilerorthesource
code.Intheinterpretedcase,theinterpreterandthesourceareneededeverytimetheprogramruns.Compiled
programstendtobefaster, sincethetranslationisdoneonceandforall,butinterpretedlanguageslend
themselvestoa moreflexibleprogrammingenvironmentasprogramscanbedevelopedandruninteractively.
Thetranslationprocesshighlightsanotheradvantagethathigh-levellanguageshave overmachinelan-
guage:portability. Themachinelanguageofa computeris createdbythedesignersoftheparticularCPU.