Algorithmic Problem Solving with Python - School of Electrical ...

unique combinations or values: 000, 001, 010, 011, 100, 101, 110, and 111. .... A program may be written such that it is free of typographical errors and does ... certain type but be horribly ill-suited for solving problems outside the domain for ...
2MB Sizes 3 Downloads 217 Views
Algorithmic Problem Solving with Python John B. Schneider

Shira Lynn Broschat May 31, 2015

Jess Dahmen

ii

Contents 1

2

3

Introduction 1.1 Modern Computers . . . . . . . . . . . . . 1.2 Computer Languages . . . . . . . . . . . . 1.3 Python . . . . . . . . . . . . . . . . . . . 1.4 Algorithmic Problem Solving . . . . . . . . 1.5 Obtaining Python . . . . . . . . . . . . . . 1.6 Running Python . . . . . . . . . . . . . . 1.6.1 Interactive Sessions and Comments 1.6.2 Running Commands from a File . . 1.7 Bugs . . . . . . . . . . . . . . . . . . . . . 1.8 The help() Function . . . . . . . . . . . 1.9 Comments on Learning New Languages . . 1.10 Chapter Summary . . . . . . . . . . . . . . 1.11 Review Questions . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

Core Basics 2.1 Literals and Types . . . . . . . . . . . . . . . . . 2.2 Expressions, Arithmetic Operators, and Precedence 2.3 Statements and the Assignment Operator . . . . . 2.4 Cascaded and Simultaneous Assignment . . . . . 2.5 Multi-Line Statements and Multi-Line Strings . . . 2.6 Identifiers and Keywords . . . . . . . . . . . . . . 2.7 Names and Namespaces . . . . . . . . . . . . . . 2.8 Additional Arithmetic Operators . . . . . . . . . . 2.8.1 Exponentiation . . . . . . . . . . . . . . . 2.8.2 Floor Division . . . . . . . . . . . . . . . 2.8.3 Modulo and divmod() . . . . . . . . . . 2.8.4 Augmented Assignment . . . . . . . . . . 2.9 Chapter Summary . . . . . . . . . . . . . . . . . . 2.10 Review Questions . . . . . . . . . . . . . . . . . . 2.11 Exercises . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . .

1 1 2 3 6 7 8 9 11 12 13 14 15 15

. . . . . . . . . . . . . . .

19 19 22 24 27 29 30 32 37 37 37 38 40 42 43 49

Input and Type Conversion 51 3.1 Obtaining Input: input() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.2 Explicit Type Conversion: int(), float(), and str() . . . . . . . . . . . . . 53 iii

iv

CONTENTS 3.3 3.4 3.5 3.6

4

5

6

Evaluating Strings: eval() Chapter Summary . . . . . . Review Questions . . . . . . Exercises . . . . . . . . . .

Functions 4.1 Void Functions and None . 4.2 Creating Void Functions . 4.3 Non-Void Functions . . . 4.4 Scope of Variables . . . . 4.5 Scope of Functions . . . . 4.6 print() vs. return . . 4.7 Using a main() Function 4.8 Optional Parameters . . . . 4.9 Chapter Summary . . . . . 4.10 Review Questions . . . . . 4.11 Ex