16 CHAPTER2. WRITINGSIMPLEPROGRAMS
Thesimplestkindofexpressionis aliteral. A literalis usedtoindicatea specificvalue.Inchaos.py
youcanfindthenumbers3.9and 1. Theconvert.pyprogramcontains9.0,5.0, and 32. Theseare
allexamplesofnumericliterals,andtheirmeaningis obvious: 32 represents,well,32.
Asimpleidentifiercanalsobeanexpression. We useidentifiersasvariablestogive namestovalues.
Whenanidentifierappearsinanexpression,thisvalueisretrievedtoprovidea resultfortheexpression.
Hereis aninteractionwiththePythoninterpreterthatillustratestheuseofvariablesasexpressions.
x = 5
x
5
print x
5
print spam
Traceback (innermostlast):
File "<pyshell#34>",line 1, in?
print spam
NameError: spam
Firstthevariablexis assignedthevalue5 (usingthenumericliteral 5 ). Thenext linehasPythonevaluatethe
expressionx. Pythonspitsback 5 , whichis thevaluethatwasjustassignedtox. Ofcourse,wegetthesame
resultwhenweputxina printstatement.Thelastexampleshowswhathappenswhenweusea variablethat
hasnotbeenassigneda value.Pythoncannotfinda value,soit reportsaNameError. Thissaysthatthereis
novaluewiththatname.A variablemustalwaysbeassigneda valuebeforeit canbeusedinanexpression.
Morecomplex andinterestingexpressionscanbeconstructedbycombiningsimplerexpressionswith
operators. Fornumbers,Pythonprovidesthenormalsetofmathematicaloperations:addition,subtraction,
multiplication,division,andexponentiation. ThecorrespondingPythonoperatorsare:+,-,*,/, and**.
Herearesomeexamplesofcomplex expressionsfromchaos.pyandconvert.py
3.9 x (1 - x)
9.0 / 5.0 * celsius+ 32
Spacesareirrelevantwithinanexpression.Thelastexpressioncouldhave beenwritten9.0/5.0*celsius+32
andtheresultwouldbeexactlythesame.Usuallyit’s a goodideatoplacesomespacesinexpressionsto
make themeasiertoread.
Python’s mathematicaloperatorsobey thesamerulesofprecedenceandassociativitythatyoulearned
inyourmathclasses,includingusingparenthesestomodifytheorderofevaluation.Youshouldhave lit-
tletroubleconstructingcomplex expressionsinyourownprograms. Dokeepinmindthatonlytheround
parenthesesareallowedinexpressions,butyoucannestthemif necessarytocreateexpressionslike this.
((x1 - x2) / 2*n) + (spam/ k**3)
Ifyouarereadingcarefully, youmaybecuriouswhy, inhertemperatureconversionprogram,Suzie
Programmerchosetowrite9.0/5.0ratherthan9/5. Bothofthesearelegalexpressions,butthey give
differentresults.ThismysterywillbediscussedinChapter3.If youcan’t standthewait,trythemoutfor
yourselfandseeif youcanfigureoutwhat’s goingon.
2.4 OutputStatements.
Nowthatyouhave thebasicbuildingblocks,identifierandexpression,youarereadyfora morecomplete
descriptionofvariousPythonstatements.Youalreadyknow thatyoucandisplayinformationonthescreen
usingPython’sprintstatement.Butwhatexactlycanbeprinted?Python,like allprogramminglanguages,
hasa precisesetofrulesforthesyntax(form)andsemantics(meaning)ofeachstatement.Computerscientists
have developedsophisticatednotationscalledmeta-languagesfordescribingprogramminglanguages.Inthis
bookwewillrelyona simpletemplatenotationtoillustratethesyntaxofstatements.
Herearethepossibleformsoftheprintstatement: