Python For Informatics - Dr. Chuck

3y ago
54 Views
7 Downloads
1.47 MB
244 Pages
Last View : 2m ago
Last Download : 3m ago
Upload by : Mara Blakely
Transcription

Python for InformaticsExploring InformationVersion 2.7.3Charles Severance

Copyright 2009- Charles Severance.Printing history:May 2015: Editorial pass thanks to Sue Blumenberg.October 2013: Major revision to Chapters 13 and 14 to switch to JSON and use OAuth.Added new chapter on Visualization.September 2013: Published book on Amazon CreateSpaceJanuary 2010: Published book using the University of Michigan Espresso Book machine.December 2009: Major revision to chapters 2-10 from Think Python: How to Think Likea Computer Scientist and writing chapters 1 and 11-15 to produce Python for Informatics: Exploring InformationJune 2008: Major revision, changed title to Think Python: How to Think Like a Computer Scientist.August 2007: Major revision, changed title to How to Think Like a (Python) Programmer.April 2002: First edition of How to Think Like a Computer Scientist.This work is licensed under a Creative Common Attribution-NonCommercial-ShareAlike3.0 Unported License. This license is available at creativecommons.org/licenses/by-nc-sa/3.0/. You can see what the author considers commercial and non-commercialuses of this material as well as license exemptions in the Appendix titled Copyright Detail.The LATEX source for the Think Python: How to Think Like a Computer Scientist versionof this book is available from http://www.thinkpython.com.

PrefacePython for Informatics: Remixing an Open BookIt is quite natural for academics who are continuously told to “publish or perish”to want to always create something from scratch that is their own fresh creation.This book is an experiment in not starting from scratch, but instead “remixing”the book titled Think Python: How to Think Like a Computer Scientist written byAllen B. Downey, Jeff Elkner, and others.In December of 2009, I was preparing to teach SI502 - Networked Programmingat the University of Michigan for the fifth semester in a row and decided it was timeto write a Python textbook that focused on exploring data instead of understandingalgorithms and abstractions. My goal in SI502 is to teach people lifelong datahandling skills using Python. Few of my students were planning to be professionalcomputer programmers. Instead, they planned to be librarians, managers, lawyers,biologists, economists, etc., who happened to want to skillfully use technology intheir chosen field.I never seemed to find the perfect data-oriented Python book for my course, so Iset out to write just such a book. Luckily at a faculty meeting three weeks beforeI was about to start my new book from scratch over the holiday break, Dr. AtulPrakash showed me the Think Python book which he had used to teach his Pythoncourse that semester. It is a well-written Computer Science text with a focus onshort, direct explanations and ease of learning.The overall book structure has been changed to get to doing data analysis problemsas quickly as possible and have a series of running examples and exercises aboutdata analysis from the very beginning.Chapters 2–10 are similar to the Think Python book, but there have been majorchanges. Number-oriented examples and exercises have been replaced with dataoriented exercises. Topics are presented in the order needed to build increasinglysophisticated data analysis solutions. Some topics like try and except are pulledforward and presented as part of the chapter on conditionals. Functions are givenvery light treatment until they are needed to handle program complexity ratherthan introduced as an early lesson in abstraction. Nearly all user-defined functions

ivChapter 0. Prefacehave been removed from the example code and exercises outside of Chapter 4.The word “recursion”1 does not appear in the book at all.In chapters 1 and 11–16, all of the material is brand new, focusing on real-worlduses and simple examples of Python for data analysis including regular expressions for searching and parsing, automating tasks on your computer, retrievingdata across the network, scraping web pages for data, using web services, parsingXML and JSON data, and creating and using databases using Structured QueryLanguage.The ultimate goal of all of these changes is a shift from a Computer Science to anInformatics focus is to only include topics into a first technology class that can beuseful even if one chooses not to become a professional programmer.Students who find this book interesting and want to further explore should lookat Allen B. Downey’s Think Python book. Because there is a lot of overlap between the two books, students will quickly pick up skills in the additional areas oftechnical programming and algorithmic thinking that are covered in Think Python.And given that the books have a similar writing style, they should be able to movequickly through Think Python with a minimum of effort.As the copyright holder of Think Python, Allen has given me permission to changethe book’s license on the material from his book that remains in this book from theGNU Free Documentation License to the more recent Creative Commons Attribution — Share Alike license. This follows a general shift in open documentationlicenses moving from the GFDL to the CC-BY-SA (e.g., Wikipedia). Using theCC-BY-SA license maintains the book’s strong copyleft tradition while making iteven more straightforward for new authors to reuse this material as they see fit.I feel that this book serves an example of why open materials are so importantto the future of education, and want to thank Allen B. Downey and CambridgeUniversity Press for their forward-looking decision to make the book availableunder an open copyright. I hope they are pleased with the results of my efforts andI hope that you the reader are pleased with our collective efforts.I would like to thank Allen B. Downey and Lauren Cowles for their help, patience,and guidance in dealing with and resolving the copyright issues around this book.Charles Severancewww.dr-chuck.comAnn Arbor, MI, USASeptember 9, 2013Charles Severance is a Clinical Associate Professor at the University of MichiganSchool of Information.1 Except,of course, for this line.

