(This page has no text content)
Zero to Py A Comprehensive Guide to Learning the Python Programming Language Michael Green This book is for sale at http://leanpub.com/zero-to-py This version was published on 2023-02-20 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. © 2023 Michael Green
Contents Zero to Py: A Comprehensive Guide to Learning the Python Programming Language . . . . . . . . . . . . . . . . . . . . . . . . 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Part I: A Whirlwind Tour . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 0: System Setup . . . . . . . . . . . . . . . . . . . . . . . . . 3 Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Python Versions . . . . . . . . . . . . . . . . . . . . . . . 4 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 What’s in a PATH? . . . . . . . . . . . . . . . . . . . . . 6 Setting up your dev environment . . . . . . . . . . . . . . . 7 Running Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 A program that runs programs . . . . . . . . . . . . . 8 One interpreter, many modes . . . . . . . . . . . . . . 8 Chapter 1. Fundamental data types . . . . . . . . . . . . . . . . . . 11 But first, Variables . . . . . . . . . . . . . . . . . . . . . . . . . 11 Primitives and Containers . . . . . . . . . . . . . . . . . . . . 14 Mutability vs Immutability . . . . . . . . . . . . . . . . . . . . 15 Introduction to Python’s data types . . . . . . . . . . . . . . 15 Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . 16 Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
CONTENTS Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Complex . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Container Types . . . . . . . . . . . . . . . . . . . . . . . 20 Tuples and Lists . . . . . . . . . . . . . . . . . . . . . . 20 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . 23 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Python Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 References to Objects . . . . . . . . . . . . . . . . . . . . 27 Chapter 2. Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Packing operators . . . . . . . . . . . . . . . . . . . . . . 31 Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Logical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 What is Truthy? . . . . . . . . . . . . . . . . . . . . . . . 36 Short-Circuits . . . . . . . . . . . . . . . . . . . . . . . . . 36 Logical Assignments . . . . . . . . . . . . . . . . . . . . 36 Membership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Bitwise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Chapter 3. Lexical Structure . . . . . . . . . . . . . . . . . . . . . . . 41 Line Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Explicit and Implicit Line Joining . . . . . . . . . . . . . . . 43 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Chapter 4. Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . 46 if, elif, and else . . . . . . . . . . . . . . . . . . . . . . . . . 46 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
CONTENTS continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 What is an Iterable? . . . . . . . . . . . . . . . . . . . . . 50 for/else and break . . . . . . . . . . . . . . . . . . . . . . . 51 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . 53 raise from . . . . . . . . . . . . . . . . . . . . . . . . . . 56 else and finally . . . . . . . . . . . . . . . . . . . . . 56 match . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 type checking . . . . . . . . . . . . . . . . . . . . . . . . . 60 guards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Or Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 as . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Chapter 5. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Function Signatures . . . . . . . . . . . . . . . . . . . . . . . . . 66 Explicitly positional/key-value . . . . . . . . . . . . . . 69 Default Values . . . . . . . . . . . . . . . . . . . . . . . . . 69 Mutable Types as Default Values . . . . . . . . . . . 70 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Nested Scopes . . . . . . . . . . . . . . . . . . . . . . . . . 73 nonlocal and global . . . . . . . . . . . . . . . . . . . 74 Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . 77 Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Chapter 6. Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 data types as classes. . . . . . . . . . . . . . . . . . . . . . . . . 84 __dunder__ methods . . . . . . . . . . . . . . . . . . . . . . . 85 The __init__ method . . . . . . . . . . . . . . . . . . . 85 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Class Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 A Functional Approach . . . . . . . . . . . . . . . . . . . . . . 88 @staticmethod . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
CONTENTS @classmethod . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Part II. A Deeper Dive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Chapter 7. Expressions, Comprehensions, and Generators . . 92 Generator Expressions . . . . . . . . . . . . . . . . . . . . . . . 92 Generator Functions . . . . . . . . . . . . . . . . . . . . . . . . 93 yield from . . . . . . . . . . . . . . . . . . . . . . . . . . 94 List Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . 95 Dictionary Comprehensions . . . . . . . . . . . . . . . . . . . 96 Expressions and the Walrus Operator . . . . . . . . . . . . . 96 Chapter 8. Python’s Built-in Functions . . . . . . . . . . . . . . . 97 Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . 104 all() and any() . . . . . . . . . . . . . . . . . . . . . . . . . . 106 dir() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 enumerate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 eval() and exec() . . . . . . . . . . . . . . . . . . . . . . . . 108 map() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 filter() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 input() and print() . . . . . . . . . . . . . . . . . . . . . . 110 open() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 range() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 sorted() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 reversed() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 zip() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Chapter 9. The Python Data Model . . . . . . . . . . . . . . . . . . 115 Object Creation Using __new__ and __init__ . . . . . . 116 Singletons . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Rich Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . 120 String Representations . . . . . . . . . . . . . . . . . . . 123
CONTENTS Emulating Containers . . . . . . . . . . . . . . . . . . . . . . . 123 Emulating Functions . . . . . . . . . . . . . . . . . . . . . . . . 126 Using Slots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Customizing Attribute Access . . . . . . . . . . . . . . . . . . 127 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Lazy Evaluation . . . . . . . . . . . . . . . . . . . . . . . 131 Context Managers . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Chapter 10. Concepts in Object-Oriented Programming . . . . 139 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Calling the Super Class using super() . . . . . . . . 141 Multiple Inheritance and Method Resolution Order 142 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Chapter 11. Metaclasses . . . . . . . . . . . . . . . . . . . . . . . . . 146 Chapter 12. The Data Types, Revisited. . . . . . . . . . . . . . . . 149 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Bits and Bytes . . . . . . . . . . . . . . . . . . . . . . . . . 151 Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Float Methods . . . . . . . . . . . . . . . . . . . . . . . . . 152 Hex Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 153 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Split and Join . . . . . . . . . . . . . . . . . . . . . . . . . 153 Search and Replace . . . . . . . . . . . . . . . . . . . . . 154 Paddings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Translating . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Prefixes and Suffixes . . . . . . . . . . . . . . . . . . . . 158
CONTENTS Boolean Checks . . . . . . . . . . . . . . . . . . . . . . . . 159 Case Methods . . . . . . . . . . . . . . . . . . . . . . . . . 160 Encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Decoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Hex Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Counting and Indexing . . . . . . . . . . . . . . . . . . . 164 Copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Mutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Orderings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Iter Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Getter/Setter Methods . . . . . . . . . . . . . . . . . . . 169 Mutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Creating new Dictionaries . . . . . . . . . . . . . . . . 171 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Mutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Set Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Boolean Checks . . . . . . . . . . . . . . . . . . . . . . . . 176 Copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Chapter 13. Type Hints . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Incorporating Type Hints . . . . . . . . . . . . . . . . . . . . . 178 Union types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Optional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 type|None . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 TypeAlias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 NewType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
CONTENTS TypeVar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Runtime type checking . . . . . . . . . . . . . . . . . . . 191 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 TypedDict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Chapter 14. Modules, Packages, and Namespaces . . . . . . . . 196 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Module Attributes . . . . . . . . . . . . . . . . . . . . . . 199 if __name__ == "__main__": . . . . . . . . . . . . 200 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Imports within packages . . . . . . . . . . . . . . . . . . 203 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Part III. The Python Standard Library . . . . . . . . . . . . . . . . . . 206 Chapter 15. Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Chapter 16. Itertools . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Chaining Iterables . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Filtering Iterables . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Cycling Through Iterables . . . . . . . . . . . . . . . . . . . . 211 Creating Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Slicing Iterables . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Zip Longest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Chapter 17. Functools . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Partials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Reduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Dispatching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Chapter 18. Enums, NamedTuples, and Dataclasses . . . . . . . 218 Enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 NamedTuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
CONTENTS Dataclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Chapter 19. Multithreading and Multiprocessing . . . . . . . . . 224 Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Thread Locks . . . . . . . . . . . . . . . . . . . . . . . . . 227 Multiprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Process and Pool . . . . . . . . . . . . . . . . . . . . . . . 230 Process Locks . . . . . . . . . . . . . . . . . . . . . . . . . 232 Pipes and Queues . . . . . . . . . . . . . . . . . . . . . . 233 concurrent.futures . . . . . . . . . . . . . . . . . . . . . . 235 Chapter 20. Asyncio . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Tasks and Task Groups . . . . . . . . . . . . . . . . . . . . . . 240 ExceptionGroup and Exception unpacking . . . . 241 Part VI. The Underbelly of the Snake . . . . . . . . . . . . . . . . . . . 244 Chapter 21. Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 244 pdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Other Debuggers . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Chapter 22. Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 cProfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 flameprof . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 snakeviz . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 memory_profiler . . . . . . . . . . . . . . . . . . . . . . . . . 260 Chapter 23. C extensions . . . . . . . . . . . . . . . . . . . . . . . . . 261 Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 hello_world.c . . . . . . . . . . . . . . . . . . . . . . . 263 setup.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Passing data in and out of Python . . . . . . . . . . . . . . . 270 Memory Management . . . . . . . . . . . . . . . . . . . 274 Parsing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 275 Parsing Tuple Arguments . . . . . . . . . . . . . . . . . 275
CONTENTS Parsing Keyword Arguments . . . . . . . . . . . . . . . 276 Creating PyObjects . . . . . . . . . . . . . . . . . . . . . . . . . 278 Importing Modules . . . . . . . . . . . . . . . . . . . . . . . . . 280 Defining New Types . . . . . . . . . . . . . . . . . . . . . . . . 281 Stack type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Debugging C extensions . . . . . . . . . . . . . . . . . . . . . 301
Zero to Py: A Comprehensive Guide to Learning the Python Programming Language Introduction Python is a powerful and versatile programming language that is widely used across many different industries. From data science and machine learning to web development and automation, Python is a go-to choice for many developers due to its ease of use and its vast developer ecosystem. This book aims to be a comprehensive guide to learning the Python Programming Language, covering all the essential concepts and topics that a python developer needs to know. This book has been written so to be accessible to readers of all levels, whether you’re just starting your journery into programming, or already have some experience with python. Even more advanced users may find some utility from the later chapters. This book is divided into four parts. In Part I of this book, we start by introducing you to the basics of Python,
Zero to Py: A Comprehensive Guide to Learning the Python Programming Language 2 building a foundation on the fundamentals as we cover topics such as data types, operators, control flow, functions, and classes. In Part II of this book, we build upon the foundation established in Part I and dive deeper intomore advanced features of the Python programming language. We explore the more advanced features of Python, discussing topics such as generators, the data object model, metaclasses, etc, with the explicit aim to help you write more efficient and elegant code. And finally, we’ll look into Pythonmodules and packaging, so we can see how to take your python code and construct libraries which can be shared with the broader python community. In Part III of this book, we will take a closer look at some of the “batteries included” sections of the Python standard library. These are the modules and packages that are included with every Python installation and provide a wide range of functionality that can be used to solve a variety of problems. We’ll explore packages like functools, itertools, dataclasses, etc. And finally in Part VI we’ll dig into mechanisms for profiling and debugging python. Furthermore when we identify specific pain points in our implementation, we’ll see how to refactor our projects to use performant C code for a boost in execution speed. We’ll also look into how to use C debuggers to run the python interpreter, so we can debug our C extensions.
Part I: A Whirlwind Tour Chapter 0: System Setup If you have a working instance of python installed and are comfortable using it, feel free to skip this chapter. Python code is executed by a software program called the python interpreter. The python interpreter reads the code from top to bottom, line by line, and performs actions corresponding to the directive of the program. Unlike a compiler, which converts the entire code intomachine- readable code before executing it, an interpreter executes the code as it is read. This book is written against CPython 3.11, and many of the features discussed are version-dependent. If your system comes with a prepack- aged version of python that is older than 3.11, you may consider looking into a python version manager. Personally I recommend using pyenv to manage your local and global python versions. My typical workflow is to use pyenv to switch local versions to a target distribution, and then use that version to create a local virtual environment using venv. This however is just one of many opinions, and how you choose to configure your setup is completely up to you. Installing Python Installing the python interpreter is the first step to getting started with programming in python. The installation process is different for every
Part I: A Whirlwind Tour 4 operating system. To install the latest version, Python 3.11, you’ll want to go to the downloads¹ page of the official website for the Python Software Foundation (PSF), and click the “download” button for the latest release version. This will take you to a page where you can select a version specific for your operating system. Python Versions Python is versioned by release number, <major>.<minor>.<patch>. The latest version is the one with the greatest major version, followed by the greatest minor version, followed by the greatest patch version. So for example, 3.1.0 is greater that 2.7.18, and 3.11.2 is greater than 3.10.8 (This is worth noting because in the download page on python.org, a patch release for an older version may sometimes be listed above the latest patch release for a newer version). Windows The PSF provides several installation options for windows users. The first two options are for 32-bit and 64-bit versions of the python interpreter. Your default choice is likely to be the 64-bit interpreter - the 32 bit interpreter is only necessary for hardware which doesn’t support 64- bit, which nowadays is quite rare for standard desktop and laptop. The second two options are for installing offline vs. over the web. If you intend to hold onto the original installer for archive, the offline version is what you want. Else, the web installer is what you want. When you start the installer, a dialogue box will appear. There will be four clickable items in this installer; an “Install Now” button, a “Cus- tomize installation” button, a “install launcher for all users” checkbox, ¹https://www.python.org/downloads
Part I: A Whirlwind Tour 5 and a “Add python to PATH” checkbox. In order to launch python from the terminal, you will need to check the “add python to path” checkbox. With that, click “Install Now” to install Python. A secondary option to a windows install is to use Windows Subsystem for Linux. WSL is a tool for running a linux kernel along side your windows installation. While the details of this tool are outside the scope of this text, using WSL provides you a linux-like experience without forcing you away from a windows OS. With WSL installed and running in the terminal, you can read the rest of this book as if you were running python on linux. And, given that a vast majority of python projects are web servers, and those web servers run on linux, learning linux alongside python would certainly be beneficial. macOS For macOS, a universal installer is provided in the downloads page as a .pkg file. Downloading this installer and running through the installation process will install this version of python to /usr/lo- cal/bin/python3. This install will provide a separate instance of python as compared to the Apple-controlled installation which comes with your mac. This is important because you’ll need to take special care that this new installation does not take precedence over your default install, as system utilities, libraries, and programsmay rely on the default python install, and overriding the system python preferences may result in instability. Linux For linux users, installing the latest version of python can be done by compiling the source code. First, download a tarball from the downloads
Part I: A Whirlwind Tour 6 page and extract into a local directory. Next, install the build dependen- cies, and run the ./configure script to configure the workspace. Run make to compile the source code and finally use make altinstall to install the python version as python<version> (this is so your latest version doesn’t conflict with the system installation). 1 root@f26d333a183e:~# apt-get install wget tar make gcc build-esse\ 2 ntial \ 3 > gdb lcov pkg-config libbz2-dev libffi-dev libgdbm-dev libgdbm-c\ 4 ompat-dev \ 5 > liblzma-dev libncurses5-dev libreadline6-dev libsqlite3-dev lib\ 6 ssl-dev \ 7 > lzma lzma-dev tk-dev uuid-dev zlib1g-dev 8 root@f26d333a183e:~# wget https://www.python.org/ftp/python/3.11.\ 9 2/Python-3.11.2.tar.xz 10 root@f26d333a183e:~# file="Python-3.11.2.tar.xz"; tar -xvf $file \ 11 && rm $file 12 root@f26d333a183e:~# cd Python-3.11.2 13 root@f26d333a183e:~# ./configure 14 root@f26d333a183e:~# make 15 root@f26d333a183e:~# make altinstall 16 root@f26d333a183e:~# python3.11 17 Python 3.11.2 (main, Feb 13 2023, 18:44:04) [GCC 11.3.0] on linux 18 Type "help", "copyright", "credits" or "license" for more informa\ 19 tion. 20 >>> What’s in a PATH? Before moving forward, it’s worthwhile to talk a bit about the PATH variable and what it’s used for. When you type a name into the terminal, python for example, the computer looks for an executable on your
Part I: A Whirlwind Tour 7 machine that matches this name. But it doesn’t look just anywhere; it looks in a specific list of folders, and the PATH environment variable is that list. 1 root@2711ea43ad26:~# echo $PATH 2 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 3 root@2711ea43ad26:~# which python3.11 4 /usr/local/bin/python3.11 So for example, on this linux machine, the $PATH variable is a list of folders separated by a colon (on Windows the separator is a semicolon, but the concept is the same). One of those folders is the /usr/local/bin folder, which is where our python3.11 binary was installed. This means we can simply type python3.11 into our terminal, and the OS will find this binary and execute it. Setting up your dev environment Many developers have many opinions on what the best setup is for writing Python. This debate over the optimal setup is a contentious one, with many developers holding strong opinions on the matter. Some prefer a lightweight text editor like Sublime Text or Vim, while others prefer an IDE like PyCharm or Visual Studio Code. Some developers prefer a minimalist setup with only a terminal and the interpreter itself, while others prefer a more feature-rich environment with additional tools builtin for debugging and testing. Ultimately, the best setup for writing Python will vary depending on the individual developer’s needs and preferences. Some developers may find that a certain setup works best for them, while others may find that another setup is more suitable. With that being said, if you’re new to python and writing software, it might be best to keep your setup simple and focus on learning the basics
Part I: A Whirlwind Tour 8 of the language. When it comes down to it, all you really need is an interpreter and a .py file. This minimal setup allows you to focus on the core concepts of the programming language without getting bogged down by the plethora of additional tools and features. As you progress and gain more experience, you can explore more advanced tools and setups. But when starting out, keeping things simple will allow you to quickly start writing and running your own code. Running Python At this point, we should have a working python installation on our system. This means that somewhere in our file system there is a binary which, if executed, will start the python interpreter. A program that runs programs In essence, the python interpreter is a program that runs programs. The standard interpreter, the cpython interpreter, is primarily written in C (though there are other implementations of the interpreter written in different languages, like Jython written in Java and IronPython written in C#). When a Python script is executed, the interpreter reads the code line by line and performs specified actions. This allows developers to write code in a high-level language that is easy to read and understand, while the interpreter takes care of the low-level details of translating the code into machine-readable instructions. One interpreter, many modes The python interpreter can be interfaced with in a multitude of ways, and before getting into the main contents of this book, it’ll be worth
Part I: A Whirlwind Tour 9 discussing what those ways are, as this book contains many examples which you may want to run locally yourself. The first mode is called a REPL. REPL is an acronym that stands for “read, evaluate, print, loop”. These are the four stages of a cycle which python can use to collect input from a user, execute that input as code, print any particular results, and finally loop back to repeat the cycle. To enter the python REPL, execute the python binary with no arguments. 1 root@2711ea43ad26:~# python3.11 2 Python 3.11.2 (main, Feb 14 2023, 05:47:57) [GCC 11.3.0] on linux 3 Type "help", "copyright", "credits" or "license" for more informa\ 4 tion. 5 >>> Within the REPLwe can write code, submit it, and the python interpreter will execute that code and update its state accordingly. To exit the REPL, simply type exit() or press Ctrl-D. Many examples in this textbook are depicted as code which was written in the python repl. If you see three right-pointing angle brackets >>>, this is meant to represent the right pointing angle brackets of the python repl. Furthermore, any blocks of code which require multiple lines will be continuated with three dots ..., which is the default configuration of the python repl.
Comments 0
Loading comments...
Reply to Comment
Edit Comment