xvi Contributor List David Hutchins caught a typo in the Foreword. Gregor Lingl is teaching Python at a high school in Vienna,Austria.He is working on a German translation of the book,and he caught a couple of bad errors in Chapter 5. Julie Peters caught a typo in the Preface. Florin Oprina sent in an improvement in makeTime,a correction in printTime,and a nice typo. D.J.Webre suggested a clarification in Chapter 3. Ken found a fistful of errors in Chapters 8,9 and 11. Ivo Wever caught a typo in Chapter 5 and suggested a clarification in Chap- ter 3. Curtis Yanko suggested a clarification in Chapter 2. Ben Logan sent in a number of typos and problems with translating the book into HTML. Jason Armstrong saw the missing word in Chapter 2. Louis Cordier noticed a spot in Chapter 16 where the code didn't match the text. Brian Cain suggested several clarifications in Chapters 2 and 3. Rob Black sent in a passel of corrections,including some changes for Python 2.2. Jean-Philippe Rey at Ecole Centrale Paris sent a number of patches,includ- ing some updates for Python 2.2 and other thoughtful improvements. Jason Mader at George Washington University made a number of useful suggestions and corrections. ●Jan Gundtofte-Bruun reminded us that“a error'”is an error. ·Abel David and Alexis Dinno reminded us that the plural of“matrix”is “matrices”,not“matrixes'”.This error was in the book for years,but two readers with the same initials reported it on the same day.Weird. Charles Thayer encouraged us to get rid of the semi-colons we had put at the ends of some statements and to clean up our use of "argument"and “parameter
xvi Contributor List • David Hutchins caught a typo in the Foreword. • Gregor Lingl is teaching Python at a high school in Vienna, Austria. He is working on a German translation of the book, and he caught a couple of bad errors in Chapter 5. • Julie Peters caught a typo in the Preface. • Florin Oprina sent in an improvement in makeTime, a correction in printTime, and a nice typo. • D. J. Webre suggested a clarification in Chapter 3. • Ken found a fistful of errors in Chapters 8, 9 and 11. • Ivo Wever caught a typo in Chapter 5 and suggested a clarification in Chapter 3. • Curtis Yanko suggested a clarification in Chapter 2. • Ben Logan sent in a number of typos and problems with translating the book into HTML. • Jason Armstrong saw the missing word in Chapter 2. • Louis Cordier noticed a spot in Chapter 16 where the code didn’t match the text. • Brian Cain suggested several clarifications in Chapters 2 and 3. • Rob Black sent in a passel of corrections, including some changes for Python 2.2. • Jean-Philippe Rey at Ecole Centrale Paris sent a number of patches, including some updates for Python 2.2 and other thoughtful improvements. • Jason Mader at George Washington University made a number of useful suggestions and corrections. • Jan Gundtofte-Bruun reminded us that “a error” is an error. • Abel David and Alexis Dinno reminded us that the plural of “matrix” is “matrices”, not “matrixes”. This error was in the book for years, but two readers with the same initials reported it on the same day. Weird. • Charles Thayer encouraged us to get rid of the semi-colons we had put at the ends of some statements and to clean up our use of “argument” and “parameter
xvii Roger Sperberg pointed out a twisted piece of logic in Chapter 3. Sam Bull pointed out a confusing paragraph in Chapter 2. Andrew Cheung pointed out two instances of "use before def." Hans Batra found an error in Chapter 16. Chris Seberino suggested some improvements in the Preface. Yuri Takhteyev pointed out a problem with single and double quotes
xvii • Roger Sperberg pointed out a twisted piece of logic in Chapter 3. • Sam Bull pointed out a confusing paragraph in Chapter 2. • Andrew Cheung pointed out two instances of “use before def.” • Hans Batra found an error in Chapter 16. • Chris Seberino suggested some improvements in the Preface. • Yuri Takhteyev pointed out a problem with single and double quotes
xviii Contributor List
xviii Contributor List
Contents Foreword Preface vii Contributor List xiii 1 The way of the program 1 1.1 The Python programming language.····.···· 1 1.2 What is a program? 3 1.3 What is debugging? 4 1.4 Formal and natural languages···.. 6 1.5 The first program········· 8 1.6 Glossary.·········· 8 2 Variables,expressions and statements 11 2.1 Values and types..........·.·.··· 11 2.2 Variables........··· 12 2.3 Variable names and keywords 13 2.4 Statements.········ 15 2.5 Evaluating expressions······ 16 2.6 Operators and operands..... 17
Contents Foreword v Preface vii Contributor List xiii 1 The way of the program 1 1.1 The Python programming language . . . . . . . . . . . . . . . . . 1 1.2 What is a program? . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.3 What is debugging? . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4 Formal and natural languages . . . . . . . . . . . . . . . . . . . . 6 1.5 The first program . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.6 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2 Variables, expressions and statements 11 2.1 Values and types . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3 Variable names and keywords . . . . . . . . . . . . . . . . . . . . 13 2.4 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.5 Evaluating expressions . . . . . . . . . . . . . . . . . . . . . . . . 16 2.6 Operators and operands . . . . . . . . . . . . . . . . . . . . . . . 17
XX Contents 2.7 Order of operations..·.·..······.· 17 2.8 Operations on strings.·.·· 18 2.9 Composition...···. 19 2.10 Comments..... 19 2.11 Glossary.··· 20 3 Functions 23 3.1 Function calls.... 23 3.2 Type conversion.· 24 3.3 Type coercion.··. 24 3.4 Math functions...· 25 3.5 Composition...... 26 3.6 Adding new functions 26 3.7 Definitions and use..... 29 3.8 Flow of execution...... 29 3.9 Parameters and arguments.··· 30 3.10 Variables and parameters are local 31 3.11 Stack diagrams。,。.。······· 32 3.12 Functions with results..... 33 3.13 Glossary...······ 34 4 Conditionals and recursion 37 4.1 The modulus operator..... 37 4.2 Boolean expressions 44 37 4.3 Logical operators···· 38 4.4 Conditional execution 39 4.5 Alternative execution.··· 39 4.6 Chained conditionals..... 40
xx Contents 2.7 Order of operations . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.8 Operations on strings . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.9 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.10 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 3 Functions 23 3.1 Function calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.2 Type conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.3 Type coercion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.4 Math functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.5 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.6 Adding new functions . . . . . . . . . . . . . . . . . . . . . . . . 26 3.7 Definitions and use . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.8 Flow of execution . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.9 Parameters and arguments . . . . . . . . . . . . . . . . . . . . . . 30 3.10 Variables and parameters are local . . . . . . . . . . . . . . . . . 31 3.11 Stack diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.12 Functions with results . . . . . . . . . . . . . . . . . . . . . . . . 33 3.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 4 Conditionals and recursion 37 4.1 The modulus operator . . . . . . . . . . . . . . . . . . . . . . . . 37 4.2 Boolean expressions . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.3 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 4.4 Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . 39 4.5 Alternative execution . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.6 Chained conditionals . . . . . . . . . . . . . . . . . . . . . . . . . 40