INDEX

Numbers and Symbols __(double underscore), using in 256 objects and 257 objects, 154–155 dunder methods, 322. See also ./, using with Ubuntu, 42 underscore (_) /? command line argument, 25–26 / (forward slash) = assignment operator, 113 purpose of, 18 == comparison operator, 113, 336 using with command line chaining, 103, 105 arguments, 25 using to compare objects, 154 # (hash mark) using with None, 94–95 using with comments, 183 != comparison operator, 336 using with docstrings, 188 * and ** syntax [] (index operator), using, 117 using to create variadic functions, ; (semicolons), using with timeit 167–171 module, 226–227 using to create wrapper functions, ' (single quote), using, 46 171–172 ~ (tilde), using in macOS, 23 using with arguments and - (unary operator), 155–157 functions, 166–167 + (unary operator), 156–157 * character, using as wildcard, 28–29 _ (underscore) ? character, using as wildcard, 28–29 PEP 8’s naming conventions, [:] syntax, using, 104 60–61 < comparison operator, 337 as prefix for methods and <= comparison operator, 337 attributes, 291–292 > comparison operator, 337 private prefix, 81 >= comparison operator, 337 using with _spaces attribute, 290 -> (arrow), using with type hints, 191 using with dunder methods, 120 \ (backslash) using with private attributes and purpose of, 18 methods, 283 using with strings, 95 using with WizCoin class, 279 : (colon), using with lists, 97–98, 104 , (comma), including in single-item A tuples, 150 abcTraceback.py program, saving, 4 - (dash), using with command line __abs__() numeric dunder arguments, 25 method, 328 $ (dollar sign), using in macOS, 23 absolute versus relative paths, 20–21 . (dot), using with commands, 31 __add__() numeric dunder method, 327 -- (double dash), using with command addToTotal() function, creating, line arguments, 25 172–173 algebra for big O, 236 base class, relationship to algorithm analysis. See big O algorithm inheritance, 296 aliases, defined, 121 Bash file, 22–23 all() function, 157–158 BFG Repo-Cleaner tool, 220 alphabetical sort, performing, 146–147 big O algorithm ALT key. See keyboard shortcuts algebra, 236 Amdahl’s Law, 229–230 algorithm analysis, 230 and operator, using, 103 analysis, 230 __and__() numeric dunder method, 328 analysis examples, 239–242 anonymous functions, 174 –175 analyzing, 243–244 answer archive, building with Stack best practice, 244 Overflow, 12 bookshelf metaphor for orders, antigravity feature, enabling, 160 231–235 API (application programming determining order for code, interface), 130 237–244 API vs. library vs. framework vs. SDK vs. doing analysis, 236 engine, 130 function calls, 242–243 append() method, using, 115 lower and higher orders, 230–231 applications, opening, 22 lower orders and coefficients, arguments. See also positional 238–239 arguments math for analysis, 236 vs. parameters, 128 measuring worst-case passing to functions, 166–167 scenario, 235 setting defaults for parameters, “n” is small, 244 142–143 order of function calls, 242–243 arrow (->), using with type hints, 191 orders of notation, 245 ASCII (American Standard Code for big Omega notation, 235 Information Interchange), 146 big Theta notation, 235 ASCII art, using in Four-in-a-Row binary code, explained, 129 game, 272 Black code formatting tool AsciiArt class, creating, 304–305 adjusting line length setting, 55 assignment and comparison operators, described, 45 chaining, 103, 105 disabling double-quoted strings atoms and literals, 110 setting, 55–56 attributes disabling for parts of code, 57 as backing fields, 317–318 installing, 54 defined, 278, 282–284 previewing changes, 56–57 vs. properties, 128–129 running from command line, turning into properties, 316–319 54–57 vs. variable, 124 semantic decisions, 58 syntactic decisions, 58 B block vs. clause vs. body, 123–124 backing field, relationship to attributes, __bool__() numeric dunder method, 328 317–318 Boolean values, 158–159. See also backporting type hints, 196 values backslash (\) Bourne Shells, 22–23 purpose of, 18 bugs, types of, 109, 150–151 using with strings, 95 bytecode vs. machine code, 129

