title count CodeLab count 19s subTot 19s CodeLab
exercise
odeLab
title
Introducing Python book
Table of Contents






Page numbers might be a little off










Begin Unit A CodeLab




Preface xv










1. A Taste of Py 1




Python in the Real World 5




Python versus Language X 6




So, Why Python? 9




When Not to Use Python 9




Python 2 versus Python 3 10




Installing Python 10




Running Python 11




Using the Interactive Interpreter 11




Use Python Files 12




What’s Next? 13




Your Moment of Zen 13




Things to Do 14
















2. Py Ingredients: Numbers, Strings,
and Variables 152. Py Ingredients: Numbers, Strings,
and Variables 15
Variables, Names, and Objects 15 5 5 5 51001 51014 51019 51013 51043 Assignment of variables
Numbers 19 14 3 8 51872 51029 51030 51031 51085 51032 51033 51034 51044 51045 51046 51048 51047 59249 Arithmetic expressions
Integers 19




Precedence 23




Bases 24




Type Conversions 25 8 8 16 51750 51751 51752 51753 51026 51024 51843 51842 Conversions (some of these are near this section in the book)
How Big Is an int? 26




Floats 27




Math Functions 27




Strings 27




Create with Quotes 28




Convert Data Types by Using str() 30




Escape with \ 30




Combine with + 31 2 2 18 51611 51612 list slicing/splicing/concatenation (first two)
Duplicate with * 32




Extract a Character with [] 32 11 3 21 51840 51841 51836 51847 51846 51763 51760 51761 51762 51855 51868 Strings
Begin Unit B CodeLab




Slice with [start: end: step] 33 11 7 28 51764 51765 51771 51772 51850 51851 51852 81853 51844 51848 51854 Slicing
Slice with [start: end: step] 33 7 4 32 51613 51614 51615 51616 51617 51618 51619 list slicing/splicing/concatenation (second group)
Get Length with len() 35 2 2 34 51211 51602 list-methods (part here)
Split with split() 35 5 3 37 51078 51079 51080 51081 51082
Combine with join() 36




Playing with Strings 36 3 3 40 51849 51889 51890 finding
Begin Unit C CodeLab




Case and Alignment 37




Substitute with replace() 38




More String Things 39




Things to Do 39
















3. Py Filling: Lists, Tuples,
Dictionaries, and Sets 41
Lists and Tuples 41




Lists 42




Create with [] or list() 42 5 4 44 51600 51601 51192 51193 51194 List initialization
Convert Other Data Types to Lists with list() 43




Get an Item by Using [offset] 43 14 2 46 51203 51201 51195 51196 51197 51199 51200 51202 51204 51205 51206 51207 51208 51209 list indexing (move 51280 later with loops)
Lists of Lists 44




Change an Item by [offset] 45




Get a Slice to Extract Items by Offset Range 45




Add an Item to the End with append() 46 10 2 48 51603 51275 51276 51277 51278 51284 51285 51287 51288 51289 list-addition/retrieval/deletion
Combine Lists by Using extend() or += 46




Add an Item by Offset with insert() 46




Delete an Item by Offset with del 47 3 2 50 51609 51607 51608 list-addition/retrieval/deletion
Delete an Item by Value with remove() 47




Get an Item by Offset and Delete It by Using pop() 47 2 2 52 51605 51606 list-methods(part here)
Find an Item’s Offset by Value with index() 48




Test for a Value with in 48 3 3 55 51620 51621 51213 list membership
Count Occurrences of a Value by Using count() 49




Convert to a String with join() 49




Reorder Items with sort() 49 6 4 59 51212 51610 51256 51294 51295 51296 list-methods(part here)
Reorder Items with sort() 49 1 1 60 51283 functions and lists
Begin Unit D CodeLab




Get Length by Using len() 50




Assign with =, Copy with copy() 50




Tuples 52




Create a Tuple by Using () 52 5 3 63 31700 51701 51706 51707 51708
Tuples versus Lists 53




Dictionaries 53




Create with {} 54 2 2 65 51815 51816
Convert by Using dict() 54




Add or Change an Item by [ key] 55Add or Change an Item by [ key] 55 5 3 68 51818 51820 51821 51822 51823
Combine Dictionaries with update() 56




Delete an Item by Key with del 57 3 3 71 51819 51824 51825
Delete All Items by Using clear() 57




Test for a Key by Using in 58




Get an Item by [key] 58 1 1 72 51817
Get All Keys by Using keys() 59




Get All Values by Using values() 59




Get All Key-Value Pairs by Using items() 59




Assign with =, Copy with copy() 59




Sets 60




Create with set() 61 4 2 74 51800 51802 51803 51804
Create with set() 61 6 3 77 51805 51806 51807 51809 51810 51811
Convert from Other Data Types with set() 61 1 1 78 51248
Test for Value by Using in 62 1 1 79 51808
Begin Unit E CodeLab




Combinations and Operators 63




Compare Data Structures 66




Make Bigger Data Structures 66




Things to Do 67
















4. Py Crust: Code Structures 69




Comment with # 69




Continue Lines with \ 70




Compare with if, elif, and else 71 14 2 81 51109 51112 51113 51173 51061 51062 51063 51165 51166 51167 51110 51111 51065 51064 Logical Operators
Compare with if, elif, and else 71 8 2 83 51114 51115 51116 51117 51102 51217 51160 51896 if
Compare with if, elif, and else 71 4 2 85 51891 51892 51893 51897 boolean values and variables
Compare with if, elif, and else 71 5 2 87 51118 51119 51120 51121 51122 if-else
Compare with if, elif, and else 71 3 3 90 51900 51898 51899 cascaded and nested if
Compare with if, elif, and else 71 10 2 92 51915 51912 51913 51914 51910 51911 51901 51902 51903 51905 range-testing
What Is True? 74




Repeat with while 75 6 2 94 51180 51181 51187 51251 51252 51253 while loops
Repeat with while 75 9 3 97 51935 51936 51937 51938 51939 51940 51941 51942 51943 input testing loops
Repeat with while 75 1 1 98 51007 loops and tuples
Begin Unit F CodeLab




Repeat with while 75 2 2 100 51214 51215 list-traversal (part)
Repeat with while 75 2 2 102 51291 51292 loops and lists (part)
Repeat with while 75 1 1 103 51012 loops and lists (combos)
Cancel with break 75




Skip Ahead with continue 76




Check break Use with else 76




Iterate with for 77 13 13 116 51176 51177 51259 51250 51286 51268 51269 51929 51930 51931 51932 51933 51934 for and range
Iterate with for 77 2 2 118 51946 51945 nested loops
Begin Unit G CodeLab




Iterate with for 77 11 11 129 51270 51271 51272 51273 51274 51279 51006 51002 51003 51004 51005 loops and strings
Iterate with for 77 4 4 133 51210 51216 51219 51263 list-traversal (part)
Iterate with for 77 1 1 134 51280 list indexing (move 51280 later with loops)
Iterate with for 77 4 4 138 51262 51264 51266 51267
Iterate with for 77 3 3 141 51290 51293 51297 loops and lists
Begin Unit H CodeLab




Iterate with for 77 3 3 144 51298 51301 51305 set iteration
Iterate with for 77 1 1 145 51302 dictionary adding/removal
Iterate with for 77 12 12 157 51828 51829 51830 51831 51826 51300 51265 51281 51299 51827 51282 51832 dictionary traversal
Iterate with for 77 2 2 159 51303 51304
Begin Unit I CodeLab




Cancel with break 78




input function small mention 79




Skip with continue 78




Check break Use with else 78




Iterate Multiple Sequences with zip() 79




Generate Number Sequences with range() 80 2 2 161 51257 51258 list-creation from range
Other Iterators 81




Comprehensions 81




List Comprehensions 81




Dictionary Comprehensions 83 2 2 163 51260 51261
Set Comprehensions 84




Generator Comprehensions 84




Functions 85 11 9 172 51074 51156 51073 51075 51076 51077 51143 51144 51146 51145 51147 Invoking functions
Functions 85 2 2 174 51148 51149 composition
Functions 85 2 2 176 51155 51016 function definition
Functions 85 3 3 179 51105 51158 51218 functions and if statements
Begin Unit J CodeLab




Functions 85 4 4 183 51162 51008 51009 51011 function and loops
Positional Arguments 89




Keyword Arguments 89




Specify Default Parameter Values 90




Gather Positional Arguments with * 91




Gather Keyword Arguments with ** 92




Docstrings 92




Functions Are First-Class Citizens 93




Inner Functions 95




Closures 95




Anonymous Functions: the lambda() Function 96




Generators 98




Decorators 99




Namespaces and Scope 101




Uses of _ and __ in Names 103




Handle Errors with try and except 103




Make Your Own Exceptions 105




Things to Do 106
















5. Py Boxes: Modules, Packages,
and Programs 109
Standalone Programs 109




Command-Line Arguments 110




Modules and the import Statement 110




Import a Module 110




Import a Module with Another Name 112




Import Only What You Want from a Module 112




Module Search Path 113




Packages 113




The Python Standard Library 114




Handle Missing Keys with setdefault() and defaultdict() 114




Count Items with Counter() 116




Order by Key with OrderedDict() 118




Stack + Queue == deque 118




Iterate over Code Structures with itertools 119




Print Nicely with pprint() 120




More Batteries: Get Other Python Code 121




Things to Do 121
















6. Oh Oh: Objects and Classes 123




What Are Objects? 123




Define a Class with class 124 6 5 188 51225 51226 51227 51244 51245 51227
Inheritance 126




Override a Method 127




Add a Method 128




Get Help from Your Parent with super 129




In self Defense 130




Get and Set Attribute Values with Properties 131




Name Mangling for Privacy 133




Method Types 134




Duck Typing 135




Special Methods 137




Composition 140




When to Use Classes and Objects versus Modules 141




Named Tuples 141




Things to Do 143
















7. Mangle Data Like a Pro 145




Text Strings 145




Unicode 145




Format 152




Match with Regular Expressions 156




Binary Data 163




bytes and bytearray 164




Convert Binary Data with struct 165




Other Binary Data Tools 168




Convert Bytes/Strings with binascii() 169




Bit Operators 169




Things to Do 170
















8. Data Has to Go Somewhere 173




File Input/Output 173




Write a Text File with write() 174




Read a Text File with read(), readline(), or readlines() 176




Write a Binary File with write() 177




Read a Binary File with read() 178




Close Files Automatically by Using with 178




Change Position with seek() 179




Structured Text Files 181




CSV 181




XML 183




HTML 185




JSON 185




YAML 188




A Security Note 189




Configuration Files 190




Other Interchange Formats 191




Serialize by Using pickle 191




Structured Binary Files 192




Spreadsheets 192




HDF5 192




Relational Databases 193




SQL 194




DB-API 195




SQLite 196




MySQL 198




PostgreSQL 198




SQLAlchemy 198




NoSQL Data Stores 204




The dbm Family 204




Memcached 205




Redis 206




Other NoSQL 214




Full-Text Databases 214




Things to Do 215
















9. The Web, Untangled 217




Web Clients 218




Test with telnet 219




Python’s Standard Web Libraries 220




Beyond the Standard Library: Requests 222




Web Servers 223




The Simplest Python Web Server 223




Web Server Gateway Interface 224




Frameworks 225




Bottle 226




Flask 228




Non-Python Web Servers 232




Other Frameworks 234




Web Services and Automation 236




The webbrowser Module 236




Web APIs and Representational State Transfer 236




JSON 237




Crawl and Scrape 237