ContentsPrefaceiii1 Why should you learn to write programs?11.1Creativity and motivation . . . . . . . . . . . . . . . . . . . . .21.2Computer hardware architecture . . . . . . . . . . . . . . . . .31.3Understanding programming . . . . . . . . . . . . . . . . . . .41.4Words and sentences . . . . . . . . . . . . . . . . . . . . . . .51.5Conversing with Python . . . . . . . . . . . . . . . . . . . . . .61.6Terminology: interpreter and compiler . . . . . . . . . . . . . .81.7Writing a program . . . . . . . . . . . . . . . . . . . . . . . . .101.8What is a program? . . . . . . . . . . . . . . . . . . . . . . . .111.9The building blocks of programs . . . . . . . . . . . . . . . . .121.10What could possibly go wrong? . . . . . . . . . . . . . . . . . .131.11The learning journey . . . . . . . . . . . . . . . . . . . . . . .141.12Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151.13Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162 Variables, expressions, and statements192.1Values and types . . . . . . . . . . . . . . . . . . . . . . . . . .192.2Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202.3Variable names and keywords . . . . . . . . . . . . . . . . . . .212.4Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21

viContents2.5Operators and operands . . . . . . . . . . . . . . . . . . . . . .222.6Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . .232.7Order of operations . . . . . . . . . . . . . . . . . . . . . . . .232.8Modulus operator . . . . . . . . . . . . . . . . . . . . . . . . .242.9String operations . . . . . . . . . . . . . . . . . . . . . . . . .242.10Asking the user for input . . . . . . . . . . . . . . . . . . . . .242.11Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252.12Choosing mnemonic variable names . . . . . . . . . . . . . . .262.13Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . .282.14Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .282.15Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Conditional execution313.1Boolean expressions . . . . . . . . . . . . . . . . . . . . . . . .313.2Logical operators . . . . . . . . . . . . . . . . . . . . . . . . .323.3Conditional execution . . . . . . . . . . . . . . . . . . . . . . .323.4Alternative execution . . . . . . . . . . . . . . . . . . . . . . .333.5Chained conditionals . . . . . . . . . . . . . . . . . . . . . . .343.6Nested conditionals . . . . . . . . . . . . . . . . . . . . . . . .353.7Catching exceptions using try and except . . . . . . . . . . . . .363.8Short-circuit evaluation of logical expressions . . . . . . . . . .373.9Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . .383.10Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .393.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .404 Functions434.1Function calls . . . . . . . . . . . . . . . . . . . . . . . . . . .434.2Built-in functions . . . . . . . . . . . . . . . . . . . . . . . . .434.3Type conversion functions . . . . . . . . . . . . . . . . . . . .444.4Random numbers . . . . . . . . . . . . . . . . . . . . . . . . .45

Contentsvii4.5Math functions . . . . . . . . . . . . . . . . . . . . . . . . . .464.6Adding new functions . . . . . . . . . . . . . . . . . . . . . . .474.7Definitions and uses . . . . . . . . . . . . . . . . . . . . . . . .484.8Flow of execution . . . . . . . . . . . . . . . . . . . . . . . . .494.9Parameters and arguments . . . . . . . . . . . . . . . . . . . .494.10Fruitful functions and void functions . . . . . . . . . . . . . . .504.11Why functions? . . . . . . . . . . . . . . . . . . . . . . . . . .524.12Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . .524.13Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .534.14Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .545 Iteration575.1Updating variables . . . . . . . . . . . . . . . . . . . . . . . .575.2The while statement . . . . . . . . . . . . . . . . . . . . . . .575.3Infinite loops . . . . . . . . . . . . . . . . . . . . . . . . . . .585.4“Infinite loops” and break . . . . . . . . . . . . . . . . . . . .585.5Finishing iterations with continue . . . . . . . . . . . . . . . .595.6Definite loops using for . . . . . . . . . . . . . . . . . . . . .605.7Loop patterns . . . . . . . . . . . . . . . . . . . . . . . . . . .615.8Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . .645.9Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .645.10Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .656 Strings676.1A string is a sequence . . . . . . . . . . . . . . . . . . . . . . .676.2Getting the length of a string using len . . . . . . . . . . . . . .686.3Traversal through a string with a loop . . . . . . . . . . . . . .686.4String slices . . . . . . . . . . . . . . . . . . . . . . . . . . . .696.5Strings are immutable . . . . . . . . . . . . . . . . . . . . . . .696.6Looping and counting . . . . . . . . . . . . . . . . . . . . . . .70

