xvi
Contents
2 A Few More Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.1 Using Python Interactively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.1.1 The IPython Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.1.2 Command History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.1.3 TAB Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.2 Variables, Objects and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.2.1 Choose Descriptive Variable Names . . . . . . . . . . . . . . . . . . . 41
2.2.2 Reserved Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.2.3 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.2.4 Object Type and Type Conversion . . . . . . . . . . . . . . . . . . . . . 42
2.2.5 Automatic Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.2.6 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.2.7 Division—Quotient and Remainder . . . . . . . . . . . . . . . . . . . 45
2.2.8 Using Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.2.9 Round-Off Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.2.10 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.3 Numerical Python Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.3.1 Array Creation and Array Elements . . . . . . . . . . . . . . . . . . . 47
2.3.2 Indexing an Array from the End . . . . . . . . . . . . . . . . . . . . . . 50
2.3.3 Index Out of Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.3.4 Copying an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.3.5 Slicing an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.3.6 Two-Dimensional Arrays and Matrix Computations . . . . . . 52
2.4 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3 Loops and Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.1 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.1.1 Example: Printing the 5 Times Table . . . . . . . . . . . . . . . . . . . 59
3.1.2 Characteristics of a Typical for Loop . . . . . . . . . . . . . . . . . . . 60
3.1.3 Combining for Loop and Array . . . . . . . . . . . . . . . . . . . . . . . 62
3.1.4 Using the range Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.1.5 Using break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.2 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.2.1 Example: Finding the Time of Flight . . . . . . . . . . . . . . . . . . . 65
3.2.2 Characteristics of a Typical while Loop . . . . . . . . . . . . . . . . 66
3.3 Branching (if, elif and else) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
3.3.1 Example: Judging the Water Temperature . . . . . . . . . . . . . . 68
3.3.2 The Characteristics of Branching . . . . . . . . . . . . . . . . . . . . . . 70
3.3.3 Example: Finding the Maximum Height . . . . . . . . . . . . . . . . 70
3.3.4 Example: Random Walk in Two Dimensions . . . . . . . . . . . . 72
3.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
4 Functions and the Writing of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
4.1 Functions: How to Write Them? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
4.1.1 Example: Writing Our First Function . . . . . . . . . . . . . . . . . . 79
4.1.2 Characteristics of a Function Definition . . . . . . . . . . . . . . . . 80
4.1.3 Functions and the Main Program . . . . . . . . . . . . . . . . . . . . . . 82
4.1.4 Local Versus Global Variables . . . . . . . . . . . . . . . . . . . . . . . . 83
Précédent

- 15/350

Suivant