340 Index code formatting, defined, 45 C:\ part of path, 18 code point, getting for characters, -c switch, using to run code from 146 –147 command line, 26 code smells. See also source code callables and first-class objects, 121–122 classes as functions or modules, 77 camelCase, 60 commented-out code, 74–75 casing styles, 60 dead code, 74–75 Catalina version, 23 defined, 69 cd (change directories) command, duplicate code, 70–71 29–30 empty except blocks, 79–80 __ceil__() numeric dunder error messages, 79–80 method, 328 list comprehensions, 77–79 chaining operators, 103, 105, 159–160 magic numbers, 71–73 child class, creating, 294 myths, 80–84 class attributes, 306 print debugging, 75–76 class methods, 304–306 summary, 84–85 class objects, 284 variables with numeric suffixes, 76 classes. See also inheritance codetags and TODO comments, 187 creating, 77 coercion, explained, 128 creating objects from, 278 collections module, contents of, 120 creating WizCoin, 279–284 collections.defaultdict, using for defined, 276 default values, 99–100 designing for real world, 290–291 colon (:), using with lists, 97–98, 104 as functions or modules, 77 comma (,), including in single-item “is a” relationships, 299 tuples, 150 clause vs. block vs. body, 123–124 command history, viewing, 28 CLI (command line interface), 22 command line close() and open() functions, 93–94 arguments, 24–26 cls and clear (clear terminal) options, 25 commands, 35 overview, 22–23, 42 code. See also source code running code with -c switch, 26 avoiding guesses, 90 running programs from, 23–24 beauty of, 88 running py.exe program, 26–27 commented out, 74–75 running Python programs flat vs. nested, 89 from, 26 formatting for readability, 12–13 tab completion, 27–28 implementation, 90 terminal window, 23 interrupting, 134 Command Prompt shell, 23 namespaces, 91 commands organizing, 77 canceling, 28 readability of, 89 cd (change directories), 29–30 running from command line, 26 cls and clear (clear terminal), 35 silenced errors, 89–90 copy and cp (copy files), 31–32 simplicity and complexity of, 89 del (delete files and folders), 33–34 sparse vs. dense, 89 dir (list folder contents), 30 special cases, 89 dir /s (list subfolder contents), 31 speed of, 90 find (list subfolder contents), 31 verbose and explicit, 89 ls (list folder contents), 30

Index 341 commands (continued) CPython implementation, 108 md and mkdir (make folders), 34 CTRL key. See keyboard shortcuts move and mv (move files), 32 cwd (current working directory), mv (rename files and folders), 19–20, 31 32–33 rd and rmdir (delete folders), 34–35 dash (-) using with command line ren (rename files and folders), arguments, 25 32–33 data, validating with setters, 319 rm (delete files and folders), 33–34 data types running from Python defined, 276 programs, 27 and return values, 177–178 shortened names, 32 dead code, 74–75 where (find programs), 35 decimal.Decimal(), passing integers to, which (find programs), 35 148–149 wildcard characters, 28–29 decrement and increment operators, commented-out code, 74–75 156–157 comments default arguments, setting for best practices, 197 parameters, 142–143, 165–166 myth about, 83–84 default values, using collections using, 182–188 .defaultdict for, 99–100 using with type hints, 196 deleting commit history, rewriting in Git, 220 files and folders, 33–34 commit log, viewing in Git, 216–217 files from repo, 214–215 commits, rolling back in Git, 218–220 folders, 34–35 comparing objects, 154–155 items from list, 134–140 comparison operators. See also and moving files in repo, 215–216 sequence comparisons derived class, relationship to chaining with assignment inheritance, 296 operators, 103, 105 deterministic function, 173. See also function form of, 333 functions comparisons, making, 94–95 dictionaries __complex__() numeric dunder get() and setdefault() method, 328 methods, 104 composition vs. inheritance, 299–301 key-value pairs, 118 conditional expressions, ternary Python mailing list discussion, 131 operator, 101–102 setting type hints for, 195–196 containers, defined, 119–120 using, 98–101 Cookiecutter, using to create projects, using as default arguments, 200–202 143–144 copy and cp commands, 31 diff program, seeing changes with, copy.copy() and copy.deepcopy(), using 211–212 with mutable values, 140–142 dir command, using, 30 copying dir /s command, 31 files and folders, 31 displays and literals, 110 mutable values, 140–142 __divmod__() numeric dunder cProfile profiler, 154, 228–230 method, 327 CPU, instruction set of, 129 docs folder, contents of, 200

342 Index docstrings errors, preventing with linters, 8–9 defined, 182 exceptions summary, 197 catching, 79–80 using, 188–190 raising, 4, 90, 178–179 dollar sign ($), using in macOS, 23 RecursionError, 318–319 doskey program, 28 explanatory comments, 184–185 dot (.), using with commands, 31 expressions vs. statements, 122–123 double dash (--), using with command line arguments, 25 F double-free bugs, 109 False and True keywords, 158–159 double underscore (__), using in FAQs (Frequently Asked Questions), 10 dunder methods, 322. See also filenames, as command line underscore (_) arguments, 25 dunder methods. See also methods; OOP filenames and folders, matching with (object-oriented programming) wildcards, 28–29 comparison, 332–337 file paths, specifying, 20–21 defined, 120 files numeric, 325–328 copying, 31 in-place augmented assignment, deleting, 33–34 330–332 moving, 32 reflected numeric, 328–330 renaming, 32–33 string representation, 323–325 filesystem, 18–21, 42 using, 322–323 filtering with list comprehensions, duplicate code, 70–71 175 –176 dynamic typing, 190 find command, 31 find feature, accessing, 64, 67 E find() string method, error related Easter egg, The Zen of Python as, 88 to, 178 encapsulation, explained, 307–308 finding programs, 35 encoding definition, using with magic first-class objects and callables, comment, 187–188 121–122. See also objects engine vs. library vs. framework vs. SDK flag arguments, myth about, 82 vs. API, 130 __float__() numeric dunder enumerate() vs. range(), 92–93, 104 method, 328 environment setup, process of, 17, 42 floating-point numbers, accuracy of, environment variables and PATH, 35–39 147–149, 151 __eq__() comparison dunder __floor__() numeric dunder method, 336 method, 328 EQual operation, 336 __floordiv__() numeric dunder equality (==) operator, using with method, 327 None, 94–95 folders error codes, returning, 178–179 adding to PATH on macOS and error messages Linux, 39 and except blocks, 79–80 adding to PATH on Windows, 38–39 getting help with, 11 as command line arguments, 25 parsing, 15 copying, 31 tracing, 178 deleting, 33–34 understanding, 4–8 in filesystem, 18

Index 343 folders (continued) names, 162 home directory, 19 parameters and arguments, listing contents of, 30 165 –172 making, 34 passing arguments to, 166–167 moving, 32 and return statements, 80–81 renaming, 32–33 size trade-offs, 162–165 folders and filenames, matching with and try statements, 81–82 wildcards, 28–29 using default arguments with, for expressions, including in list 142–143 comprehension, 79 for loops G in big O analysis, 240 garbage collection, 109, 226 getting index and value, 104 __ge__() comparison dunder and lists, 134–140 method, 337 versatility of, 125 get(), using with dictionaries, 98–101 form, filling out, 276–278 getPlayerMove() function, 163–165 format() string method, 96–97 getters and setters, 315, 318 formatting for readability, 58 Git. See also repo formatting strings, 96–97 adding files to track, 208–209 forward slash (/) command line tool, 207 purpose of, 18 commits and repos, 200, 206–207 using with command line committed state, 204 arguments, 25 committing changes, 210–214 Four-in-a-Row tile-dropping game configuring username and output, 259–260 email, 203 source code, 260–264 deleting files from repo, 214–215 summary, 271–272 as distributed version control writing code, 264–271 system, 206 frame object, explained, 5 frequency of committing changes, frame summary, explained, 5 213–214 framework vs. library vs. SDK vs. engine ignoring files in repo, 209–210 vs. API, 130 installing, 202–204 f-strings, formatting strings with, 96–97 keeping track of file status, functional programming 204–206 higher-order functions, 174 log command, 216–217 lambda functions, 174 –175 modified state, 204 list comprehensions, 175–176 recovering old changes, 217–220 mapping and filtering, 175–176 renaming and moving files in side effects, 172–174 repo, 215–216 function calls, order in big O, 242–243 rewriting commit history, 220 functions. See also deterministic running status with watch function; higher-order command, 207 functions; nondeterministic staged state, 205–206 function; pure function; storing private information in, 220 variadic functions; wrapper viewing changes before functions committing, 211–212 default arguments, 165–166 viewing commit log, 216–217 vs. methods, 82, 124 workflow, 204–206

344 Index git add command, 223 indentation, using space characters git clone command, 223 for, 47–48. See also significant git commit command, 223 indentation git diff, using, 211–213 index operator ([]), using, 117–118 git filter-branch command, 220 index() string method, exception git init command, 223 related to, 179 GitHub and git push command, indexes, defined, 117–119 221–223 inequality != operators, avoiding glob patterns, explained, 29 chaining, 149–150 global variables, myth about, 82–83 inheritance. See also classes; glossary, accessing, 108, 131 multiple inheritance; GrandchildClass, creating, 294–295 OOP (object-oriented Greater Than operation, 337 programming) Greater than or Equal operation, 337 base classes, 296 __gt__() comparison dunder best practice, 308–309 method, 337 class attributes, 306–307 GUI (graphical user interface), 22 class methods, 304–306 GUI Git tools, installing, 203–204 vs. composition, 299–301 creating child classes, 294 H derived classes, 296 hash mark (#) downside, 301–303 using with comments, 183 explained, 293 using with docstrings, 188 isinstance() and issubclass() hashes, defined, 117–119 functions, 303–304 --help command line argument, 25–26 MRO (method resolution order), help with programming, asking for, 310–312 9–14 overriding methods, 296–297 higher-order functions, 174. See also static methods, 306–307 functions subclasses, 296 home directory, 19 super classes, 296 Homebrew, installing and super() function, 297–299 configuring, 213 __init__(), and self, 280–282 horizontal spacing, 47–51 __init__.py file and packages, 121 Hungarian notation, 63 inline comments, 183–184 in-place augmented assignment dunder I methods, 330–332 id() function, calling, 111, 154 installing identifiers, defined, 59 Black code formatting tool, 54 identities, defined, 111–114 Git, 202–204 IEEE 754 standard and floating-point Homebrew, 213 numbers, 147–148 Meld for Linux, 213 if statement as clause header, 124 Mypy, 192–193 immutable and mutable objects, Pyflakes, 9 114 –117, 144 tkdiff, 213 in operator, using with values of instances, defined, 111–114, 276. See variables, 105 also isinstance() increment and decrement operators, instruction set, explained, 129 156–157 int() function, using, 158

Index 345 __int__() numeric dunder method, 328 __le__() comparison dunder integers, passing to decimal.Decimal(), method, 337 148–149. legal comments, 186 __invert__() numeric dunder len() function, using, 92 method, 328 Less Than operation, 337 “is a” relationships for classes, 299 Less than or Equal operation, 337 is operator, using, 113 “lessons learned” comments, 185–186 isinstance(). See also instances library vs. framework vs. SDK vs. engine and issubclass() functions, vs. API, 130. See also Python 303–304, 312 Standard Library using with Boolean values, 158 LICENSE.txt file, 200 items links to URLs, including in best practices for dealing with, comments, 183 134 –140 linters, preventing errors with, 8–9, 15 defined, 114 Linux iterable unpacking, using to swap installing Meld for, 213 variables, 227 running Python programs on, 41 iterable vs. iterator, 125–126 list comprehensions iterating and all() function, 157 explained, 134 mapping and filtering with, forward and backward, 139 175 –176 using, 77–79, 137 J list concatenation, 115 JDK (Java Development Kit), 130 lists join() operator, using with adding or deleting items from, strings, 151 134 –140 JVM (Java Virtual Machine), 129 best practices for dealing with, 134 –140 K contents of, 141–142 keyboard shortcuts making shallow copies of, 97–98 canceling commands, 28 setting type hints for, 195–196 find feature, 64, 67 using as default arguments, interrupting code, 134 143–144 interrupting infinite loops, 134 literals, defined, 109–110 opening applications, 22 logfiles, setting up, 75–76 opening terminal window, 23, 41 logical error, defined, 127 Task Manager, 22 looping, explained, 134 viewing running processes, 22 loops keys, defined, 117–119 interrupting, 134 keywords moving duplicate code into, 71 arguments, 167 ls command, using, 30 defined, 110–111 __lshift__() numeric dunder True and False, 158–159 method, 328 Kompare, using, 213 __lt__() comparison dunder method, 337 L lambda functions, 174–175. See also M functions machine code vs. bytecode, 129

346 Index macOS multiple inheritance, 309–310. See also installing tkdiff, 213 inheritance running Python programs on, 41 mutable and immutable objects, magic comments and source file 114–117, 151 encoding, 187–188 mutable values magic methods, defined, 120 best practices for dealing with, magic numbers, 71–73 142–144 main() function, changing to override copying, 140–144 methods, 296–297 and default arguments, 142–144 mapping. See also objects Mypy, using, 192–194 defined, 119–120 and filtering with list N comprehensions, 175–176 name length, considering, 61–64 mapping data types, passing, 167 nameless functions, 174–175 math dunder methods, 325–328 names. __matmul__() numeric dunder advice about, 64–65 method, 327 avoiding overwriting, 65–66 max() and min() functions, 169 choosing, 67 MCR (minimum, complete, making searchable, 64 reproducible) example, 11 prefixes in, 63–64 md and mkdir commands, 34 sequential numeric suffixes in, 64 Meld, installing for Linux, 213 namespaces, 90–91 memory leaks, 109 __ne__() comparison dunder memory usage, 137–138 method, 336 metasyntactic variables, 60. See also __neg__() numeric dunder method, 328 variables nested conditional expressions, 102 methods. See also dunder methods; nested loops, using in big O private attributes and private analysis, 241 methods next() function, calling, 126 vs. functions, 82, 124 no operation, explained, 74 __init__(), and self, 280–282 nondeterministic function, 173. See also overriding, 296–297 functions min() and max() functions, 169 None, using == (equality) operator with, __mod__() numeric dunder method, 327 94–95 modules Not Equal operation, 336 defined, 120–121 NotImplementedError, raising, 75 finding, 14 numbers, magic, 71–73. and packages, 120–121 numeric dunder methods, 325–328 requests, 188–189 typing, 195–196 O move and mv (move files) commands, 32 O(1), Constant time, 231–232 moving files and folders, 32 objects. See also mapping MRO (method resolution order), and classes, 276 310–312 comparing, 154–155, 334 __mul__() numeric dunder creating from classes, 278 method, 327 defined, 111–114 multiple assignment trick, using to mutable and immutable, 114–117 swap variables, 227 sorting, 336

Index 347 O(log n), Logarithmic, 232 paths, specifying, 18–21 O(n!), Factorial Time, 234–235 p-code, explained, 129 O(n), Linear Time, 232 PEP (Python Enhancement O(n log n), N-Log-N Time, 232–233 Proposal) 8 O(n2), Polynomial Time, 233 documentation, 67 O(n11), Exponential Time, 233–234 naming conventions, 61 OOP (object-oriented programming). and style guides, 46–47 See also dunder methods; , 90 inheritance pip list, running, 14 creating objects from classes, 278 polymorphism, explained, 308 defined, 275 portable code, explained, 129 designing classes, 290–291 porting vs. backporting, 196 encapsulation, 307–308 __pos__() numeric dunder filling out form, 276–278 method, 328 and non-OOP examples, 285–290 positional arguments, defined, 166–167. polymorphism, 308 See also arguments properties, 316–322 __pow__() numeric dunder method, 328 summary, 292 practice projects. See also projects tic-tac-toe, 285–290 Four-in-a-Row, 259–271 type() function and __ The Tower of Hanoi, 248–259 qualname__attribute, 284–285 preallocated integers, 154. using class and static features, 307 premature optimization, 226 WizCoin class, 279–284 print debugging, 75–76 open() print() function and close() functions, 93–94 arguments for, 168 and readlines() functions, 126 passing list to, 166 operator module, 333, 336–337 using with wrapper functions, 171 operators, chaining, 103, 105, 151, private attributes and private methods, 159–160 282–284. See also methods optimizations processes and programs, 21–22 preallocated integers, 154 professional comments, 186 string interning, 155 profiling, explained, 228 __or__() numeric dunder method, 328 program vs. script, 129–130 ordinal, getting for characters, 146–147 programming help, asking for, 9–14 os.chdir(), using, 20 programming language vs. scripting language, 129–130 P programs. See also Python programs packages finding, 35 defined, 120–121 and processes, 21–22 and modules, 120–121 running from command line, parameters vs. arguments, 128 23–24, 26 ParentClass, creating, 294–295 running without command line, PascalCase, 60 39–42 pass statement vs. scripts, 129–130 relationship to stubs, 74–75 project folder, contents of, 200 using with except block, 79–80 projects, creating with Cookiecutter, PATH and environment variables, 35–39 200–202. See also practice pathlib module, importing, 18–19 projects

348 Index properties read-only properties, 320–321 vs. attributes, 128–129 RecursionError exception, raising, best practices, 322 318–319 read-only, 320–321 references, explained, 137–138 turning attributes into, 316–319 reflected numeric dunder methods, using, 316 328–330 public access attributes and methods, 283 relative vs. absolute paths, 20–21 pure function, 173–174. See also renaming files and folders, 32–33 functions repo. See also Git push command, using in Git, 221–223 cloning for GitHub repo, 222–223 .py source code files, locating, 200 creating, 223 .pyc files, bytecode in, 129 creating on computer, 206–207 py.exe program, running, 26–27 deleting and moving files in, Pyflakes, installing, 9 215–216 PyPy just-in-time , 108 deleting files from, 214–215 Python ignoring files in, 209–210 documentation, 121 and version control systems, 200 error messages, 4–8 __repr__() method, using, 325 glossary, 108, 131 repr string, sensitive information language and interpreter, 108–109 in, 325 programming language, 109 requests module, sessions.py file, Python programs, running without 188–189 command line, 39–42. See also return values and data types, 177–178. programs; The Zen of Python See also values Python Standard Library, 120–121. See __rfloordiv__() reflected numeric also library vs. framework vs. dunder method, 330 SDK vs. engine vs. API __rlshift__() reflected numeric pythonic code, core of, 104 dunder method, 330 rm (removing files and folders) Q command, 33–34 __qualname__attribute and type() __rmatmul__() reflected numeric function, 284–285 dunder method, 330 questions, asking, 10–11, 14–15 __rmod__() reflected numeric dunder method, 330 R __rmul__() reflected numeric dunder __radd__() reflected numeric dunder method, 330 method, 330 roll back, performing in Git, 217–220 raising exceptions, 90, 178–179 root folder, explained, 18 __rand__() reflected numeric dunder __ror__() reflected numeric dunder method, 330 method, 330 range() vs. enumerate(), 92–93, 103–104 __round__() numeric dunder rd and rmdir commands, 34–35 method, 328 __rdivmod__() reflected numeric __rpow__() reflected numeric dunder dunder method, 330 method, 330 readlines() and open() functions, __rrshift__() reflected numeric using, 126 dunder method, 330 README files, 200, 211–212, __rshift__() numeric dunder 215–216, 218 method, 328

Index 349 __rsub__() reflected numeric dunder software license, file for, 200 method, 330 sort() function, behavior of, 146–147, __rtruediv__() reflected numeric 151, 332, 336 dunder method, 330 source code, avoiding dropping letters running processes, viewing, 22 from, 62. See also code; code runtime smells defined, 226 source file encoding and magic quickening for functions, 173 comments, 187–188 vs. syntax vs. semantic errors, space characters, rendering on 126–127 screen, 47 __rxor__() reflected numeric dunder spacing within lines, 48–51 method, 330 Stack Overflow, building answer archive, 12 S stack trace, 4–7 %s conversion specifiers, using, 96–97 staged files script vs. program, 129–130 committing, 211 scripting language vs. programming unstaging in Git, 218 language, 129–130 statements vs. expressions, 122–123 SDK vs. library vs. framework vs. engine static analysis, explained, 8, 192–194 vs. API, 130 static methods, 306–307 self and __init__(), 280–282 string concatenation, 144–146, 151 semantic vs. syntax vs. runtime errors, string interning, 155–156 126–127 strings semicolons (;), using with timeit formatting, 95–97 module, 227 as immutable objects, 144 sensitive information in repr strings, 325 immutable quality of, 116 sequence comparisons, 335–336. See interpolating, 104 also comparison operators stubs, relationship to code smells, 74 sequences style guides and PEP (Python defined, 119–120 Enhancement Proposal) 8, and iterables, 125 46–47 sessions.py file inrequests module, __sub__() numeric dunder method, 327 188–189 subclass, relationship to inheritance, set types, defined, 119–120 296. See also isinstance() setdefault(), using with dictionaries, subfolders, listing contents of, 31 98–100 Sublime Text editor, 193 setters subprocess.run() function, 27 and getters, 315, 318 subtract() function, creating, 172 using to validate data, 319–320 sum() function, 168 sh file, 22–23 summary comments, 185 shell programs, 22–23 super class, relationship to i side effects, 172–174 nheritance, 296 significant indentation, 91–92, 104.See super() function, relationship to also indentation overriding method, 297–299 single quote ('), using, 46 switch statement vs. dictionaries, slice syntax, explained, 97 100–101 snake_case, 60 syntax snapshots, saving with Git, 200 catching errors, 6

350 Index misuse of, 92–95 type hints, 182, 190–196 vs. runtime vs. semantic errors, 58, types, defined, 276 126–127 typing, minimizing with tab sys.getsizeof() function, 137–138 completion, 27–28 system environment variables, 38 typing module, 195–196 T U tab completion, 27–28 Ubuntu Linux, running Python Task Manager, opening, 22 programs on, 41–42 terminal window underscore (_). See also double clearing, 35 underscore (__) opening, 23, 41 PEP 8’s naming conventions, 60–61 ternary operator, 101–102 as prefix for methods and tests folder, contents of, 200 attributes, 291–292 text editor, Sublime Text, 193 private prefix, 81 tic-tac-toe program using with _spaces attribute, 290 creating, 285–290 using with dunder methods, 120 MRO (method resolution order), using with private attributes and 311–312 methods, 283 tilde (~), using in macOS, 23 using with WizCoin class, 279 timeit module, using to measure undo features, 199, 217–220 performance, 226–228. See Unicode resource, 188 also modules unit tests, folder for, 200 time.time() function, 72, 227 Unix operating system, shell programs, tkdiff, installing on macOS, 213 22–23 TODO comments and codetags, 187 URL links, including in comments, 183 The Tower of Hanoi puzzle user environment variables, 38 getPlayerMove() function, 163, 165, UTF-8 encoding, 187–188 254–257, 268 output, 249–250 V restrictions, 248 validating data using setters, 319–320 source code, 250–252 values. See also Boolean values; return summary, 271–272 values and data types writing code, 252–259 defined, 111–114 tracebacks, examining, 4–7 modifying in place, 115 True and False keywords, 158–159 variable names, 64, 66. See also names __truediv__() numeric dunder variable values, 103–104 method, 327 variables. See also metasyntactic variables __trunc__() numeric dunder method, vs. attributes, 124 328 box vs. label metaphor, 112–113 tuples checking values, 103–104 identities, 119 with numeric suffixes, 76 immutable quality of, 116–117 swapping, 227 using commas with, 150 variadic functions, creating, 167–171. values of, 116 See also functions type coercion vs. type casting, 128 version control systems, 199–200 type() function and __ vertical spacing, 51–53 qualname__attribute, 284–285 volumes, explained, 18

Index 351 W worst-case scenario, measuring with Big O, 235 watch command, using with Git, 207 wrapper functions, creating, 171–172. webbrowser module, 160 See also functions where command, 35 which command, 35 while keyword, 110 X while loops XOR algorithm, using, 226–227 in big O analysis, 241 __xor__() numeric dunder method, 328 and lists, 134–140 wildcard characters, 28–29 Z Windows, running Python programs Zakharenko, Nina, 131 on, 40–41 The Zen of Python, 88–91. See also WinMerge, downloading, 212–213 programs; Python programs with statement, 93–94 zero-based indexing, using, 117 WizCoin class, creating, 279–284 Zsh and Z shells, 23

352 Index