viiiContents6.7The in operator . . . . . . . . . . . . . . . . . . . . . . . . . .706.8String comparison . . . . . . . . . . . . . . . . . . . . . . . . .706.9string methods . . . . . . . . . . . . . . . . . . . . . . . . . .716.10Parsing strings . . . . . . . . . . . . . . . . . . . . . . . . . . .736.11Format operator . . . . . . . . . . . . . . . . . . . . . . . . . .746.12Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . .756.13Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .766.14Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .777 Files797.1Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . .797.2Opening files . . . . . . . . . . . . . . . . . . . . . . . . . . .807.3Text files and lines . . . . . . . . . . . . . . . . . . . . . . . . .817.4Reading files . . . . . . . . . . . . . . . . . . . . . . . . . . .827.5Searching through a file . . . . . . . . . . . . . . . . . . . . . .837.6Letting the user choose the file name . . . . . . . . . . . . . . .857.7Using try, except, and open . . . . . . . . . . . . . . . . . .857.8Writing files . . . . . . . . . . . . . . . . . . . . . . . . . . . .877.9Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . .877.10Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .887.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .888 Lists918.1A list is a sequence . . . . . . . . . . . . . . . . . . . . . . . .918.2Lists are mutable . . . . . . . . . . . . . . . . . . . . . . . . .918.3Traversing a list . . . . . . . . . . . . . . . . . . . . . . . . . .928.4List operations . . . . . . . . . . . . . . . . . . . . . . . . . . .938.5List slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . .938.6List methods . . . . . . . . . . . . . . . . . . . . . . . . . . . .948.7Deleting elements . . . . . . . . . . . . . . . . . . . . . . . . .94

Contentsix8.8Lists and functions . . . . . . . . . . . . . . . . . . . . . . . .958.9Lists and strings . . . . . . . . . . . . . . . . . . . . . . . . . .968.10Parsing lines . . . . . . . . . . . . . . . . . . . . . . . . . . . .978.11Objects and values . . . . . . . . . . . . . . . . . . . . . . . .988.12Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .998.13List arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 1008.14Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018.15Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048.16Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059 Dictionaries1079.1Dictionary as a set of counters . . . . . . . . . . . . . . . . . . 1099.2Dictionaries and files . . . . . . . . . . . . . . . . . . . . . . . 1109.3Looping and dictionaries . . . . . . . . . . . . . . . . . . . . . 1119.4Advanced text parsing . . . . . . . . . . . . . . . . . . . . . . . 1129.5Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1149.6Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1159.7Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11510 Tuples11710.1Tuples are immutable . . . . . . . . . . . . . . . . . . . . . . . 11710.2Comparing tuples . . . . . . . . . . . . . . . . . . . . . . . . . 11810.3Tuple assignment . . . . . . . . . . . . . . . . . . . . . . . . . 11910.4Dictionaries and tuples . . . . . . . . . . . . . . . . . . . . . . 12110.5Multiple assignment with dictionaries . . . . . . . . . . . . . . 12110.6The most common words . . . . . . . . . . . . . . . . . . . . . 12210.7Using tuples as keys in dictionaries . . . . . . . . . . . . . . . . 12410.8Sequences: strings, lists, and tuples—Oh My! . . . . . . . . . . 12410.9Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12510.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12610.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

xContents11 Regular expressions12911.1Character matching in regular expressions . . . . . . . . . . . . 13011.2Extracting data using regular expressions . . . . . . . . . . . . . 13111.3Combining searching and extracting . . . . . . . . . . . . . . . 13311.4Escape character . . . . . . . . . . . . . . . . . . . . . . . . . . 13711.5Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13711.6Bonus section for Unix users . . . . . . . . . . . . . . . . . . . 13811.7Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13911.8Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14011.9Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14012 Networked programs14312.1HyperText Transport Protocol - HTTP . . . . . . . . . . . . . . 14312.2The World’s Simplest Web Browser . . . . . . . . . . . . . . . 14412.3Retrieving an image over HTTP . . . . . . . . . . . . . . . . . 14512.4Retrieving web pages with urllib . . . . . . . . . . . . . . . . 14812.5Parsing HTML and scraping the web . . . . . . . . . . . . . . . 14812.6Parsing HTML using regular expressions . . . . . . . . . . . . . 14912.7Parsing HTML using BeautifulSoup . . . . . . . . . . . . . . . 15012.8Reading binary files using urllib . . . . . . . . . . . . . . . . . 15212.9Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15312.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15313 Using Web Services15513.1eXtensible Markup Language - XML . . . . . . . . . . . . . . . 15513.2Parsing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . 15613.3Looping through nodes . . . . . . . . . . . . . . . . . . . . . . 15613.4JavaScript Object Notation - JSON . . . . . . . . . . . . . . . . 15713.5Parsing JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . 15813.6Application Programming Interfaces . . . . . . . . . . . . . . . 159

