📄 Page
1
(This page has no text content)
📄 Page
2
Python Basics: A Practical Introductionto Python 3 Real Python
📄 Page
3
Python Basics: A Practical Introduction to Python 3 Revised and Updated 4th Edition David Amos, Dan Bader, Joanna Jablonski, Fletcher Heisler Copyright © Real Python (realpython.com), 2012–2020 For online information and ordering of this and other books by RealPython, please visit realpython.com. For more information, pleasecontact us at info@realpython.com. ISBN: 9781775093329 (paperback) ISBN: 9781775093336 (electronic) Cover design by Aldren Santos Additional editing and proofreading by Jacob Schmitt “Python” and the Python logos are trademarks or registered trade-marks of the Python Software Foundation, used by Real Python withpermission from the Foundation. Thank you for downloading this ebook. This ebook is licensed for yourpersonal enjoyment only. This ebookmay not be resold or given awayto other people. If you would like to share this book with another per-son, please purchase an additional copy for each recipient. If you’rereading this book anddid not purchase it, or if it was not purchased foryour use only, then please return to realpython.com/pybasics-bookand purchase your own copy. Thank you for respecting the hard workbehind this book. Updated 2020-10-05
📄 Page
4
What Pythonistas Say About Python Basics: A Practical In-troduction to Python 3 “I love [the book]! The wording is casual, easy to understand, andmakes the information ow well. I never feel lost in the material, andit’s not too dense so it’s easy for me to review older chapters over andover. I’ve looked at over 10 di erent Python tutorials/books/online courses,and I’ve probably learned the most from Real Python!” — ThomasWong “Three years later and I still return to my Real Python books when Ineed a quick refresher on usage of vital Python commands.” — Rob Fowler “I oundered for a long time trying to teach myself. I slogged throughdozens of incomplete online tutorials. I snoozed through hours of bor-ing screencasts. I gave up on countless crufty books from big-timepublishers. And then I found Real Python. The easy-to-follow, step-by-step instructions break the big conceptsdown into bite-sized chunks written in plain English. The authorsnever forget their audience andare consistently thoroughanddetailedin their explanations. I’m up and running now, but I constantly referto the material for guidance.” — Jared Nielsen
📄 Page
5
“I love the book because at the end of each particular lesson there arereal world and interesting challenges. I just built a savings estimatorthat actually re ects my savings account – neat!” —Drew Prescott “As a practice of what you taught I started building simple scripts forpeople on my team to help them in their everyday duties. When mymanagers noticed that, I was o ered a new position as a developer. I know there is heaps of things to learn and there will be huge chal-lenges, but I nally started doing what I really came to like. Once again: MANY THANKS!” —Kamil “What I found great about the Real Python courses compared to othersis how they explain things in the simplest way possible. A lot of courses, in any discipline really, require the learning of a lot ofjargon when in fact what is being taught could be taught quickly andsuccinctly without too much of it. The courses do a very good job ofkeeping the examples interesting.” — Stephen Grady “After reading the rst Real Python course Iwrote a script to automateamundane task at work. What used to takeme three to ve hours nowtakes less than ten minutes!” — Brandon Youngdale
📄 Page
6
“Honestly, throughout this whole process what I found was just melooking really hard for things that couldmaybe be added or improved,but this tutorial is amazing! You do a wonderful job of explaining andteaching Python in away that people likeme, a complete novice, couldreally grasp. The ow of the lessonsworks perfectly throughout. The exercises trulyhelped along the way and you feel very accomplished when you nishup the book. I think you have a gift for making Python seem moreattainable to people outside the programming world. This is something I never thought I would be doing or learning andwith a little push from you I am learning it and I can see that it will benothing but bene cial to me in the future!” — Shea Klusewicz “The authors of the courses have NOT forgotten what it is like to bea beginner – something that many authors do – and assume noth-ing about their readers, which makes the courses fantastic reads. Thecourses are also accompanied by some great videos as well as plentyof references for extra learning, homework assignments and examplecode that you can experiment with and extend. I really liked that there was always full code examples and each lineof code had good comments so you can see what is doing what. I now have a number of books on Python and the Real Python onesare the only ones I have actually nished cover to cover, and theyare hands down the best on the market. If like me, you’re not a pro-grammer (I work in online marketing) you’ll nd these courses to belike a mentor due to the clear, u -free explanations! Highly recom-mended!” — Craig Addyman
📄 Page
7
About the Authors AtReal Python you’ll learn real-world programming skills from a com-munity of professional Pythonistas from all around the world. The realpython.com website launched in 2012 and currently helpsmore than two million Python developers each month with freeprogramming tutorials and in-depth learning resources. Everyone whoworked on this book is a practitionerwith several yearsof professional experience in the software industry. Here are themem-bers of the Real Python tutorial team who worked on Python Basics: DavidAmos is the content technical lead forReal Python. After leav-ing academia in 2015, David worked in various technical positions asa programmer and data scientist. In 2019, David joined Real Pythonfull time to pursue his passion for education. He lead the charge onrewriting and updating the Python Basics curriculum to Python 3. Dan Bader is the owner and editor in chief of Real Python and themain developer of the realpython.com learning platform. Dan hasbeen writing code for more than twenty years and holds a master’sdegree in computer science. He’s the author of Python Tricks, a best-selling programming book for intermediate Python developers. Joanna Jablonski is the executive editor of Real Python. She likesnatural languages just as much as she likes programming languages.Her love for puzzles, patterns, and pesky little details led her to followa career in translation. It was only a matter of time before she wouldfall in love with a new language: Python! She joined Real Python in2018 and has been helping Pythonistas level up ever since. Fletcher Heisler is the founder of Hunter2, where he teaches de-velopers how to hack and secure modern web apps. As one of thefounding members of Real Python, Fletcher wrote the first version ofthe Python curriculum this book is based on in 2012.
📄 Page
8
Contents Contents 7 Foreword 12 1 Introduction 191.1 Why This Book? . . . . . . . . . . . . . . . . . . . . 201.2 About Real Python . . . . . . . . . . . . . . . . . . 221.3 How to Use This Book . . . . . . . . . . . . . . . . . 231.4 Bonus Material and Learning Resources . . . . . . . 24 2 Setting Up Python 282.1 A Note on Python Versions . . . . . . . . . . . . . . 292.2 Windows . . . . . . . . . . . . . . . . . . . . . . . 302.3 macOS . . . . . . . . . . . . . . . . . . . . . . . . . 332.4 Ubuntu Linux . . . . . . . . . . . . . . . . . . . . . 36 3 Your First Python Program 413.1 Write a Python Program . . . . . . . . . . . . . . . 423.2 Mess Things Up . . . . . . . . . . . . . . . . . . . . 463.3 Create a Variable . . . . . . . . . . . . . . . . . . . 493.4 Inspect Values in the Interactive Window . . . . . . 543.5 Leave Yourself Helpful Notes . . . . . . . . . . . . . 573.6 Summary and Additional Resources . . . . . . . . . 59 4 Strings and String Methods 614.1 What Is a String? . . . . . . . . . . . . . . . . . . . 624.2 Concatenation, Indexing, and Slicing . . . . . . . . . 68 7
📄 Page
9
Contents 4.3 Manipulate Strings With Methods . . . . . . . . . . 784.4 Interact With User Input . . . . . . . . . . . . . . . 844.5 Challenge: Pick Apart Your User’s Input . . . . . . . 874.6 Working With Strings and Numbers . . . . . . . . . 874.7 Streamline Your Print Statements . . . . . . . . . . 934.8 Find a String in a String . . . . . . . . . . . . . . . . 954.9 Challenge: Turn Your User Into a L33t H4x0r . . . . 984.10 Summary and Additional Resources . . . . . . . . . 99 5 Numbers and Math 1015.1 Integers and Floating-Point Numbers . . . . . . . . 1025.2 Arithmetic Operators and Expressions . . . . . . . . 1065.3 Challenge: Perform Calculations on User Input . . . 1145.4 Make Python Lie to You . . . . . . . . . . . . . . . . 1155.5 Math Functions and Number Methods . . . . . . . . 1175.6 Print Numbers in Style . . . . . . . . . . . . . . . . 1225.7 Complex Numbers . . . . . . . . . . . . . . . . . . 1255.8 Summary and Additional Resources . . . . . . . . . 129 6 Functions and Loops 1316.1 What Is a Function, Really? . . . . . . . . . . . . . . 1326.2 Write Your Own Functions . . . . . . . . . . . . . . 1366.3 Challenge: Convert Temperatures . . . . . . . . . . 1456.4 Run in Circles . . . . . . . . . . . . . . . . . . . . . 1466.5 Challenge: Track Your Investments . . . . . . . . . . 1556.6 Understand Scope in Python . . . . . . . . . . . . . 1566.7 Summary and Additional Resources . . . . . . . . . 161 7 Finding and Fixing Code Bugs 1637.1 Use the Debug Control Window . . . . . . . . . . . 1647.2 Squash Some Bugs . . . . . . . . . . . . . . . . . . 1707.3 Summary and Additional Resources . . . . . . . . . 178 8 Conditional Logic and Control Flow 1808.1 Compare Values . . . . . . . . . . . . . . . . . . . . 1818.2 Add Some Logic . . . . . . . . . . . . . . . . . . . . 1858.3 Control the Flow of Your Program . . . . . . . . . . 193 8
📄 Page
10
Contents 8.4 Challenge: Find the Factors of a Number . . . . . . . 2058.5 Break Out of the Pattern . . . . . . . . . . . . . . . 2068.6 Recover From Errors . . . . . . . . . . . . . . . . . 2108.7 Simulate Events and Calculate Probabilities . . . . . 2168.8 Challenge: Simulate a Coin Toss Experiment . . . . . 2228.9 Challenge: Simulate an Election . . . . . . . . . . . 2228.10 Summary and Additional Resources . . . . . . . . . 223 9 Tuples, Lists, and Dictionaries 2259.1 Tuples Are Immutable Sequences . . . . . . . . . . . 2269.2 Lists Are Mutable Sequences . . . . . . . . . . . . . 2369.3 Nesting, Copying, and Sorting Tuples and Lists . . . 2509.4 Challenge: List of lists . . . . . . . . . . . . . . . . . 2569.5 Challenge: Wax Poetic . . . . . . . . . . . . . . . . 2579.6 Store Relationships in Dictionaries . . . . . . . . . . 2599.7 Challenge: Capital City Loop . . . . . . . . . . . . . 2699.8 How to Pick a Data Structure . . . . . . . . . . . . . 2719.9 Challenge: Cats With Hats . . . . . . . . . . . . . . 2729.10 Summary and Additional Resources . . . . . . . . . 273 10 Object-Oriented Programming (OOP) 27510.1 Define a Class . . . . . . . . . . . . . . . . . . . . . 27610.2 Instantiate an Object . . . . . . . . . . . . . . . . . 28010.3 Inherit From Other Classes . . . . . . . . . . . . . . 28610.4 Challenge: Model a Farm . . . . . . . . . . . . . . . 29510.5 Summary and Additional Resources . . . . . . . . . 296 11 Modules and Packages 29711.1 Working With Modules . . . . . . . . . . . . . . . . 29811.2 Working With Packages . . . . . . . . . . . . . . . . 30911.3 Summary and Additional Resources . . . . . . . . . 317 12 File Input and Output 31912.1 Files and the File System . . . . . . . . . . . . . . . 32012.2 Working With File Paths in Python . . . . . . . . . . 32312.3 Common File System Operations . . . . . . . . . . . 33212.4 Challenge: Move All Image Files to a New Directory . 349 9
📄 Page
11
Contents 12.5 Reading and Writing Files . . . . . . . . . . . . . . 35012.6 Read and Write CSV Data . . . . . . . . . . . . . . . 36512.7 Challenge: Create a High Scores List . . . . . . . . . 37612.8 Summary and Additional Resources . . . . . . . . . 377 13 Installing Packages With pip 37813.1 Installing Third-Party Packages With pip . . . . . . . 37913.2 The Pitfalls of Third-Party Packages . . . . . . . . . 38913.3 Summary and Additional Resources . . . . . . . . . 391 14 Creating and Modifying PDF Files 39314.1 Extracting Text From a PDF . . . . . . . . . . . . . 39414.2 Extracting Pages From a PDF . . . . . . . . . . . . . 40114.3 Challenge: PdfFileSplitter Class . . . . . . . . . . . 40814.4 Concatenating and Merging PDFs . . . . . . . . . . 40914.5 Rotating and Cropping PDF Pages . . . . . . . . . . 41614.6 Encrypting and Decrypting PDFs . . . . . . . . . . . 42714.7 Challenge: Unscramble a PDF . . . . . . . . . . . . 43214.8 Creating a PDF File From Scratch . . . . . . . . . . 43214.9 Summary and Additional Resources . . . . . . . . . 439 15 WorkingWith Databases 44115.1 An Introduction to SQLite . . . . . . . . . . . . . . 44215.2 Libraries for Working With Other SQL Databases . . 45415.3 Summary and Additional Resources . . . . . . . . . 455 16 Interacting With theWeb 45716.1 Scrape and Parse Text From Websites . . . . . . . . 45816.2 Use an HTML Parser to Scrape Websites . . . . . . . 46816.3 Interact With HTML Forms . . . . . . . . . . . . . . 47416.4 Interact With Websites in Real Time . . . . . . . . . 48016.5 Summary and Additional Resources . . . . . . . . . 484 17 Scienti c Computing and Graphing 48617.1 Use NumPy for Matrix Manipulation . . . . . . . . . 48717.2 Use Matplotlib for Plotting Graphs . . . . . . . . . . 49817.3 Summary and Additional Resources . . . . . . . . . 521 10
📄 Page
12
Contents 18 Graphical User Interfaces 52218.1 Add GUI Elements With EasyGUI . . . . . . . . . . 52318.2 Example App: PDF Page Rotator . . . . . . . . . . . 53518.3 Challenge: PDF Page Extraction Application . . . . . 54218.4 Introduction to Tkinter . . . . . . . . . . . . . . . . 54318.5 Working With Widgets . . . . . . . . . . . . . . . . 54718.6 Controlling Layout With Geometry Managers . . . . 57218.7 Making Your Applications Interactive . . . . . . . . 59118.8 Example App: Temperature Converter . . . . . . . . 60118.9 Example App: Text Editor . . . . . . . . . . . . . . 60618.10 Challenge: Return of the Poet . . . . . . . . . . . . . 61518.11 Summary and Additional Resources . . . . . . . . . 617 19 Final Thoughts and Next Steps 61919.1 Free Weekly Tips for Python Developers . . . . . . . 62119.2 Python Tricks: The Book . . . . . . . . . . . . . . . 62119.3 Real Python Video Course Library . . . . . . . . . . 62219.4 Acknowledgements . . . . . . . . . . . . . . . . . . 623 11
📄 Page
13
Foreword Hello, and welcome to Python Basics: A Practical Introduction toPython 3. I hope you’re ready to learn why so many professional andhobbyist developers are drawn to Python and how you can beginusing it on your own projects, small and large, right away. This book is targeted at beginners who either know a little program-ming but not the Python language and ecosystem or are starting freshwith no programming experience whatsoever. If you don’t have a computer science degree, don’t worry. David, Dan,Joanna, and Fletcher will guide you through the important comput-ing concepts while teaching you the Python basics and, just as impor-tantly, skipping the unnecessary details at first. Python Is a Full-Spectrum Language When learning a new programming language, you don’t yet have theexperience to judge howwell it will serve you in the long run. If you’reconsidering learning Python, let me assure you that this is a goodchoice. One key reason is that Python is a full-spectrum language. What do I mean by this? Some languages are very good for beginners.They hold your hand and make programming super easy. We can goto the extreme and look at visual languages such as Scratch. In Scratch, you get blocks that represent programming concepts likevariables, loops, method calls, and so on, and you drag and drop themon a visual surface. Scratch may be easy to get started with for sim- 12
📄 Page
14
Contents ple programs, but you cannot build professional applications with it.Name one Fortune 500 company that powers its core business logicwith Scratch. Come up empty? Me too, because that would be insanity. Other languages are incredibly powerful for expert developers. Themost popular one in this category is likely C++ and its close relative,C. Whichever web browser you used today was likely written in C orC++. Your operating system running that browser was very likely alsobuilt with C/C++. Your favorite first-person shooter or strategy videogame? You nailed it: C/C++. You can do amazing things with these languages, but they are whollyunwelcoming to newcomers looking for a gentle introduction. You might not have read a lot of C++ code. It can almost make youreyes burn. Here’s an example, a real albeit complex one: template <typename T> _Defer<void(*(PID<T>, void (T::*)(void))) (const PID<T>&, void (T::*)(void))> defer(const PID<T>& pid, void (T::*method)(void)) { void (*dispatch)(const PID<T>&, void (T::*)(void)) = &process::template dispatch<T>; return std::tr1::bind(dispatch, pid, method); } Please, just no. Both Scratch and C++ are decidedly not what I would call full-spectrum languages. With Scratch, it’s easy to start, but you have toswitch to a “real” language to build real applications. Conversely, youcan build real apps with C++, but there’s no gentle on-ramp. Youdive headfirst into all the complexity of the language, which exists tosupport these rich applications. 13
📄 Page
15
Contents Python, on the other hand, is special. It is a full-spectrum language.We often judge the simplicity of a language based on the Hello, Worldtest. That is, what syntax and actions are necessary to get the languageto output Hello, World to the user? In Python, it couldn’t be simpler: print("Hello, World") That’s it! However, I find this an unsatisfying test. The Hello, World test is useful but really not enough to show the poweror complexity of a language. Let’s try another example. Not every-thing here needs to make total sense—just follow along to get the Zenof it. The book covers these concepts andmore as you go through. Thenext example is certainly something you could write as you get nearthe end of the book. Here’s the new test: What would it take to write a program that ac-cesses an externalwebsite, downloads the content to your app inmem-ory, then displays a subsection of that content to the user? Let’s trythat experiment using Python 3 with the help of the requests package(which needs to be installed—more on that in chapter 12): import requests resp = requests.get("http://olympus.realpython.org") html = resp.text print(html[86:132]) Incredibly, that’s it! When run, the program outputs something likethis: <h2>Please log in to access Mount Olympus:</h2> This is the easy, getting-started side of the Python spectrum. A fewtrivial lines can unleash incredible power. Because Python has accessto so many powerful but well-packaged libraries, such as requests, it’soften described as having batteries included. 14
📄 Page
16
Contents So there you have a simple yet powerful starter example. On the real-world side of things, many incredible applications have been writtenin Python as well. YouTube, the world’s most popular video streaming site, is written inPython and processes more than amillion requests per second. Insta-gram is another example of a Python application. Closer to home, weeven have realpython.com and my sites, such as talkpython.fm. This full-spectrum aspect of Python means that you can start withthe basics and adopt more advanced features as your application de-mands grow. Python Is Popular You might have heard that Python is popular. It may seem that itdoesn’t really matter how popular a language is so long as you canbuild the app you want to build with it. But, for better or worse, the popularity of a programming languageis a strong indicator of the quality of libraries you’ll have available aswell the number of job openings you’ll find. In short, you should tendto gravitate toward more popular technologies as there will be morechoices and integrations available. So, is Python actually that popular? Yes it is. You’ll find a lot ofhype and hyperbole, but there are plenty of stats backing this claim.Let’s look at some analytics presented by stackoverflow.com, a popu-lar question-and-answer site for programmers. StackOverflow runs a site called StackOverflowTrendswhere you canlook at the trends for various technologies by tag. When you compare 15
📄 Page
17
Contents Python to the other likely candidates you could pick to learn program-ming, you’ll see one is unlike the others: You can explore this chart and create similar charts to this one over atinsights.stackoverflow.com/trends. Notice the incredible growth of Python compared to the flat or evendownward trend of the other usual candidates! If you’re betting yourfuture on the success of a given technology, which one would youchoose from this list? That’s just one chart—what does it really tell us? Well, let’s look atanother. Stack Overflow does a yearly survey of developers. It’s com-prehensive and very well done. You can find the full 2020 results atinsights.stackoverflow.com/survey/2020. From that writeup, I’d like to call your attention to a section titled“Most Loved, Dreaded, and Wanted Languages.” In the “MostWanted” section, you’ll find data on the share of “developers who arenot developing with the language or technology but have expressedinterest in developing with it.” 16
📄 Page
18
Contents Again, in the graph below, you’ll see that Python is topping the chartsand is well above even second place: If you agree with me that the relative popularity of a programminglanguage matters, then Python is clearly a good choice. We Don’t Need You to Be a Computer Scientist One other point that I want to emphasize as you start your Pythonlearning journey is that we don’t need you to be a computer scientist.If that’s your goal, then great. Learning Python is a powerful stepin that direction. But the invitation to learn programming is oftenframed as “We have all these developer jobs going unfilled! We needsoftware developers!” That may or may not be true. But, more importantly, programming(even a little programming) can be a personal superpower for you. To illustrate this idea, suppose you are a biologist. Should you dropout of biology and get a job as a front-end web developer? Probablynot. But skills such as the one I opened this foreword with, using re-quests to get data from theWeb, can be incredibly powerful for you asas a biologist. Rather than manually exporting and scraping data from the Web orfrom spreadsheets, you can use Python to scrape thousands of datasources or spreadsheets in the time it takes you to do just one man- 17
📄 Page
19
Contents ually. Python skills can take your biology power and amplify it wellbeyond your colleagues’ to make it your superpower. Dan and Real Python Finally, let me leave you with a comment on your authors. Dan Baderand the other Real Python authors work day in and day out to bringclear and powerful explanations of Python concepts to all of us viarealpython.com. They have a unique view into the Python ecosystem and are keyed intowhat beginners need to know. I’m confident leaving you in their hands on this Python journey. Goforth and learn this amazing language using this great book. Mostimportantly, remember to have fun! —Michael Kennedy, Founder of Talk Python (@mkennedy) 18
📄 Page
20
Chapter 1 Introduction Welcome to Real Python’s Python Basics book, fully updated forPython 3.9! In this book, you’ll learn real-world Python program-ming techniques, illustrated with useful and interesting examples. Whether you’re a new programmer or a professional software devel-oper looking to dive into a new language, this book will teach you allthe practical Python that you need to get started on projects of yourown. No matter what your ultimate goals may be, if you work with a com-puter at all, then you’ll soon be finding endless ways to improve yourlife by automating tasks and solving problems through Python pro-grams that you create. But what’s so great about Python as a programming language? Forone, Python is open source freeware, meaning you can download itfor free and use it for any purpose, commercial or not. Python also has an amazing community that has built a number ofuseful tools that you can use in your own programs. Need to workwith PDF documents? There’s a comprehensive tool for that. Want tocollect data from web pages? No need to start from scratch! 19