Python and Algorithms

? Python and Algorithms ?

Mari Wahl, mari.wahl9@

University of New York at Stony Brook

May 24, 2013

¡°There¡¯s nothing to fear but the fear itself.

That¡¯s called recursion, and that would lead you

to infinite fear.¡±

Hello, human! Welcome to my book on Python and algorithms! If you

are reading this you probably agree with me that those two can be a

lot of fun together (or you might be lost, and in this case I

suggest you give it a try anyway!). Also, many of the examples

shown here are available in my git repository, together with several

other (more advanced) examples for abstract data structures, trees,

graphs, and solutions for the Euler Project and the Topcoder

website. Don¡¯t forget to check them out!

This text was written purely for fun (I know, I know, this is a

broad definition of the word fun...) with no pretensions for

anything big, so please forgive me (or better, let me know) if you

find any typo or mistake. I am not a computer scientist by

formation (I am actually an almost-I-swear-it-is-close-Ph.D. in

Physics) so this maybe makes things a little less usual (or risky?).

I hope you have fun!

Mari, Stony Brook, NY

Summer/2013

4

Contents

I

Flying with Python

9

1 Numbers

1.1 Integers . . . . . . . . .

1.2 Floats . . . . . . . . . .

1.3 Complex Numbers . . .

1.4 The fractions Module

1.5 The decimal Module . .

1.6 Other Representations .

1.7 Additional Exercises . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

11

11

12

13

14

15

15

16

2 Built-in Sequence Types

2.1 Strings . . . . . . . . .

2.2 Tuples . . . . . . . . .

2.3 Lists . . . . . . . . . .

2.4 Bytes and Byte Arrays

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

25

27

33

35

43

3 Collection Data Structures

3.1 Sets . . . . . . . . . . . . . . . . .

3.2 Dictionaries . . . . . . . . . . . . .

3.3 Python¡¯s collection Data Types

3.4 Additional Exercises . . . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

45

45

49

54

58

.

.

.

.

.

.

63

63

66

72

79

81

83

.

.

.

.

4 Python¡¯s Structure and Modules

4.1 Modules in Python . . . . . . . .

4.2 Control Flow . . . . . . . . . . .

4.3 File Handling . . . . . . . . . . .

4.4 Multiprocessing and Threading .

4.5 Error Handling in Python . . . .

4.6 Debugging and Profiling . . . . .

5

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

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

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download