Please note that index links to approximate location of each term.
Numbers and Symbols
256 objects and 257 objects, 154–155
./
, using with Ubuntu, 42
/?
command line argument, 25–26
=
assignment operator, 113
==
comparison operator, 113, 336
chaining, 105
using to compare objects, 154
using with None
, 96
!=
comparison operator, 336
*
and **
syntax
using to create variadic functions, 167–171
using to create wrapper functions, 171–172
using with arguments and functions, 166–167
*
character, using as wildcard, 28–29
?
character, using as wildcard, 28–29
[:]
syntax, using, 105
<
comparison operator, 337
<=
comparison operator, 337
>
comparison operator, 337
>=
comparison operator, 337
->
(arrow), using with type hints, 191
(backslash)
purpose of, 18
using with strings, 97
:
(colon), using with lists, 98–99, 105
,
(comma), including in single-item tuples, 150
-
(dash), using with command line arguments, 25
$
(dollar sign), using in macOS, 23
.
(dot), using with commands, 31
--
(double dash), using with command line arguments, 25
__
(double underscore), using in dunder methods, 322. See also underscore (_
)
/
(forward slash)
purpose of, 18
using with command line arguments, 25
#
(hash mark
using with comments, 183
using with docstrings, 188
[]
(index operator), using, 117
;
(semicolons), using with timeit
module, 226–227
' (single quote), using, 46
~
(tilde), using in macOS, 23
_
(underscore)
PEP 8’s naming conventions, 60–61
as prefix for methods and attributes, 291–292
private prefix, 81
using with _spaces
attribute, 290
using with dunder methods, 120
using with private attributes and methods, 283
using with WizCoin
class, 279
A
abcTraceback.py program, saving, 4
__abs__()
numeric dunder method, 328
absolute versus relative paths, 20–21
__add__()
numeric dunder method, 327
addToTotal()
function, creating, 172–173
algebra for big O, 236
algorithm analysis. See big O algorithm
aliases, defined, 121
alphabetical sort, performing, 146–147
ALT key. See keyboard shortcuts
and
operator, using, 104
__and__()
numeric dunder method, 328
answer archive, building with Stack Overflow, 12
antigravity feature, enabling, 160
API (application programming interface), 130
API vs. library vs. framework vs. SDK vs. engine, 130
append()
method, using, 115
applications, opening, 22
arguments. See also positional arguments
vs. parameters, 128
setting defaults for parameters, 142–143
arrow (->
), using with type hints, 191
ASCII (American Standard Code for Information Interchange), 146
ASCII art, using in Four-in-a-Row game, 272
AsciiArt
class, creating, 304–305
assignment and comparison operators, chaining, 104–105
atoms and literals, 110
attributes
turning into properties, 316–319
vs. variable, 124
B
backing field, relationship to attributes, 317–318
backporting type hints, 196
backslash ()
purpose of, 18
using with strings, 97
base class, relationship to inheritance, 296
BFG Repo-Cleaner tool, 220
big O algorithm
algebra, 236
algorithm analysis, 230
analysis, 230
best practice, 244
bookshelf metaphor for orders, 231–235
determining order for code, 237–244
doing analysis, 236
lower and higher orders, 230–231
lower orders and coefficients, 238–239
math for analysis, 236
measuring worst-case scenario, 235
“n” is small, 244
order of function calls, 242–243
orders of notation, 245
big Omega notation, 235
big Theta notation, 235
binary code, explained, 129
Black code formatting tool
adjusting line length setting, 55
described, 45
disabling double-quoted strings setting, 55–56
disabling for parts of code, 57
installing, 54
running from command line, 54–57
semantic decisions, 58
syntactic decisions, 58
block vs. clause vs. body, 123–124
__bool__()
numeric dunder method, 328
Boolean values, 158–159. See also values
bytecode vs. machine code, 129
C
C:
part of path, 18
-c
switch, using to run code from command line, 26
callables and first-class objects, 121–122
camelCase, 60
casing styles, 60
Catalina version, 23
cd
(change directories) command, 29–30
__ceil__()
numeric dunder method, 328
chaining operators, 105, 159–160
child class, creating, 294
class attributes, 306
class objects, 284
classes. See also inheritance
creating, 77
creating objects from, 278
defined, 276
designing for real world, 290–291
as functions or modules, 77
“is a” relationships, 299
clause vs. block vs. body, 123–124
CLI (command line interface), 22
close()
and open()
functions, 95–96
cls
and clear
(clear terminal) commands, 35
code. See also source code
avoiding guesses, 90
beauty of, 88
flat vs. nested, 89
formatting for readability, 12–13
implementation, 90
interrupting, 134
namespaces, 91
organizing, 77
readability of, 89
running from command line, 26
simplicity and complexity of, 89
sparse vs. dense, 89
special cases, 89
speed of, 90
verbose and explicit, 89
code formatting, defined, 45
code point, getting for characters, 146–147
code smells. See also source code
classes as functions or modules, 77
defined, 69
variables with numeric suffixes, 76
codetags and TODO comments, 187
coercion, explained, 128
collections module, contents of, 120
collections.defaultdict
, using for default values, 101
colon (:
), using with lists, 98–99, 105
comma (,
), including in single-item tuples, 150
command history, viewing, 28
command line
options, 25
running code with -c
switch, 26
running Python programs from, 26
terminal window, 23
Command Prompt shell, 23
commands
canceling, 28
cd
(change directories), 29–30
cls
and clear
(clear terminal), 35
copy
and cp
(copy files), 31–32
del
(delete files and folders), 33–34
dir
(list folder contents), 30
dir /s
(list subfolder contents), 31
find
(list subfolder contents), 31
ls
(list folder contents), 30
md
and mkdir
(make folders), 34move
and mv
(move files), 32
mv
(rename files and folders), 32–33
rd
and rmdir
(delete folders), 34–35
ren
(rename files and folders), 32–33
rm
(delete files and folders), 33–34
running from Python programs, 27
shortened names, 32
where
(find programs), 35
which
(find programs), 35
comments
best practices, 197
using with type hints, 196
commit history, rewriting in Git, 220
commit log, viewing in Git, 216–217
commits, rolling back in Git, 218–220
comparison operators. See also sequence comparisons
chaining with assignment operators, 104–105
function form of, 333
comparisons, making, 96
__complex__()
numeric dunder method, 328
composition vs. inheritance, 299–301
conditional expressions, ternary operator, 102–104
Cookiecutter, using to create projects, 200–202
copy
and cp
commands, 31
copy.copy()
and copy.deepcopy()
, using with mutable values, 140–142
copying
files and folders, 31
cProfile profiler, 154, 228–230
CPU, instruction set of, 129
CPython implementation, 108
CTRL key. See keyboard shortcuts
cwd
(current working directory), 19–20, 31
D
dash (-
) using with command line arguments, 25
data, validating with setters, 319
data types
defined, 276
decimal.Decimal()
, passing integers to, 148–149
decrement and increment operators, 156–157
default arguments, setting for parameters, 142–143, 165–166
default values, using collections.defaultdict
for, 101
deleting
and moving files in repo, 215–216
derived class, relationship to inheritance, 296
deterministic function, 173. See also functions
dictionaries
get()
and setdefault()
methods, 105
key-value pairs, 118
Python mailing list discussion, 131
setting type hints for, 195–196
using as default arguments, 143–144
diff program, seeing changes with, 211–212
dir
command, using, 30
dir /s
command, 31
displays and literals, 110
__divmod__()
numeric dunder method, 327
docs folder, contents of, 200
docstrings
defined, 182
summary, 197
dollar sign ($
), using in macOS, 23
doskey program, 28
dot (.
), using with commands, 31
double dash (--
), using with command line arguments, 25
double-free bugs, 109
double underscore (__
), using in dunder methods, 322. See also underscore (_
)
dunder methods. See also methods; OOP (object-oriented programming)
defined, 120
in-place augmented assignment, 330–332
string representation, 323–325
dynamic typing, 190
E
Easter egg, The Zen of Python as, 88
encapsulation, explained, 307–308
encoding definition, using with magic comment, 187–188
engine vs. library vs. framework vs. SDK vs. API, 130
enumerate()
vs. range()
, 94–95, 105–105
environment setup, process of, 17, 42
environment variables and PATH, 35–39
__eq__()
comparison dunder method, 336
EQual operation, 336
equality (==
) operator, using with None, 96
error codes, returning, 178–179
error messages
getting help with, 11
parsing, 15
tracing, 178
errors, preventing with linters, 8–9
exceptions
expressions vs. statements, 122–123
F
False
and True
keywords, 158–159
FAQs (Frequently Asked Questions), 10
filenames, as command line arguments, 25
filenames and folders, matching with wildcards, 28–29
files
copying, 31
moving, 32
filtering with list comprehensions, 175–176
find
command, 31
find feature, accessing, 64, 67
find()
string method, error related to, 178
finding programs, 35
first-class objects and callables, 121–122. See also objects
flag arguments, myth about, 82
__float__()
numeric dunder method, 328
floating-point numbers, accuracy of, 147–149, 151
__floor__()
numeric dunder method, 328
__floordiv__()
numeric dunder method, 327
folders
adding to PATH
on macOS and Linux, 39
adding to PATH
on Windows, 38–39
as command line arguments, 25
copying, 31
in filesystem, 18
home directory, 19
listing contents of, 30
making, 34
moving, 32
folders and filenames, matching with wildcards, 28–29
for
expressions, including in list comprehension, 79
for
loops
in big O analysis, 240
getting index and value, 105–105
versatility of, 125
formatting for readability, 58
forward slash (/
)
purpose of, 18
using with command line arguments, 25
Four-in-a-Row tile-dropping game
frame object, explained, 5
frame summary, explained, 5
framework vs. library vs. SDK vs. engine vs. API, 130
f-strings, formatting strings with, 97–98
functional programming
higher-order functions, 174
mapping and filtering, 175–176
function calls, order in big O, 242–243
functions. See also deterministic function; higher-order functions; nondeterministic function; pure function; variadic functions; wrapper functions
names, 162
parameters and arguments, 165–172
using default arguments with, 142–143
G
__ge__()
comparison dunder method, 337
get()
, using with dictionaries, 99–101
getPlayerMove()
function, 163–165
Git. See also repo
adding files to track, 208–209
command line tool, 207
commits and repos, 200, 206–207
committed state, 204
configuring username and email, 203
deleting files from repo, 214–215
as distributed version control system, 206
frequency of committing changes, 213–214
ignoring files in repo, 209–210
keeping track of file status, 204–206
modified state, 204
recovering old changes, 217–220
renaming and moving files in repo, 215–216
rewriting commit history, 220
running status with watch
command, 207
storing private information in, 220
viewing changes before committing, 211–212
git
add
command, 223
git
clone
command, 223
git
commit
command, 223
git filter-branch
command, 220
git init
command, 223
GitHub and git push
command, 221–223
glob patterns, explained, 29
global variables, myth about, 82–83
GrandchildClass
, creating, 294–295
Greater Than operation, 337
Greater than or Equal operation, 337
__gt__()
comparison dunder method, 337
GUI (graphical user interface), 22
GUI Git tools, installing, 203–204
H
hash mark (#
)
using with comments, 183
using with docstrings, 188
--help
command line argument, 25–26
help with programming, asking for, 9–14
higher-order functions, 174. See also functions
home directory, 19
Homebrew, installing and configuring, 213
Hungarian notation, 63
I
id()
function, calling, 111, 154
identifiers, defined, 59
IEEE 754 standard and floating-point numbers, 147–148
if
statement as clause header, 124
immutable and mutable objects, 114–117, 144
in
operator, using with values of variables, 105
increment and decrement operators, 156–157
indentation, using space characters for, 47–48. See also significant indentation
index operator ([]
), using, 117–118
index()
string method, exception related to, 179
inequality !=
operators, avoiding chaining, 149–150
inheritance. See also classes; multiple inheritance; OOP (object-oriented programming)
base classes, 296
creating child classes, 294
derived classes, 296
explained, 293
isinstance()
and issubclass()
functions, 303–304
MRO (method resolution order), 310–312
subclasses, 296
super classes, 296
__init__.py file and packages, 121
in-place augmented assignment dunder methods, 330–332
installing
Black code formatting tool, 54
Homebrew, 213
Meld for Linux, 213
Pyflakes, 9
tkdiff, 213
instances, defined, 111–114, 276. See also isinstance()
instruction set, explained, 129
int()
function, using, 158
__int__()
numeric dunder method, 328
integers, passing to decimal.Decimal()
, 148–149.
__invert__()
numeric dunder method, 328
“is a” relationships for classes, 299
is
operator, using, 113
isinstance()
. See also instances
and issubclass()
functions, 303–304, 312
using with Boolean values, 158
items
best practices for dealing with, 134–140
defined, 114
iterable unpacking, using to swap variables, 227
iterable vs. iterator, 125–126
iterating
explained, 134
forward and backward, 139
J
JDK (Java Development Kit), 130
join()
operator, using with strings, 151
JVM (Java Virtual Machine), 129
K
keyboard shortcuts
canceling commands, 28
interrupting code, 134
interrupting infinite loops, 134
opening applications, 22
opening terminal window, 23, 41
Task Manager, 22
viewing running processes, 22
keywords
arguments, 167
Kompare, using, 213
L
lambda functions, 174–175. See also functions
__le__()
comparison dunder method, 337
legal comments, 186
len()
function, using, 94
Less Than operation, 337
Less than or Equal operation, 337
“lessons learned” comments, 185–186
library vs. framework vs. SDK vs. engine vs. API, 130. See also Python Standard Library
LICENSE.txt
file, 200
links to URLs, including in comments, 183
linters, preventing errors with, 8–9, 15
Linux
installing Meld for, 213
running Python programs on, 41
list comprehensions
and all()
function, 157
mapping and filtering with, 175–176
list concatenation, 115
lists
adding or deleting items from, 134–140
best practices for dealing with, 134–140
making shallow copies of, 98–99
setting type hints for, 195–196
using as default arguments, 143–144
logical error, defined, 127
looping, explained, 134
loops
interrupting, 134
moving duplicate code into, 71
ls
command, using, 30
__lshift__()
numeric dunder method, 328
__lt__()
comparison dunder method, 337
M
machine code vs. bytecode, 129
macOS
installing tkdiff, 213
running Python programs on, 41
magic comments and source file encoding, 187–188
magic methods, defined, 120
main()
function, changing to override methods, 296–297
mapping. See also objects
and filtering with list comprehensions, 175–176
mapping data types, passing, 167
__matmul__()
numeric dunder method, 327
max()
and min()
functions, 169
MCR (minimum, complete, reproducible) example, 11
md
and mkdir
commands, 34
Meld, installing for Linux, 213
memory leaks, 109
metasyntactic variables, 60. See also variables
methods. See also dunder methods; private attributes and private methods
min()
and max()
functions, 169
__mod__()
numeric dunder method, 327
modules
finding, 14
move
and mv
(move files) commands, 32
moving files and folders, 32
MRO (method resolution order), 310–312
__mul__()
numeric dunder method, 327
multiple assignment trick, using to swap variables, 227
multiple inheritance, 309–310. See also inheritance
mutable and immutable objects, 114–117, 151
mutable values
best practices for dealing with, 142–144
and default arguments, 142–144
N
name length, considering, 61–64
names.
choosing, 67
making searchable, 64
sequential numeric suffixes in, 64
__ne__()
comparison dunder method, 336
__neg__()
numeric dunder method, 328
nested conditional expressions, 104
nested loops, using in big O analysis, 241
next()
function, calling, 126
no operation, explained, 74
nondeterministic function, 173. See also functions
None
, using ==
(equality) operator with, 96
Not Equal operation, 336
NotImplementedError
, raising, 75
numeric dunder methods, 325–328
O
objects. See also mapping
and classes, 276
creating from classes, 278
mutable and immutable, 114–117
sorting, 336
O(log n)
, Logarithmic, 232
O(n!)
, Factorial Time, 234–235
O(n)
, Linear Time, 232
O(n log n)
, N-Log-N Time, 232–233
O(n2)
, Polynomial Time, 233
O(n11)
, Exponential Time, 233–234
OOP (object-oriented programming). See also dunder methods; inheritance
creating objects from classes, 278
defined, 275
polymorphism, 308
summary, 292
type()
function and __qualname__
attribute, 284–285
using class and static features, 307
open()
and readlines()
functions, 126
operators, chaining, 105, 151, 159–160
optimizations
preallocated integers, 154
string interning, 155
__or__()
numeric dunder method, 328
ordinal, getting for characters, 146–147
os.chdir()
, using, 20
P
packages
parameters vs. arguments, 128
ParentClass
, creating, 294–295
PascalCase
, 60
pass
statement
using with except
block, 79–80
PATH
and environment variables, 35–39
pathlib
module, importing, 18–19
p-code, explained, 129
PEP (Python Enhancement Proposal) 8
documentation, 67
naming conventions, 61
Perl programming language, 90
pip list
, running, 14
polymorphism, explained, 308
portable code, explained, 129
porting vs. backporting, 196
__pos__()
numeric dunder method, 328
positional arguments, defined, 166–167. See also arguments
__pow__()
numeric dunder method, 328
practice projects. See also projects
preallocated integers, 154.
premature optimization, 92, 226
print()
function
arguments for, 168
passing list to, 166
using with wrapper functions, 171
private attributes and private methods, 282–284. See also methods
professional comments, 186
programming help, asking for, 9–14
programming language vs. scripting language, 129–130
programs. See also Python programs
finding, 35
running from command line, 23–24, 26
running without command line, 39–42
project folder, contents of, 200
projects, creating with Cookiecutter, 200–202. See also practice projects
properties
best practices, 322
turning attributes into, 316–319
using, 316
public access attributes and methods, 283
pure function, 173–174. See also functions
push
command, using in Git, 221–223
.py source code files, locating, 200
.pyc files, bytecode in, 129
py.exe program, running, 26–27
Pyflakes, installing, 9
PyPy just-in-time compiler, 108
Python
documentation, 121
language and interpreter, 108–109
programming language, 109
Python programs, running without command line, 39–42. See also programs; The Zen of Python
Python Standard Library, 120–121. See also library vs. framework vs. SDK vs. engine vs. API
pythonic code, core of, 105
Q
__qualname__
attribute and type()
function, 284–285
questions, asking, 10–11, 14–15
R
__radd__()
reflected numeric dunder method, 330
__rand__()
reflected numeric dunder method, 330
range()
vs. enumerate()
, 94–95, 105–105
__rdivmod__()
reflected numeric dunder method, 330
readlines()
and open()
functions, using, 126
README files, 200, 211–212, 215–216, 218
RecursionError
exception, raising, 318–319
references, explained, 137–138
reflected numeric dunder methods, 328–330
relative vs. absolute paths, 20–21
renaming files and folders, 32–33
repo. See also Git
cloning for GitHub repo, 222–223
creating, 223
deleting and moving files in, 215–216
and version control systems, 200
__repr__()
method, using, 325
repr
string, sensitive information in, 325
requests module, sessions.py file, 188–189
return values and data types, 177–178. See also values
__rfloordiv__()
reflected numeric dunder method, 330
__rlshift__()
reflected numeric dunder method, 330
rm
(removing files and folders) command, 33–34
__rmatmul__()
reflected numeric dunder method, 330
__rmod__()
reflected numeric dunder method, 330
__rmul__()
reflected numeric dunder method, 330
roll back, performing in Git, 217–220
root folder, explained, 18
__ror__()
reflected numeric dunder method, 330
__round__()
numeric dunder method, 328
__rpow__()
reflected numeric dunder method, 330
__rrshift__()
reflected numeric dunder method, 330
__rshift__()
numeric dunder method, 328
__rsub__()
reflected numeric dunder method, 330
__rtruediv__()
reflected numeric dunder method, 330
running processes, viewing, 22
runtime
defined, 226
quickening for functions, 173
vs. syntax vs. semantic errors, 126–127
__rxor__()
reflected numeric dunder method, 330
S
%
s conversion specifiers, using, 98
scripting language vs. programming language, 129–130
SDK vs. library vs. framework vs. engine vs. API, 130
semantic vs. syntax vs. runtime errors, 126–127
semicolons (;
), using with timeit
module, 227
sensitive information in repr
strings, 325
sequence comparisons, 335–336. See also comparison operators
sequences
and iterables, 125
sessions.py file in requests
module, 188–189
setdefault()
, using with dictionaries, 99–101
setters
using to validate data, 319–320
significant indentation, 91–92, 105. See also indentation
single quote ('), using, 46
slice syntax, explained, 98
snake_case
, 60
snapshots, saving with Git, 200
software license, file for, 200
sort()
function, behavior of, 146–147, 151, 332, 336
source code, avoiding dropping letters from, 62. See also code; code smells
source file encoding and magic comments, 187–188
space characters, rendering on screen, 47
Stack Overflow, building answer archive, 12
staged files
committing, 211
unstaging in Git, 218
statements vs. expressions, 122–123
static analysis, explained, 8, 192–194
string concatenation, 144–146, 151
strings
as immutable objects, 144
immutable quality of, 116
interpolating, 105
stubs, relationship to code smells, 74
style guides and PEP (Python Enhancement Proposal) 8, 46–47
__sub__()
numeric dunder method, 327
subclass, relationship to inheritance, 296. See also isinstance()
subfolders, listing contents of, 31
Sublime Text editor, 193
subprocess.run()
function, 27
subtract()
function, creating, 172
sum()
function, 168
summary comments, 185
super class, relationship to inheritance, 296
super()
function, relationship to overriding method, 297–299
switch
statement vs. dictionaries, 101–102
syntax
catching errors, 6
vs. runtime vs. semantic errors, 58, 126–127
sys.getsizeof()
function, 137–138
system environment variables, 38
T
Task Manager, opening, 22
terminal window
clearing, 35
tests folder, contents of, 200
text editor, Sublime Text, 193
tic-tac-toe program
MRO (method resolution order), 311–312
tilde (~
), using in macOS, 23
timeit
module, using to measure performance, 92–94, 105, 226–228. See also modules
tkdiff, installing on macOS, 213
TODO comments and codetags, 187
The Tower of Hanoi puzzle
getPlayerMove()
function, 163, 165, 254–257, 268
restrictions, 248
True
and False
keywords, 158–159
__truediv__()
numeric dunder method, 327
__trunc__()
numeric dunder method, 328
tuples
identities, 119
using commas with, 150
values of, 116
type coercion vs. type casting, 128
type()
function and __qualname__
attribute, 284–285
types, defined, 276
typing, minimizing with tab completion, 27–28
U
Ubuntu Linux, running Python programs on, 41–42
underscore (_
). See also double underscore (__
)
PEP 8’s naming conventions, 60–61
as prefix for methods and attributes, 291–292
private prefix, 81
using with _spaces
attribute, 290
using with dunder methods, 120
using with private attributes and methods, 283
using with WizCoin
class, 279
Unicode resource, 188
unit tests, folder for, 200
Unix operating system, shell programs, 22–23
URL links, including in comments, 183
user environment variables, 38
V
validating data using setters, 319–320
values. See also Boolean values; return values and data types
modifying in place, 115
variable names, 64, 66. See also names
variables. See also metasyntactic variables
vs. attributes, 124
box vs. label metaphor, 112–113
checking values, 105
with numeric suffixes, 76
swapping, 227
variadic functions, creating, 167–171. See also functions
version control systems, 199–200
volumes, explained, 18
W
watch
command, using with Git, 207
webbrowser
module, 160
where
command, 35
which
command, 35
while
keyword, 110
while
loops
in big O analysis, 241
Windows, running Python programs on, 40–41
WinMerge, downloading, 212–213
WizCoin
class, creating, 279–284
worst-case scenario, measuring with Big O, 235
wrapper functions, creating, 171–172. See also functions
X
XOR algorithm, using, 92, 226–227
__xor__()
numeric dunder method, 328
Z
Zakharenko, Nina, 131
The Zen of Python, 88–91. See also programs; Python programs
zero-based indexing, using, 117
Zsh and Z shells, 23
3.129.67.26