An introduction to Python through practical examples Fletcher Heisler Copyright © 2012 RealPython.com “Python” and the original Python logo are registered trademarks of the Python Software Foundation, modified and used by RealPython.com with permission from the Foundation. Real Python RealPython.com 0) Introduction......................................5 0.1) Why Python?...........................................................5 0.2) Why this book?........................................................6 0.3) How to use this book................................................7 0.4) License..................................................................8 1) Getting Started...................................9 1.1) Download Python.....................................................9 1.2) Open IDLE..............................................................9 1.3) Write a Python script..............................................10 1.4) Screw things up.....................................................12 1.5) Store a variable.....................................................14 Interlude: Leave yourself helpful notes......16 2) Fundamentals: Strings and Methods........18 2.1) Learn to speak in Python..........................................18 2.2) Mess around with your words....................................19 2.3) Use objects and methods.........................................23 Assignment 2.3: Pick apart your user's input.................................26 3) Fundamentals: Working with Strings.......27 3.1) Mix and match different objects................................27 3.2) Streamline your print statements...............................29 3.3) Find a string in a string............................................31 Assignment 3.3: Turn your user into a l33t h4x0r...........................33 4) Fundamentals: Functions and Loops.......34 4.1) Do futuristic arithmetic...........................................34 Assignment 4.1: Perform calculations on user input........................36 4.2) Create your own functions........................................37 Assignment 4.2: Convert temperatures........................................40 4.3) Run in circles........................................................40 Assignment 4.3: Track your investments.......................................44 2 Real Python RealPython.com Interlude: Debug your code.....................46 5) Fundamentals: Conditional logic............51 5.1) Compare values.....................................................51 5.2) Add some logic.......................................................53 5.3) Control the flow of your program...............................58 Assignment 5.3: Find the factors of a number................................61 5.4) Break out of the pattern..........................................62 5.5) Recover from errors................................................65 5.6) Simulate events and calculate probabilities..................68 Assignment 5.6.1: Simulate an election.......................................70 Assignment 5.6.2: Simulate a coin toss experiment.........................71 6) Fundamentals: Lists and Dictionaries......72 6.1) Make and update lists..............................................72 Assignment 6.1: Wax poetic......................................................77 6.2) Make permanent lists..............................................78 6.3) Store relationships in dictionaries..............................80 7) File Input and Output.........................86 7.1) Read and write simple files.......................................86 7.2) Use more complicated folder structures.......................91 Assignment 7.2: Use pattern matching to delete files......................97 7.3) Read and write CSV data..........................................98 Assignment 7.3: Create a high scores list from CSV data.................102 Interlude: Install packages.....................103 8) Interact with PDF files......................107 8.1) Read and write PDFs..............................................107 8.2) Manipulate PDF files..............................................111 Assignment 8.2: Add a cover sheet to a PDF file...........................115 3 Real Python RealPython.com 9) SQL database connections..................116 9.1) Communicate with databases using SQLite..................116 9.2) Use other SQL variants...........................................122 10) Interacting with the web..................124 10.1) Scrape and parse text from websites........................124 10.2) Use an HTML parser to scrape websites....................131 10.3) Interact with HTML forms......................................135 10.4) Interact with websites in real-time..........................142 11) Scientific computing and graphing......145 11.1) Use NumPy for matrix manipulation.........................145 11.2) Use matplotlib for plotting graphs...........................151 Assignment 11.2: Plot a graph from CSV data...............................164 12) Graphical User Interfaces.................165 12.1) Add GUI elements with EasyGUI..............................165 Assignment 12.1: Use GUI elements to help a user modify files........172 12.2) Create GUI applications with Tkinter........................172 Assignment 12.2: Return of the poet.........................................187 13) Web applications............................188 13.1) Create a simple web application.............................188 13.2) Create an interactive web application......................196 Assignment 13.2: The poet gains a web presence.........................201 13.3) Put your web application online.............................202 Final Thoughts....................................204 Acknowledgements..............................205 4 Real Python RealPython.com 0) Introduction W hether you're new to programming or a professional code monkey looking to dive into a new language, this book will teach you all of the practical Python that you need to get started on projects on your own. Real Python emphasizes real-world programming techniques, which are illustrated through interesting, useful examples. No matter what your ultimate goals may be, if you work with computer at all, you will soon be finding endless ways to improve your life by automating tasks and solving problems through Python programs that you create. 0.1) Why Python? P ython is open-source freeware, meaning you can download it for free and use it for any purpose. It also has a great support community that has built a number of additional free tools. Need to work with PDF documents in Python? There's a free package for that. Want to collect data from webpages? No need to start from scratch! Python was built to be easier to use than other programming languages. It's usually much easier to read Python code and MUCH faster to write code in Python than in other languages. For instance, here's some simple code written in C, another commonly used programming language: #include <stdio.h> int main () { printf ("Hello, world\n"); } All the program does is print “Hello, world” on the screen. That was a lot of work to print one phrase! Here's the same code in Python: print "Hello, world" 5 Real Python RealPython.com Simple, right? Easy, faster, more readable. At the same time, Python has all the functionality of other languages and more. You might be surprised how many professional products are built on Python code: Gmail, Google Maps, YouTube, reddit, Spotify, turntable.fm, Yahoo! Groups, and the list goes on… And if it's powerful enough for both NASA and the NSA, it's good enough for us. 0.2) Why this book? T here are tons of books and tutorials out there for learning Python already. However, most of the resources out there generally have two main problems: 1) They aren't practical. 2) They aren't interesting. Most books are so preoccupied with covering every last possible variation of every command that it's easy to get lost in the details. In the end, most of them end up looking more like the Python documentation pages. This is great as reference material, but it's a horrible way to learn a programming language. Not only do you spend most of your time learning things you'll never use, but it isn't any fun! This book is built on the 80/20 principle. We will cover the commands and techniques used in the vast majority of cases and focus on how to program real-world solutions to problems that ordinary people actually want to solve. This way, I guarantee that you will: • Learn useful techniques much faster • Spend less time struggling with unimportant complications • Find more practical uses for Python in your own life • Have more fun in the process! If you want to become a serious, professional Python programmer, this book won't be enough by itself – but it will still be the best starting point. Once you've mastered the material in this book, you will have gained a strong enough foundation that venturing 6 Real Python RealPython.com out into more advanced territory on your own will be a breeze. So dive in! Learn to program in a widely used, free language that can do more than you ever thought was possible. 0.3) How to use this book F or the most part, you should approach the topics in the first half of this book in the same order as they are presented. This is less true of the second half, which covers a number of mostly non-overlapping topics, although the chapters are generally increasing in difficulty throughout. If you are a more experienced programmer, then you may find yourself heading toward the back of the book right away – but don't neglect getting a strong foundation in the basics first! Each chapter section is followed by review exercises to help you make sure that you've mastered all the topics covered. There are also a number of assignments, which are more involved and usually require you to tie together a number of different concepts from previous chapters. The practice files that accompany this course also include solution scripts to the assignments as well as some of the trickier exercises – but to get the most out of them, you should try your best to solve the assignment problems on your own before looking at the example solutions. If you get stuck, you can always log in at RealPython.com and ask for help on the members' forum; it's likely that someone else has already experienced the same difficulty that you're encountering and might be able to guide you along. This book does move quickly, however, so if you're completely new to programming, you may want to supplement the first few chapters with additional practice. I highly recommend working through the beginning Python lessons available for free at the Codecademy site while you make your way through the beginning of this material as the best way to make sure that you have all the basics down. Finally, if you have any questions or feedback about the course, you're always welcome to contact me directly. 7 Real Python RealPython.com 0.4) License T his e-book is copyrighted and licensed under a Creative Commons Attribution- NonCommercial-NoDerivs 3.0 Unported License. This means that you are welcome to share this book and use it for any non-commercial purposes so long as the entire book remains intact and unaltered. That being said, if you have received this copy for free and have found it helpful, I would very much appreciate if you purchased a copy of your own. The example Python scripts associated with this book should be considered open content. This means that anyone is welcome to use any portion of the code for any purpose. 8 Real Python RealPython.com 1) Getting Started 1.1) Download Python B efore we can do anything, you have to download Python. Even if you already have Python on your computer, make sure that you have the correct version: 2.7.3 is the version used in this book and by most of the rest of the world. ! There's a newer version, Python 3.3, but it can't run code that was created with previous versions of Python (including a lot of useful and important packages that haven't been updated). As a result, Python 3 still hasn't caught on yet. Since most of the code you'll see elsewhere will be from Python 2.7, you should learn that first. The two versions are still very similar, and it will take you very little time to get used to the minor changes in Python 3 after you've mastered Python 2. Mac users: You already have a version of Python installed by default, but it's not quite the same as the standard installation. You should still download Python 2.7.3 as directed below. Otherwise, you might run into problems later when trying to install some additional functionality in Python or running code that involves graphics windows. Linux users: You might already have Python 2.7.3 installed by default. Open your Terminal application and type “python --version” to find out. If you have 2.7.1 or 2.7.2, you should go ahead and update to the latest version. If you need to, go to http://www.python.org/download/ to download Python 2.7.3 for your operating system and install the program. 1.2) Open IDLE W e'll be using IDLE (Interactive DeveLopment Environment) to write our Python code. IDLE is a simple editing program that comes automatically installed with 9 Real Python RealPython.com Python on Windows and Mac, and it will make our lives much easier while we're coding. You could write Python scripts in any program from a basic text editor to a very complex development environment (and many professional coders use more advanced setups), but IDLE is simple to use and will easily provide all the functionality we need. Windows: Go to your start menu and click on “IDLE (Python GUI)” from the “Python 2.7” program folder to open IDLE. You can also type “IDLE” into the search bar. OS X: Go to your Applications folder and click on “IDLE” from the “Python 2.7” folder to start running IDLE. Alternatively, you can type “IDLE” (without quotes) into your Terminal window to launch IDLE. Linux: I recommend that you install IDLE to follow along with this course. You could use Vim or Emacs, but they will not have the same built-in debugging features. To install IDLE with admin privileges: On Ubuntu/Debian, type: sudo apt-get install idle On Fedora/Red Hat/RHEL/CentOS, type: sudo yum install python-tools On SUSE, you can search for IDLE via “install software” through YaST. Opening IDLE, you will see a brief description of Python, followed by a prompt: >>> We're ready to program! 1.3) Write a Python script T he window we have open at the moment is IDLE's interactive window; usually this window will just show us results when we run programs that we've written, but we can also enter Python code into this window directly. Go ahead and try typing some basic math into the interactive window at the prompt – when you hit enter, it should evaluate your calculation, display the result and prompt you for more input: >>> 1+1 2 >>> 10
Description: