Python and Algorithms - Stony Brook University
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
11
1.1 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.2 Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.3 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4 The fractions Module . . . . . . . . . . . . . . . . . . . . . 14
1.5 The decimal Module . . . . . . . . . . . . . . . . . . . . . . . 15
1.6 Other Representations . . . . . . . . . . . . . . . . . . . . . . 15
1.7 Additional Exercises . . . . . . . . . . . . . . . . . . . . . . . 16
2 Built-in Sequence Types
25
2.1 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.3 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.4 Bytes and Byte Arrays . . . . . . . . . . . . . . . . . . . . . . 43
3 Collection Data Structures
45
3.1 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.2 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.3 Python's collection Data Types . . . . . . . . . . . . . . . 54
3.4 Additional Exercises . . . . . . . . . . . . . . . . . . . . . . . 58
4 Python's Structure and Modules
63
4.1 Modules in Python . . . . . . . . . . . . . . . . . . . . . . . . 63
4.2 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
4.3 File Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.4 Multiprocessing and Threading . . . . . . . . . . . . . . . . . 79
4.5 Error Handling in Python . . . . . . . . . . . . . . . . . . . . 81
4.6 Debugging and Profiling . . . . . . . . . . . . . . . . . . . . . 83
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.