b
An Introduction to Computer Science
b b b b
JohnbM.bZelle,bPh.D.
PreliminarybSecondbEditionbFa
llb2009
,Copyrightb ©b 2009b Johnb Mb Zelle.
Allb rightsb reserved.b Nob partb ofb thisb publicationb mayb beb reproduced,b storedb inb ab retrievalb system
,borbtransmitted,binbanybformborbbybanybmeans,belectronic,bmechanical,bphotocopying,brecording,borb
otherwise,bwithoutbpriorbwrittenbpermission.
ThisbdocumentbwasbtypesetbbybthebauthorbwithbLATEXb2ǫ.
, Contents
1 ComputersbandbPrograms 1
1.1 ThebUniversalbMachine ................................................................................................................ 1
1.2 ProgrambPower ............................................................................................................................. 2
1.3 Whatbisb Computerb Science? ......................................................................................................... 3
1.4 HardwarebBasics ........................................................................................................................... 4
1.5 ProgrammingbLanguages ............................................................................................................. 5
1.6 Theb Magicb ofb Python .................................................................................................................... 7
1.7 Insidebab Pythonb Program ........................................................................................................... 12
1.8 Chaosb andb Computers ................................................................................................................ 14
1.9 Chapterb Summary....................................................................................................................... 16
1.10 Exercises...................................................................................................................................... 17
2 Writingb Simpleb Programs 21
2.1 Theb Softwareb Developmentb Process ........................................................................................ 21
2.2 ExamplebProgram:b TemperaturebConverter ............................................................................ 22
2.3 ElementsbofbPrograms ................................................................................................................ 24
2.3.1 Names ............................................................................................................................. 24
2.3.2 Expressions..................................................................................................................... 25
2.4 Outputb Statements ..................................................................................................................... 27
2.5 AssignmentbStatements .............................................................................................................. 28
2.5.1 Simpleb Assignment ........................................................................................................ 28
2.5.2 AssigningbInput .............................................................................................................. 30
2.5.3 Simultaneousb Assignment............................................................................................. 32
2.6 Definiteb Loops............................................................................................................................. 34
2.7 Exampleb Program:b Futureb Value .............................................................................................. 37
2.8 Chapterb Summary....................................................................................................................... 39
2.9 Exercises...................................................................................................................................... 40
3 Computingb withb Numbers 45
3.1 Numericb Datab Types................................................................................................................... 45
3.2 Usingb theb Mathb Library ............................................................................................................. 49
3.3 Accumulatingb Results:b Factorial ............................................................................................... 51
i