Learn Python the Hard Way, Fifth Edition (Zed A. Shaw) (Z-Library)

Author: Zed A. Shaw

Python

In Learn Python the Hard Way, Fifth Edition, you'll learn Python by working through 60 brilliantly crafted exercises. Read them. Type their code precisely. (No copying and pasting!) Fix your mistakes. Watch the programs run. As you do, you'll learn how a computer works; what good programs look like; and how to read, write, and think about code. Install a complete Python environment Organize and write code Fix and break code Basic mathematics Variables Strings and text Interact with users Work with files Looping and logic Data structures using lists and dictionaries Program design Object-oriented programming Inheritance and composition Modules, classes, and objects Python packaging Automated testing Basic data munging Basic statistics with NumPy and SciPy Data Analysis It'll be hard at first. But soon, you'll just get it--and that will feel great! This course will reward you for every minute you put into it. Soon, you'll know one of the world's most powerful, popular programming languages. You'll be a Python programmer. This book is perfect for Total beginners with zero programming experience Junior developers who know one or two languages Returning professionals who haven't written code in years Seasoned professionals looking for a fast, simple, crash course in Python

📄 File Format: PDF
💾 File Size: 3.4 MB
5
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
(This page has no text content)
📄 Page 3
D I D E Hoboken, New Jersey
📄 Page 4
Cover image: ioat/Shutterstock Author photo courtesy of Zed A. Shaw Pages 6, 7: Screenshots from Jupyter Pages 84, 86: Screenshots from Python Software Foundation Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at corpsales@pearsoned.com or (800) 382-3419. For government sales inquiries, please contact governmentsales@pearsoned.com. For questions about sales outside the U.S., please contact intlcs@pearson.com. Visit us on the Web: informit.com/aw Library of Congress Control Number: 2023950645 Copyright © 2024 Zed A. Shaw All rights reserved. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearson.com/permissions. ISBN-13: 978-0-13-827057-5 ISBN-10: 0-13-827057-0 $PrintCode
📄 Page 5
v Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Improvements in the Fifth Edition . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx MODULE 1 Getting Started in Python . . . . . . . . . . . . . . . . . 1 Exercise 0 Gearing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 General Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Minimalist Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Complete Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Testing Your Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Learning the Command Line . . . . . . . . . . . . . . . . . . . . . . . 4 Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Exercise 1 A Good First Program . . . . . . . . . . . . . . . . . . . . . . 6 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 9 The Blue Plus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Exercise 2 Comments and Pound Characters . . . . . . . . . . . . . . . 10 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 11 Exercise 3 Numbers and Math . . . . . . . . . . . . . . . . . . . . . . . 12 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 13 Exercise 4 Variables and Names . . . . . . . . . . . . . . . . . . . . . . 16 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 17 Exercise 5 More Variables and Printing . . . . . . . . . . . . . . . . . . 20 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
📄 Page 6
vi CONTENTS Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 21 Exercise 6 Strings and Text . . . . . . . . . . . . . . . . . . . . . . . . . 22 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 24 Exercise 7 Combining Strings . . . . . . . . . . . . . . . . . . . . . . . . 26 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 27 Exercise 8 Formatting Strings Manually . . . . . . . . . . . . . . . . . . 28 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 29 Exercise 9 Multi-Line Strings . . . . . . . . . . . . . . . . . . . . . . . . 30 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 31 Exercise 10 Escape Codes in Strings . . . . . . . . . . . . . . . . . . . 32 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 34 Exercise 11 Asking People Questions . . . . . . . . . . . . . . . . . . . 36 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 37 Exercise 12 An Easier Way to Prompt . . . . . . . . . . . . . . . . . . . 38 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 39
📄 Page 7
CONTENTS vii Exercise 13 Parameters, Unpacking, Variables . . . . . . . . . . . . . . 40 If You Get Lost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Code Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Hold Up! Features Have Another Name . . . . . . . . . . . . . . . . . 42 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 43 Exercise 14 Prompting and Passing . . . . . . . . . . . . . . . . . . . . 46 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 47 Exercise 15 Reading Files . . . . . . . . . . . . . . . . . . . . . . . . . . 50 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 52 Exercise 16 Reading and Writing Files . . . . . . . . . . . . . . . . . . . 54 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 56 Exercise 17 More Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 59 MODULE 2 The Basics of Programming . . . . . . . . . . . . . . . 61 Exercise 18 Names, Variables, Code, Functions . . . . . . . . . . . . . 62 Exercise Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 66 Exercise 19 Functions and Variables . . . . . . . . . . . . . . . . . . . . 68 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 70
📄 Page 8
viii CONTENTS Exercise 20 Functions and Files . . . . . . . . . . . . . . . . . . . . . . 72 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 74 Exercise 21 Functions Can Return Something . . . . . . . . . . . . . . . 76 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 78 Exercise 22 Strings, Bytes, and Character Encodings . . . . . . . . . . 80 Initial Research . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Switches, Conventions, and Encodings . . . . . . . . . . . . . . . . . 82 Dissecting the Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Dissecting the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Encodings Deep Dive . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Breaking It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Exercise 23 Introductory Lists . . . . . . . . . . . . . . . . . . . . . . . . 88 Accessing Elements of a List . . . . . . . . . . . . . . . . . . . . . . . 88 Practicing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 The Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Fruit Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Cars Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Languages Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Final Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Exercise 24 Introductory Dictionaries . . . . . . . . . . . . . . . . . . . . 92 Key/Value Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Combining Lists with Data Objects . . . . . . . . . . . . . . . . . . . . 93 The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 The Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Fruit Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Cars Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Languages Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Final Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
📄 Page 9
CONTENTS ix Exercise 25 Dictionaries and Functions . . . . . . . . . . . . . . . . . . 98 Step 1: Function Names Are Variables . . . . . . . . . . . . . . . . . . 98 Step 2: Dictionaries with Variables . . . . . . . . . . . . . . . . . . . . 98 Step 3: Dictionaries with Functions . . . . . . . . . . . . . . . . . . . . 99 Step 4: Deciphering the Last Line . . . . . . . . . . . . . . . . . . . . . 99 Study Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Exercise 26 Dictionaries and Modules . . . . . . . . . . . . . . . . . . . 102 Step 1: Review of import . . . . . . . . . . . . . . . . . . . . . . . . 102 Step 2: Find the __dict__ . . . . . . . . . . . . . . . . . . . . . . . . 102 Step 3: Change the __dict__ . . . . . . . . . . . . . . . . . . . . . . 103 Study Drill: Find the “Dunders” . . . . . . . . . . . . . . . . . . . . . . 104 Exercise 27 The Five Simple Rules to the Game of Code . . . . . . . . 106 Rule 1: Everything Is a Sequence of Instructions . . . . . . . . . . . . 106 How can I get this output? . . . . . . . . . . . . . . . . . . . . . . . 107 Where are these bytes stored? . . . . . . . . . . . . . . . . . . . . 108 Rule 2: Jumps Make the Sequence Non-Linear . . . . . . . . . . . . . 108 Why is this backward? . . . . . . . . . . . . . . . . . . . . . . . . . 109 Can a JUMP go forward? . . . . . . . . . . . . . . . . . . . . . . . . 109 Rule 3: Tests Control Jumps . . . . . . . . . . . . . . . . . . . . . . . . 110 What do you mean “pop”? . . . . . . . . . . . . . . . . . . . . . . . 111 Wait, aren’t tests like COMPARE_OP used in loops too? . . . . . . . 111 Rule 4: Storage Controls Tests . . . . . . . . . . . . . . . . . . . . . . 111 Rule 5: Input/Output Controls Storage . . . . . . . . . . . . . . . . . . 112 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 The List of Byte Codes . . . . . . . . . . . . . . . . . . . . . . . . . 113 dis() Is a Side Quest . . . . . . . . . . . . . . . . . . . . . . . . . 114 Exercise 28 Memorizing Logic . . . . . . . . . . . . . . . . . . . . . . . 116 The Truth Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 The Truth Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 119 Exercise 29 Boolean Practice . . . . . . . . . . . . . . . . . . . . . . . . 120 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 122
📄 Page 10
x CONTENTS Exercise 30 What If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 dis() It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Study Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 125 Exercise 31 Else and If . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 dis() It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 128 Exercise 32 Making Decisions . . . . . . . . . . . . . . . . . . . . . . . 130 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 dis() It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 132 Exercise 33 Loops and Lists . . . . . . . . . . . . . . . . . . . . . . . . . 134 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 dis() It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 137 Exercise 34 While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . 138 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 dis() It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 140 Exercise 35 Branches and Functions . . . . . . . . . . . . . . . . . . . . 142 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 144 Exercise 36 Designing and Debugging . . . . . . . . . . . . . . . . . . . 146 From Idea to Working Code . . . . . . . . . . . . . . . . . . . . . . . . 146 Is This a Professional Process? . . . . . . . . . . . . . . . . . . . . 147 About the “X/Y” Non-Problem . . . . . . . . . . . . . . . . . . . . . 148 Rules for If-Statements . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Rules for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
📄 Page 11
CONTENTS xi Tips for Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Homework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Exercise 37 Symbol Review . . . . . . . . . . . . . . . . . . . . . . . . . 152 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 String Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . 154 Old-Style String Formats . . . . . . . . . . . . . . . . . . . . . . . . . 154 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Reading Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 157 MODULE 3 Applying What You Know . . . . . . . . . . . . . . . . .159 Exercise 38 Beyond Jupyter for Windows . . . . . . . . . . . . . . . . . 160 Why Learn PowerShell? . . . . . . . . . . . . . . . . . . . . . . . . . . 161 What Is PowerShell? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 PowerShell versus Cmder . . . . . . . . . . . . . . . . . . . . . . . 163 Starting Jupyter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Where Are You with start? . . . . . . . . . . . . . . . . . . . . . . 164 Going from Graphics to PowerShell . . . . . . . . . . . . . . . . . . 165 Where Are You with pwd? . . . . . . . . . . . . . . . . . . . . . . . 165 What’s in Here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Files, Folders, Directories, and Paths . . . . . . . . . . . . . . . . . 167 Moving Around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Relative Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Creating and Destroying . . . . . . . . . . . . . . . . . . . . . . . . 169 Flags and Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Copy and Move . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . 170 Running Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Common Key Sequences . . . . . . . . . . . . . . . . . . . . . . . . 171 Useful Developer Commands . . . . . . . . . . . . . . . . . . . . . 171 Crash Landing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Exercise 39 Beyond Jupyter for macOS/Linux . . . . . . . . . . . . . . . 172 macOS Troubles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
📄 Page 12
xii CONTENTS Why Learn Bash or ZSH? . . . . . . . . . . . . . . . . . . . . . . . . . 173 What Is Bash? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Starting Jupyter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Where Are You with open? . . . . . . . . . . . . . . . . . . . . . . . 175 Going from Graphics to Bash . . . . . . . . . . . . . . . . . . . . . 176 Where Are You with pwd? . . . . . . . . . . . . . . . . . . . . . . . 177 What’s in Here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Files, Folders, Directories, and Paths . . . . . . . . . . . . . . . . . 178 Moving Around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Relative Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Creating and Destroying . . . . . . . . . . . . . . . . . . . . . . . . 180 Hidden Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Flags and Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Copy and Move . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . 182 Running Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Common Key Sequences . . . . . . . . . . . . . . . . . . . . . . . . 183 Useful Developer Commands . . . . . . . . . . . . . . . . . . . . . 183 Crash Landing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Exercise 40 Advanced Developer Tools . . . . . . . . . . . . . . . . . . 186 Managing conda Environments . . . . . . . . . . . . . . . . . . . . . 186 Adding conda-forge . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Using pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Using a .condarc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 General Editing Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Going Further . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Exercise 41 A Project Skeleton . . . . . . . . . . . . . . . . . . . . . . . 190 Activate an Environment . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Just Use cookiecutter . . . . . . . . . . . . . . . . . . . . . . . . . 190 Building Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Installing Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Testing the Install . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Remove test-project . . . . . . . . . . . . . . . . . . . . . . . . . 192 Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
📄 Page 13
CONTENTS xiii Exercise 42 Doing Things to Lists . . . . . . . . . . . . . . . . . . . . . . 194 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 What Lists Can Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 When to Use Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 198 Exercise 43 Doing Things to Dictionaries . . . . . . . . . . . . . . . . . 200 A Dictionary Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 What Dictionaries Can Do . . . . . . . . . . . . . . . . . . . . . . . . . 203 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 204 Exercise 44 From Dictionaries to Objects . . . . . . . . . . . . . . . . . 206 Step 1: Passing a Dict to a Function . . . . . . . . . . . . . . . . . . . 206 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . 207 Step 2: talk inside the Dict . . . . . . . . . . . . . . . . . . . . . . . . 207 Step 3: Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . 209 Step 4: A Person Constructor . . . . . . . . . . . . . . . . . . . . . . . 209 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Exercise 45 Basic Object-Oriented Programming . . . . . . . . . . . . . 212 Python’s People . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Using dir() and __dict__ . . . . . . . . . . . . . . . . . . . . . . . 213 About the Dot (.) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 A Word on self . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 217 Exercise 46 Inheritance and Advanced OOP . . . . . . . . . . . . . . . 218 How This Looks in Code . . . . . . . . . . . . . . . . . . . . . . . . . . 219 About class Name(object) . . . . . . . . . . . . . . . . . . . . . . . . . 221 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 222 Exercise 47 Basic Object-Oriented Analysis and Design . . . . . . . . . 224 The Analysis of a Simple Game Engine . . . . . . . . . . . . . . . . . 225 Write or Draw About the Problem . . . . . . . . . . . . . . . . . . . 225
📄 Page 14
xiv CONTENTS Extract Key Concepts and Research Them . . . . . . . . . . . . . . 226 Create a Class Hierarchy and Object Map for the Concepts . . . . 226 Code the Classes and a Test to Run Them . . . . . . . . . . . . . . 227 Repeat and Refine . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Top Down versus Bottom Up . . . . . . . . . . . . . . . . . . . . . . . 229 The Code for “Gothons from Planet Percal #25” . . . . . . . . . . . . . 230 What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 237 Exercise 48 Inheritance versus Composition . . . . . . . . . . . . . . . . 238 What Is Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Implicit Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Override Explicitly . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Alter Before or After . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 All Three Combined . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 The Reason for super() . . . . . . . . . . . . . . . . . . . . . . . . . 243 Using super() with __init__() . . . . . . . . . . . . . . . . . . 243 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 When to Use Inheritance or Composition . . . . . . . . . . . . . . . . 245 Study Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 246 Exercise 49 You Make a Game . . . . . . . . . . . . . . . . . . . . . . . 248 Evaluating Your Game . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Function Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Class Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Code Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Good Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Evaluate Your Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Exercise 50 Automated Testing . . . . . . . . . . . . . . . . . . . . . . . 252 What Is the Purpose of Testing? . . . . . . . . . . . . . . . . . . . . . 252 How to Test Efficiently . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Install PyTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Simple PyTest Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Running pytest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Exceptions and try/except . . . . . . . . . . . . . . . . . . . . . . . 255 Getting Coverage Reports . . . . . . . . . . . . . . . . . . . . . . . . . 256
📄 Page 15
CONTENTS xv Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . 257 MODULE 4 Python and Data Science . . . . . . . . . . . . . . . . .259 Exercise 51 What Is Data Munging? . . . . . . . . . . . . . . . . . . . . 260 Why Data Munging? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 The Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 How to Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Process Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Solution Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Awesome ETL Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Exercise 52 Scraping Data from the Web . . . . . . . . . . . . . . . . . 268 Introducing with . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 The Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 The Clue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Awesome Scraping Tools . . . . . . . . . . . . . . . . . . . . . . . . . 270 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Exercise 53 Getting Data from APIs . . . . . . . . . . . . . . . . . . . . 272 Introducing JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 The Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 The Clue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Awesome API Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Exercise 54 Data Conversion with pandas . . . . . . . . . . . . . . . . . 276 Introducing Pandoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 The Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 The Clue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Exercise 55 How to Read Documentation (Featuring pandas) . . . . . . 280 Why Programmer Documentation Sucks . . . . . . . . . . . . . . . . . 280 How to Actively Read Programmer Docs . . . . . . . . . . . . . . . . . 281
📄 Page 16
xvi CONTENTS Step #1: Find the Docs . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Step #1 with pandas . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Step #2: Determine Your Strategy . . . . . . . . . . . . . . . . . . . . 282 Step #2 with pandas . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Step #3: Code First, Docs Second . . . . . . . . . . . . . . . . . . . . 283 Step #3 with pandas . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Step #4: Break or Change the Code . . . . . . . . . . . . . . . . . . . 283 Step #5: Take Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Step #6: Use It on Your Own . . . . . . . . . . . . . . . . . . . . . . . 284 Step #6 with pandas . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Step #7: Write About What You Learned . . . . . . . . . . . . . . . . . 284 Step #7 with pandas . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Step #8: What’s the Gestalt? . . . . . . . . . . . . . . . . . . . . . . . 285 Step #8 with pandas . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Reading My pandas Curriculum . . . . . . . . . . . . . . . . . . . . . . 286 Exercise 56 Using Only pandas . . . . . . . . . . . . . . . . . . . . . . . 288 Make a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 The Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Study Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Exercise 57 The SQL Crash Course . . . . . . . . . . . . . . . . . . . . 290 What Is SQL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 The Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Fixing and Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Back Up Your Database . . . . . . . . . . . . . . . . . . . . . . . . 292 Create, Read, Update, Delete . . . . . . . . . . . . . . . . . . . . . . . 293 SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 UPDATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 DELETE and Transactions . . . . . . . . . . . . . . . . . . . . . . . . 297 Math, Aggregates, and GROUP BY . . . . . . . . . . . . . . . . . . . 298 Python Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Exercise 58 SQL Normalization . . . . . . . . . . . . . . . . . . . . . . . 300 What Is Normalization? . . . . . . . . . . . . . . . . . . . . . . . . . . 300 First Normal Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
📄 Page 17
CONTENTS xvii Implementing 1NF . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 Creating Tables in SQL . . . . . . . . . . . . . . . . . . . . . . . . . 303 Second Normal Form (2NF) . . . . . . . . . . . . . . . . . . . . . . . . 303 Implementing 2NF . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Using Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Querying 2NF Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Querying with Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Exercise 59 SQL Relationships . . . . . . . . . . . . . . . . . . . . . . . 310 One-to-Many (1:M) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 One-to-Many in Python . . . . . . . . . . . . . . . . . . . . . . . . . 310 One-to-Many Problem . . . . . . . . . . . . . . . . . . . . . . . . . 311 Many-to-Many (M:M) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Many-to-Many Problem . . . . . . . . . . . . . . . . . . . . . . . . . 312 One-to-One (1:1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 Attributed Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 Querying M:M Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 Your Last Study Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Exercise 60 Advice from an Even Older Programmer . . . . . . . . . . . 316 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
📄 Page 18
This page intentionally left blank
📄 Page 19
xix Preface T his simple book is meant to get you started in programming. The title says it’s the hard way to learn to write code, but it’s actually not. It’s only the “hard” way because it uses a technique called instruction. Instruction is where I tell you to do a sequence of controlled exercises designed to build a skill through repetition. This technique works very well with beginners who know nothing and need to acquire basic skills before they can understand more complex topics. It’s used in everything from martial arts to music to even basic math and reading skills. This book instructs you in Python by slowly building and establishing skills through techniques such as practice and memorization, then applying them to increasingly difficult problems. By the end of the book you will have the tools needed to begin learning more complex programming topics. I like to tell people that my book gives you your “programming black belt.” What this means is that you know the basics well enough to now start learning programming. If you work hard, take your time, and build these skills, you will learn to code. Improvements in the Fifth Edition This latest edition of Learn Python the Hard Way features many innovations from over a decade of teaching absolute beginners (aka pre-beginners) how to code. The following improvements are just a few that should help almost anyone learn to code: 1. A focus on getting started quickly using Jupyter notebooks and Anaconda, rather than the traditional Python tools from the previous books. 2. Though starting with Jupyter and Anaconda, students later “graduate” to a more traditional environment found in professional Python development. 3. Less repetition of the same concepts but more combination and interaction with previous con- cepts to reinforce learning. 4. A whole new Exercise 24 that teaches the basic concepts of a Turing machine by teaching students how to use dis() to inspect Python’s byte codes. I’ve found this solves a major problemwhere students feel they don’t “really know howPython works” by giving them a looking glass into how Python actually works. 5. A completely rewritten Object-Oriented Programming section that teaches Python’s objects and classes by having students create their own toy OOP system. 6. A whole new set of nine modules that teach the “Data part of Data Science,” from covering introductory topics of processing CSV files to interacting with SQL databases.
📄 Page 20
xx LEARN PYTHON THE HARD WAY 7. New typographic conventions that make clear exactly what kind of code is being discussed. For example, I’ll say, “look at the talk() function,” when I want you to review the function named “talk” in the code. Traditionally, programming textbooks would leave off the () characters since it’s seen as redundant because they also used the word “function.” I’ve found adding the () helps students find the functions in their code and eliminates any confusion about what talk! refers to. 8. Lots of brand new subtle humor and programmer “dad jokes” to make the topic unserious and a bit more fun. I hope you enjoy my latest work, and if you ever find any mistakes or have any questions you can email me at help@learncodethehardway.com. You can also find quick fixes, updated install instructions, and additional advice at https://learncodethehardway.com/setup/python/. I recommend visiting this link first if you have any weird errors while installing software as there may be updates that are not in your printed copy. Register your copy of Learn Python the Hard Way, Fifth Edition, on the InformIT site for convenient access to updates and corrections as they become available. To start the registration process, go to informit.com/register and log in or create an account. Enter the product ISBN (9780138270575) and click Submit. If you would like to be notified of exclusive offers on new editions and updates, please check the box to receive email from us. Acknowledgments I’d like to thank Nick Cohron for reviewing the book and finding all the errors for me. I’d also like to thank my editor Debra Williams Cauley for putting up with my constant lateness and the late-night insomniac phone calls, as well as Julie Nahil and the whole crew at Pearson for their hard work producing the book. I’d also like to give a special thanks to all the past students who supported my work over the years. My courses would not be as polished if it weren’t for your feedback, criticism, and praise. I truly do thank you for helping me create something precious over the years, and I wish you all great success.
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List