Scrape HTML with BeautifulSoup 238




Things to Do 239
















10. Systems 241




Files 24




Create with open() 241




Check Existence with exists() 242




Check Type with isfile() 242




Copy with copy() 243




Change Name with rename() 243




Link with link() or symlink() 243




Change Permissions with chmod() 243




Change Ownership with chown() 244




Get a Pathname with abspath() 244




Get a symlink Pathname with realpath() 244




Delete a File with remove() 244




Directories 244




Create with mkdir() 245




Delete with rmdir() 245




List Contents with listdir() 245




Change Current Directory with chdir() 246




List Matching Files with glob() 246




Programs and Processes 247




Create a Process with subprocess 247




Create a Process with multiprocessing 249




Kill a Process with terminate() 249




Calendars and Clocks 250




The datetime Module 251




Using the time Module 254




Read and Write Dates and Times 255




Alternative Modules 258




Things to Do 259
















11. Concurrency and Networks 261




Concurrency 262




Queues 263




Processes 264




Threads 265




Green Threads and gevent 267




twisted 270




asyncio 271




Redis 271




Beyond Queues 275




Networks 276




Patterns 276




The Publish-Subscribe Model 276




TCP/IP 28




Sockets 281




ZeroMQ 285




Scapy 289




Internet Services 290




Web Services and APIs 291




Remote Processing 292




Big Fat Data and MapReduce 297




Working in the Clouds 298




Things to Do 302
















12. Be a Pythonista 303




About Programming 303




Find Python Code 304




Install Packages 304




Use pip 305




Use a Package Manager 305




Install from Source 306




Integrated Development Environments 306




IDLE 306




PyCharm 306




IPython 307




Name and Document 307




Testing Your Code 309




Check with pylint, pyflakes, and pep8 309




Test with unittest 311




Test with doctest 315




Test with nose 316




Other Test Frameworks 317




Continuous Integration 317




Debugging Python Code 317




Debug with pdb 318




Logging Error Messages 324




Optimize Your Code 326




Measure Timing 327




Algorithms and Data Structures 328




Cython, NumPy, and C Extensions 329




PyPy 330




Source Control 330




Mercurial 330




Git 330




Clone This Book 333




How You Can Learn More 333




Books 334




Websites 334




Groups 335




Conferences 335




Coming Attractions 335
















A. Py Art 337
















B. Py at Work 351
















C. Py Sci 365




Math and Stastics in the Standard Library




Math Functions




Working with Complex Numbers




Calculating Accurate Floating Point with decimal




Peform Rational Arithmetic with fractions




Use Packed Sequences with array




Handle Simple Stats bu Using statistics




Matrix Multiplication




Scientific Python




NumPy




Make an Array with array()




Make an Array with arange()




Make an Array with zeros(), ones(), or random()




Change an Array’s Shape with reshape()




Get an Element with []




Array Math




Linear Algebra




The SiPy Library




The SiKit Library




The IPython Library




A Better Interpreter




IPython Notebook




Python and Scientific Areas
















D. Install Python 3 385




Install Standard Python




Mac OS X




Windows




Linux or Unix




Install Anaconda




Install and Use ip and virtualenv




Install and use Conda
















E. Answers to Exercises 395
















F. Cheat Sheets 429




Operator Preedence




String Methods 12 12 200 51885 51865 51870 51856 51857 51858 51859 51860 51861 51862 51863 51864 String Coparisons and Logical Operations (mostly better solved with string methods)
Begin week 11 CodeLab




String Methods 5 5 205 51757 51758 51759 51766 51767 More Methods
String Methods 4 4 209 51768 51769 51770 51773 More Methods
String Module Attributes




Index 433




No Reading assignment




Not found in book




try finally block




No switch in Python 3 0 209 51909 51894 51895 switch
Conditional Expressions 2 2 211 51867 51866 Conditional Expressions











NOTE: omit Old Formatting





NOTE: omit Additional Exercises





Net:
Required total

211