0% found this document useful (0 votes)
166 views

0804 Fundamentals of Python Programming

Uploaded by

Khadija Lasri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views

0804 Fundamentals of Python Programming

Uploaded by

Khadija Lasri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Fundamentals of

Python
Programming

RA F T
D
Richard L. Halterman
Southern Adventist University

July 26, 2018


Fundamentals of Python Programming
Copyright © 2017 Richard L. Halterman. All rights reserved.

See the preface for the terms of use of this document.


i

Contents

1 The Context of Software Development 1


1.1 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Learning Programming with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Writing a Python Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 The Python Interactive Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.6 A Longer Python program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

2 Values and Variables 13


2.1 Integer and String Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Variables and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.3 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.4 Floating-point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.5 Control Codes within Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.6 User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.7 Controlling the print Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.8 String Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.9 Multi-line Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

3 Expressions and Arithmetic 43


3.1 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.2 Mixed Type Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.3 Operator Precedence and Associativity . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.4 Formatting Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

©2017 Richard L. Halterman Draft date: July 26, 2018


CONTENTS ii

3.5 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.6 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.6.1 Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.6.2 Run-time Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.6.3 Logic Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.7 Arithmetic Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.8 More Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.9 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

4 Conditional Execution 67
4.1 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4.2 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
4.3 The Simple if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.4 The if/else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4.5 Compound Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
4.6 The pass Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
4.7 Floating-point Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
4.8 Nested Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
4.9 Multi-way Decision Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
4.10 Multi-way Versus Sequential Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . 97
4.11 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
4.12 Errors in Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
4.13 Logic Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
4.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

5 Iteration 113
5.1 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
5.2 Definite Loops vs. Indefinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
5.3 The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
5.4 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
5.5 Abnormal Loop Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
5.5.1 The break statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
5.5.2 The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
5.6 while/else and for/else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

©2017 Richard L. Halterman Draft date: July 26, 2018


CONTENTS iii

5.7 Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139


5.8 Iteration Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
5.8.1 Computing Square Root . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
5.8.2 Drawing a Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
5.8.3 Printing Prime Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
5.8.4 Insisting on the Proper Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
5.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

6 Using Functions 157


6.1 Introduction to Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
6.2 Functions and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
6.3 The Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
6.4 Standard Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
6.5 time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
6.6 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
6.7 System-specific Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
6.8 The eval and exec Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
6.9 Turtle Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
6.10 Other Techniques for Importing Functions and Modules . . . . . . . . . . . . . . . . . . 185
6.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

7 Writing Functions 193


7.1 Function Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
7.2 Parameter Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
7.3 Documenting Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
7.4 Function Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
7.4.1 Better Organized Prime Generator . . . . . . . . . . . . . . . . . . . . . . . . . 213
7.4.2 Command Interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
7.4.3 Restricted Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
7.4.4 Better Die Rolling Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
7.4.5 Tree Drawing Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
7.4.6 Floating-point Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
7.5 Refactoring to Eliminate Code Duplication . . . . . . . . . . . . . . . . . . . . . . . . . 222
7.6 Custom Functions vs. Standard Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 224
7.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

©2017 Richard L. Halterman Draft date: July 26, 2018


Click here to download full PDF material

You might also like