Contentsxi13.7Google geocoding web service . . . . . . . . . . . . . . . . . . 16013.8Security and API usage . . . . . . . . . . . . . . . . . . . . . . 16213.9Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16613.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16714 Using databases and Structured Query Language (SQL)16914.1What is a database? . . . . . . . . . . . . . . . . . . . . . . . . 16914.2Database concepts . . . . . . . . . . . . . . . . . . . . . . . . . 17014.3SQLite manager Firefox add-on . . . . . . . . . . . . . . . . . 17014.4Creating a database table . . . . . . . . . . . . . . . . . . . . . 17014.5Structured Query Language summary . . . . . . . . . . . . . . 17314.6Spidering Twitter using a database . . . . . . . . . . . . . . . . 17514.7Basic data modeling . . . . . . . . . . . . . . . . . . . . . . . . 18014.8Programming with multiple tables . . . . . . . . . . . . . . . . 18214.9Three kinds of keys . . . . . . . . . . . . . . . . . . . . . . . . 18614.10 Using JOIN to retrieve data . . . . . . . . . . . . . . . . . . . . 18714.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18914.12 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18914.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19015 Visualizing data19315.1Building a Google map from geocoded data . . . . . . . . . . . 19315.2Visualizing networks and interconnections . . . . . . . . . . . . 19515.3Visualizing mail data . . . . . . . . . . . . . . . . . . . . . . . 19816 Automating common tasks on your computer20316.1File names and paths . . . . . . . . . . . . . . . . . . . . . . . 20316.2Example: Cleaning up a photo directory . . . . . . . . . . . . . 20416.3Command-line arguments . . . . . . . . . . . . . . . . . . . . . 20916.4Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21016.5Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21116.6Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

xiiContentsA Python Programming on Windows215B Python Programming on Macintosh217C Contributions219D Copyright Detail223

Chapter 1Why should you learn to writeprograms?Writing programs (or programming) is a very creative and rewarding activity. Youcan write programs for many reasons, ranging from making your living to solvinga difficult data analysis problem to having fun to helping someone else solve aproblem. This book assumes that everyone needs to know how to program, andthat once you know how to program you will figure out what you want to do withyour newfound skills.We are surrounded in our daily lives with computers ranging from laptops to cellphones. We can think of these computers as our “personal assistants” who can takecare of many thing

Preface Python for Informatics: Remixing an Open Book It is quite natural for academics who are continuously told to “publish or perish” to want to always create something from scratch that is their own fresh creation.

Related Documents:

1.5 Definition of Health Informatics Notes: Within Informatics there are several different levels; including Translational Informatics, Research Informatics, Legal Informatics, and Health Informatics. It is the latter that we are concerned with. While there are several different definitions of Health Informatics, the National Library of Medicine

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Python is readable 5 Python is complete—"batteries included" 6 Python is cross-platform 6 Python is free 6 1.3 What Python doesn't do as well 7 Python is not the fastest language 7 Python doesn't have the most libraries 8 Python doesn't check variable types at compile time 8 1.4 Why learn Python 3? 8 1.5 Summary 9

Bruksanvisning för bilstereo . Bruksanvisning for bilstereo . Instrukcja obsługi samochodowego odtwarzacza stereo . Operating Instructions for Car Stereo . 610-104 . SV . Bruksanvisning i original

site "Python 2.x is legacy, Python 3.x is the present and future of the language". In addition, "Python 3 eliminates many quirks that can unnecessarily trip up beginning programmers". However, note that Python 2 is currently still rather widely used. Python 2 and 3 are about 90% similar. Hence if you learn Python 3, you will likely

There are currently two versions of Python in use; Python 2 and Python 3. Python 3 is not backward compatible with Python 2. A lot of the imported modules were only available in Python 2 for quite some time, leading to a slow adoption of Python 3. However, this not really an issue anymore. Support for Python 2 will end in 2020.

Installing or removing drill bit 1 011313 To install the bit, place it in the chuck as far as it will go. Tighten the chuck by hand. Place the chuck key in each of the three holes and tighten clockwise. Be sure to tighten all three chuck holes evenly. To remove the bit, turn the chuck key counterclockwise in just one hole, then loosen the chuck .