Index

( ) (parentheses)

for functions, 22, 56, 101

for tuples, 15

* (star)

in function arguments, 102104, 108109

in module imports, 229230, 240241

in variable names, 46

operator, 5, 3840, 47, 57, 298, 307, 310, 313

** (double star)

in function arguments, 104, 108109

operator, 5, 40, 56, 57

*=, **= operators, 7, 41

+ (plus sign)

in file modes, 258

in numbers converted to text strings, 250251

operator, 5, 1011, 14, 3840, 47, 57, 91, 298, 307, 310, 313

+= operator, 7, 41, 92, 111

, (comma)

in numbers converted to text strings, 250251

separating values, 276

- (minus sign)

in byte formatting, 253

in numbers converted to text strings, 250251

operator, 5, 18, 3940, 50, 57, 309

-= operator, 7, 41, 92

. (dot) operator, 56, 79, 154

.. (double dots), in relative imports, 237

... (ellipsis), in extended slices, 96

/ (slash)

as path separator, 281

in function signature, 105

operator, 5, 40, 57

// operator, 5, 40, 57

/=, //= operators, 7, 41

: (colon)

for dictionaries, 18

for slicing operator, 10, 4749

:= operator, 8, 12, 39, 54, 57

= operator, 3, 51

== operator, 6, 42, 57, 93

testing for None with, 87

[] (square brackets), 13

# comments, 2

% (percent sign)

in byte formatting, 253

in numbers converted to text strings, 251

operator, 5, 9, 3940, 57, 298, 310

%=, &=, ^= operators, 7, 41

&, ^ operators, 6, 18, 40, 50, 57, 309

_ (undescore)

as variable, 2, 17, 46, 61

in function names, 106

in names of internal variables, 27

in numeric literals, 38

in private attribute and method names, 176

__ (double undescore)

in attribute names, 177179

in method names, 27, 88

~ operator, 6, 40, 57

\ (backslash), as path separator, 281

| operator, 6, 18, 40, 50, 57, 302, 309

|= operator, 7, 41

>, < operators, 6, 42, 57

>=, <= operators, 6, 42, 57, 93

>>, << operators, 6, 40, 57

>>=, <<= operators, 7, 41

>>> prompt, 1

{ } (curly braces), 20

for dictionaries, 18

in f-strings, 252

ABC base class, 185

abc module, 185

abs() function, 5, 40, 297

__abs__() method, 91

@abstractmethod decorator, 130, 185

__abstractmethods__ attribute, 222

add() method, 18, 50, 309

__add__() method, 27, 80, 9091

addition, 5

__aenter__(), __aexit__() methods, 136

__aiter__(), __anext__() methods, 137

all() function, 47, 297

__all__ variable, 229, 240241

and operator, 67, 43, 57, 92

__and__() method, 90

annotations, 23

__annotations__ attribute, 107, 129, 222, 242

any() function, 47, 297

append() method, 13, 26, 300, 307, 313

applicative evaluation order, 101

archives, working with, 284

argparse module, 33, 255

args attribute, 6465, 70315

arguments, see functions, arguments of

arithmetic operators, 5, 40

ArithmeticError exception, 6768, 314318

as qualifier, 31, 64, 75, 99, 226

as_integer_ratio() method, 303

ascii() function, 253, 297

asctime() function, 293

assert statement, 7778

AssertionError exception, 68, 77, 315

assignment expression operator, see := operator

assignments, 3839, 41, 83

augmented, 7, 41, 47, 92, 111

async for, async with statements, 137

async keyword, 135, 136

asyncio module, 135, 137, 151, 271274, 285

atexit module, 26

AttributeError exception, 68, 98, 207, 304, 315

attributes, 79, 98

binding, 167, 207208

deleting, 301

fully qualified, 158

internal, 177179

lazy evaluation of, 214

no type contraints on, 179

of classes, 308

of functions, 129130

of objects, 157158, 304, 310

public, 176

await statement, 135137, 151

awaitables, 135

banker’s rounding, 5

base classes, 160

abstract, 185188

invoking menthods of, 313

linking to, 207

__slots__ variable and, 210

tuple of, 222

used for type hinting, 184185

base64 module, 274

BaseException exception, 6768, 314

__bases__ attribute, 207, 222

bash shell, 1

bin() function, 37, 297

binary data structures, 288

binary integers, 6

binascii module, 274

bit manipulation, 6

bit_length() method, 305

bitwise operators, 6, 40, 57

blocking, 269273

BlockingIOError exception, 270, 316

bool class, 297

bool type, 298

__bool__() method, 9395

Boolean values, 67, 38, 4344, 297

bound methods, 158, 183

break statement, 8, 12, 6263, 141

breakpoint() function, 298

BrokenPipeError exception, 316

BSD socket interface, 286

BufferedXXX classes, 260

BufferError exception, 315

byte arrays, 247, 266, 298300

bytearray type, 247, 298

bytes, 247248

comparing, 43

converting to text, 274

formatting, 253254

operations on, 298300

serializing objects into, 268

bytes module, 274

bytes type, 247248, 298

__bytes__() method, 9495

BytesIO class, 280

C programming language, 281, 286, 288

__call__() method, 98, 219

callable() function, 301

callback functions, 114, 116

passing arguments to, 118121

returning results from, 121123

CallbackError exception, 122

capitalize() method, 298, 310

casefold() method, 310

category() function, 295

__cause__ attribute, 65, 7173, 315

__ceil__() method, 91

center() method, 298, 310

cgi module, 275

characters

end of file (EOF), 2, 68, 315

line-feed, see newline characters

ordinal values of, 307

check_output() function, 288289

child classes, see subclasses

ChildProcessError exception, 316

chr() function, 301

circular dependency, 82

class methods, 89, 170172, 222, 301

class statement, 27, 154155, 215218

base-class names in, 160

class variables, 169172, 222

mutating, 112

__class__ attribute, 206, 223

classes, 80, 153223

adding methods to, 28

body of, 215

built-in, 313

creating, 160, 215218

duplicating definitions of, 221

extending, 160

inspecting, 318

internal variables in, 27

linking instances to, 206, 223

membership testing for, 183

names of, 222

namespace of, 215, 218

static methods in, 173

type-checking, 162

uniformity of, 183

user-defined, 179, 206207

with a single method, 166

classmethod object, 125

@classmethod decorator, 125, 170171, 183, 197, 213, 301

classobj type, 305

cleanup, 66, 141

clear() method, 300, 302, 309

click module, 255

close() method, 12, 147, 202, 258, 262, 315

closed attribute, 263

__closure__ attribute, 129

closures, 116117, 129

cls object, 125, 171, 220, 222

code, see also programs

generating, 222, 233

Pythonic, 99100, 160, 176

readability of, 103, 223

testing, 223

code-checking tools, 87, 107

__code__ attribute, 129130

collect() function, 82

collections module, 20, 81, 168, 240, 318

comma-separated values (CSVs), 276

command-line options, 15, 33, 254255, 317, 319

comments, 2

extracting, 55

comparisons, 6, 42, 93

between bytes and text, 248

compile() function, 301

complex() function, 301

__complex__() method, 9495, 301

compound-interest calculation, 4

compute_usage() function, 282283

concatenation, 47

of tuples, 16

concurrency, 148, 269, 273, 291293

conditionals, 4, 79, 4445, 5960

configparser module, 276

conjugate() method, 303, 305

ConnectionXXXError exceptions, 316

containers, 9596

__contains__() method, 9596

context managers, 7577, 99

asynchronous, 136137

cleaning up with, 141

__context__ attribute, 65, 7273, 315

contextlib module, 77, 148

@contextmanager decorator, 148

continue statement, 8, 6263

control flow

with conditionals, 79, 5960

with exceptions, 6469, 77

with loops, 6063

copy() method, 300, 302, 309

copying, of sequences, 4748

coroutines, 135

generator-based, 146

inspecting, 318

cos() function, 318

count() method, 298, 307, 310, 313

Counter class, 318

cryptographic hash values, 278

csv module, 31, 276

ctime() function, 293

currentframe() function, 132

daemon flag, 291

data encapsulation, 176

data hiding, 178

@dataclass decorator, 134

dataclasses, 196, 233

datetime module, 293, 318

deadlocks, 25, 204

debug variable, 215

debug() function, 133, 281

debugging

breakpoint for, 298

error messages for, 66, 124

exception handling and, 74

in Python development environment, 3

introducing code for, 7778

logging for, 280

making output for, 162

modules individually, 235

smaller tasks, 145

stack frames and, 133

when using dictionaries, 21

with __dict__ attribute, 211

with repr(), 11

with __repr__(), 28, 196

decode() method, 248, 259, 298, 310

decorators, 104, 124127, 194197, 220

deep copies, 83

deepcopy() function, 84

def statement, 22, 27, 101

DEFAULT_BUFFER_SIZE value, 259

defaultdict class, 318

del statement, 19, 5051, 8283, 89, 202, 207, 300302, 307

__del__() method, 89, 201206

delattr() function, 157, 179, 301

__delattr__() method, 98, 207

delegation, 208210

__delete__() method, 211

__delitem__() method, 9596

dependency injection, 165, 176

deque class, 81, 318

descriptors, 211214, 220, 285

Design Patterns book, 176

detach() method, 261

dict type, 80, 301302

dict() function, 20, 51, 168

__dict__ attribute, 206207, 211, 215, 222223, 242, 302, 313

dictionaries, 1821

accessing members of, 18

adding unusual items to, 8587

as literals, 38

assignments on, 83

converting to lists, 20

creating, 18, 20, 5354

deep vs. shallow copies of, 83

dispatching through, 193

empty, 20, 43

initializing missing items automatically, 318

inserting objects into, 18

iterating over, 21

keys of, 2021, 294295

modifying objects of, 18

nested, 19

no ordered comparisons on, 43

number of items in, 306

operations on, 51, 301302

passed as function parameters, 107

removing elements from, 19, 104

using tuples in, 19

values of, 21

dictionary comprehension, 20, 5354

dictionary lookups, 176

DictReader class, 277

difference operation, 18, 50, 309

difference() method, 309

difference_update() method, 309

dir() function, 26, 31, 302

__dir__() method, 302

directories, 264

current working, 234

temporary, 289

discard() method, 18, 50, 309

distutils module, 243

division, 5, 303

divmod() function, 5, 40, 303

__divmod__() method, 90

Django library, 279

__doc__ attribute, 106, 129, 222, 242

docopt module, 255

documentation strings, 106, 129, 155, 222, 242

dot operator, see . operator

double-ended queues, 318

duck typing, 167

dump() function, 268

dumps() function, 280

dynamic binding, 167

e, E, in numbers, 37

echo servers, 286

.egg suffix, 234

elif statement, 78, 5960, 193

Ellipsis object, 96

else statement, 79, 4445, 57, 5960, 63, 66, 193

emails, sending, 285

encode() method, 248, 259, 310

encoding attribute, 13, 259260, 263

encodings, 248249, 263

end keyword, 265, 308

endswith() method, 10, 298, 311

__enter__() method, 75, 99, 148

enumerate() function, 61, 303

env command, 256

environment variables, 256

EnvironmentError exception, 314316

EOF (end of file) character, 2, 68, 315

EOFError exception, 68, 315

epoll() function, 285

__eq__() method, 9394

errno attribute, 316

errno module, 277278

errors

handling, 110111

logging, 66

errors attribute, 259260, 263

eval() function, 89, 301, 303, 308

Event class, 292

except statement, 2425, 6466, 70, 110

narrowness of, 74

Exception exception, 6669, 314

exceptions, 2425, 6475

altering control flow, 6769

asynchronous, 69

built-in, 6768, 75, 314318

catching, 64, 66, 70, 7374

chained, 7073, 315

defining new, 6970, 74

expected vs. unexpected, 7273

extracting values attached to, 141

handling, 2425, 7375, 110111, 121122, 277278

hierarchy of, 6768, 70

ignoring, 6566

predefined, 315318

propagating, 64

raising, 64, 69, 110, 147, 315

standard attributes of, 6465

traceback messages of, 70, 73

user-defined, 69, 314

wrapping with a function, 54

exec() function, 133134, 233, 301, 303

__exit__() method, 75, 99, 148

expandtabs() method, 298, 311

expressions, 3, 3839

evaluating, 43, 303

extend() method, 300, 307

Extensible Markup Language (XML), 295

f-strings, 4, 9, 11, 251252

False value, 67, 38, 4345, 297298

fast lookups, 19

fcntl module, 262, 278

fcntl() function, 278

file descriptors, 257, 262

file keyword, 265, 308

file modes, 258, 263

file objects, 258, 262, 307

attributes of, 263

implementing, 279

manipulating, 261

methods supported by, 261

standard, 263

file pointers, 262

__file__ attribute, 36, 242

__file__ variable, 241

FileExistsError exception, 258, 316

FileIO class, 260

filename attribute, 316317

fileno() method, 262

FileNotFoundError exception, 277, 316

files

converting between binary and text, 261

copying, 284, 319

encoding of, 13, 263

locking, 278

metadata of, 282

names of, 257258, 263

newline characters in, 260, 263

opening, 12, 256258

overwritting, 258

parsing, 105

paths to, 281283

reading, 1213, 262

removing, 284, 319

state of, 263

temporary, 289

writing to, 262, 268

filter() function, 128, 303

filtering, 127128

finally statement, 25, 66, 141

find() method, 10, 299, 311

first-class objects, 8587, 115, 242

flag variables, 63

flask library, 279

float() function, 11, 303, 315

__float__() method, 9495, 303

FloatingPointError exception, 67, 314315

floats, 3, 5, 3738, 303, 315

converting to text, 250251

precision of, 288

rounding, 5, 308

flock() function, 278

__floor__() method, 91

__floordiv__() method, 90

flush() method, 259, 262

flyweight pattern, 176

for statement, 1213, 2122, 45, 6063, 140, 176

for reading lines in a file, 262

implementing, 97

nested, 144145

on lists, 52

format() function, 11, 94, 250, 304

format() method, 9, 252253, 311

__format__() method, 94, 304

format_map() method, 311

from statement, 31, 228230, 237, 316

from_ prefix, in method names, 172

from_bytes() method, 305

fromhex() method, 274, 303

fromkeys() method, 302

frozenset() function, 304, 309

__fspath__() method, 257

function call operator, 56

functions, 2224, 101131, 137

accepting iterables, 47

arguments of

default, 23, 101102, 115

default values for, 87, 157

evaluated left-to-right, 101

keyword, 103104

number of, 101102

order of, 101

positional, 105106

asynchronous, 135137, 272, 317

attribute access, 207

attributes of, 129130

built-in, 297314

callback, 114, 116, 118123

comparing, 131

debugging messages in, 124

defining, 22, 101

delayed execution of, 145

documenting, 106, 129, 155

emulating, 98

evaluating, 116

helper, 106

higher-order, 115118

inspecting, 131133, 318

invoking, 22, 101, 111

local variables in, 24, 107, 111113, 149151

metadata of, 124125, 130

naming, 106, 129

nested, 113, 116117, 129

recursive, 114

return values of, 23, 109110

side effects in, 108

signature of, 130131

type hints in, 107, 129, 155

wrappers around, 124

functools module, 94

futures, 123

garbage collection, 26, 8183, 89, 141, 203204

gc module, 82

__ge__() method, 93

generator expressions, 5456, 128

GeneratorExit exception, 147, 314315

generators, 97, 139152

asynchronous, 317

creating, 54

delegating, 142144

emitting I/O streams, 265266

enhanced, 146151, 266267

for repeated iterations, 142

inspecting, 318

terminating, 315

get() method, 19, 302

__get__() method, 211213

get_data() function, 242

getattr() function, 157, 179, 193194, 304

__getattr__() method, 98, 207211

__getattribute__() method, 98, 207, 211

__getitem__() method, 9596, 159308

getsize() function, 282

__getstate__() method, 268

glob() function, 264

global statement, 112

globals() function, 131, 303304

__globals__ attribute, 129, 131

globbing, 264

gmtime() function, 293

__gt__() method, 93

hasattr() function, 157, 179, 304

hash() function, 304

__hash__() method, 9394

hashlib module, 278

help() command, 22

hex() function, 37, 95, 304

hex() method, 274, 299, 303

higher-order functions, 115118

http package, 279

httpx library, 279, 294

HyperText Markup Language (HTML), 279

Hypertext Transfer Protocol (HTTP), 279, 293294

__iadd__(), __iand__() methods, 9192

id() function, 80, 304

identifiers, 38

identity checks, 92

__idivmod__() method, 9192

if statement, 79, 39, 4445, 52, 57, 5960, 193

__ifloordiv__(), __ilshift__(), __imatmul__(), __imod__() methods, 9192

import statement, 15, 3035, 59, 225237, 241, 245, 316

inside a function, 228, 231

ImportError exception, 31, 6768, 226, 237, 316

importlib library, 232

__imul__() method, 9192

in operator, 19, 45, 5052, 57, 96, 302

in-place assignment, see assignments, augmented

indent() function, 290

indentation, 4, 316

IndentationError exception, 316

index() method, 299, 307, 311, 313

__index__() method, 9495

IndexError exception, 48, 49, 67, 314, 316

indexing operator, 13

on dictionaries, 18

on tuples, 16

info() function, 281

inheritance, 2830, 160163

breaking code with, 162

from built-in types, 167169

multiple, 163, 187192, 211

cooperative, 190192

propagating metaclasses via, 220

__slots__ variable and, 210

supervised, 197199

via composition, 164166

via functions, 166167

via implementation, 163164

INI files, 276

__init__() method, 27, 70, 89, 134, 154155, 161, 181201, 219220

__init__.py file, 3435, 235237, 239241

__init_subclass__() method, 197199, 220222

input() function, 13, 33, 304, 315

input/output (I/O), 247296

buffering, 258260

consuming input, 266267

error handling, 249, 256, 259, 316

generating output, 265266

handling, 288, 291

in binary mode, 257259

in text mode, 258260

nonblocking, 270

polling channels of, 271, 273, 284

insert() method, 13, 26, 300, 307

inspect module, 318

instances, 79, 154156

adding attributes to, 161, 206

alternate constructors of, 170172

caching, 200201, 204206

creating, 89, 199

deleting an attribute of, 156

destroying, 89, 201

getting, 156

initializing, 89

linking to classes, 206, 223

setting, 156

state associated with, 206, 223

storing data of, 210211

int class, 80

int() function, 11, 95, 305

__int__() method, 9495

integers, 3, 305

as literals, 37

base of, 37

binary, 6

converting

to text, 251

to/from bytes, 305

creating strings from

binary, 37, 297

hexadecimal, 37, 299, 303304

octal, 37, 307

iterating over, 21

precision of, 316

ranges of, 2122, 308

testing values of, 303

interfaces, 184188

interpreter, 1, 3

InterruptedError exception, 316

intersection operation, 18, 50, 309

intersection() method, 309

__invert__() method, 91

io module, 260261, 279280

ioctl() function, 278

IOError exception, 316

__ior__(), __ipow__() methods, 9192

ipython shell, 2

__irshift__() method, 9192 “is a” relationship, 163

is not operator, 42, 57, 80

is operator, 42, 57, 80, 92

is_integer() method, 303

IsADirectoryError exception, 316

isalnum(), isalpha() methods, 299, 311

isascii() method, 311

isatty() method, 262

isdecimal() method, 311

isdigit() method, 299, 311

isdir() function, 282

isdisjoint() method, 309

isfile() function, 282

isidentifier() method, 311

isinstance() function, 64, 81, 183, 305

islower() method, 299, 311

isnumeric(), isprintable() methods, 311

isspace() method, 299, 311

issubclass() function, 184, 305

issubset(), issuperset() methods, 309

istitle() method, 299, 311

__isub__() method, 9192

isupper() method, 299, 311

items() method, 21, 51, 302

iter() function, 305

__iter__() method, 9798, 142, 152, 160, 305

iterations, 2122, 4547, 6062, 9798, 305

ending, 69

handling, 307, 318

nested, 143

producing values for, 139

repeated, 142

reversed, 308

iterator objects

asynchronous, 136137

creating, 22, 61

implementing, 98, 152

internal stack of, 146

reversed, 97

itertools module, 318

__itruediv__() method, 9192

__ixor__() method, 9192

join() function, 281

join() method, 299, 311

JSON format, 280

json module, 280

KeyboardInterrupt exception, 67, 69, 314, 316

KeyError exception, 67, 314, 316

keys() method, 20, 51, 302

kqueue() function, 285

lambda expression, 114115, 117118

late binding, 115

lazy evaluation, 116, 214

__le__() method, 93

left-fold operations, 128

len() function, 10, 47, 50, 51, 159, 298, 302, 306, 309310, 313

__len__() method, 28, 93, 95, 159160, 308

libraries, 235

line endings, see newline characters

lineno attribute, 317

list class, 14, 80

list comprehension, 17, 52, 55

filtering with, 127

list() function, 20, 47, 55, 81, 168, 306

listdir() function, 264

lists, 1315

adding items to, 13, 307

as literals, 38, 46

as sequences, 47

assignments on, 83

comparing, 42

concatenating, 14

converting to, from data, 14

creating, 13

deep vs. shallow copies of, 83, 306

defining subtypes of, 81

empty, 14, 4344, 306

iterating over, 13, 21

modifying, 4950

nested, 14, 145146

number of items in, 306307

passed as function parameters, 107

processing, 5253, 307

removing items from, 307

slicing, 14

sorting, 307, 310

literals, 3738

ljust() method, 299, 311

load() function, 268

loads() function, 280

locals() function, 131, 303, 307

localtime() function, 293

Lock class, 291

locks, 25

Logger instance, 280281

logging module, 254, 280281

logical operators, 67, 43, 57, 92

LookupError exception, 6768, 314, 316

loops, 4, 79, 2122, 6063

aborting, 141

flag variables in, 63

numerical indices in, 61

skipping the reminder of, 8, 62

taking items from different sequences, 62

loose coupling, 167

lower() method, 10, 299, 311

__lshift__() method, 90

lstrip() method, 299, 311

__lt__() method, 9394

main() function, 33, 281

__main__ module, 234

__main__.py file, 235, 239, 245

maketrans() method, 299, 311

map() function, 128, 307

mappings, 1819, 128, 307

opeations on, 5152

math module, 318

mathematical operations, 5, 40, 9092

__matmul__() method, 90

max() function, 47, 94, 128, 307

MD5 algorithm, 278

memory

allocating, 82

reducing usage of, 55, 210211

MemoryError exception, 68, 316

metaclass keyword, 218219

metaclasses, 217222

Method Resolution Order (MRO, 190192, 222

methods, 79, 154

abstract, 185, 222

available on an object, 26

bound, 158, 183

decorating automatically, 222

defining, 215

fully qualified, 158

implementing, 213

internal, 176

internal variables in, 27

invoking, 154

magic (special), 154, 210

naming, 172, 176

public, 176

redefining, 28, 160

rewriting, 195

static, 173176, 310

user-defined, 180

Microsoft Excel, 276

min() function, 47, 94, 128, 307

mixin classes, 188192, 220

__mod__() method, 90

mode attribute, 263

module objects, 226

__module__ attribute, 129, 222

__module__ variable, 215

ModuleNotFoundError exception, 316

modules, 3032, 225235

as first-class objects, 242243

caching, 227228, 231233

compiling, 233

debugging, 235

importing, 31, 225227, 232234

circular, 230232

specific definitions from, 228230

listing contents of, 31

locating, 226

names of, 222, 226, 234, 242

namespaces of, 242, 304

organizing, 33

reloading and unloading, 232233

renaming, 227

standard library, 31, 273296, 318319

third-party, 32

modulo, 5, 307

monads, 123

__mro__ attribute, 190, 207, 222

__mul__() method, 88, 90

multiplication, 5

name attribute, 263

name mangling, 177179

__name__ attribute, 106, 129, 222, 242

__name__ variable, 33, 234

named fields, 19

namedtuple() function, 134

NameError exception, 68, 111, 316317

namespace packages, 237

namespaces, 31, 158, 226, 242

global, 112

local, 111

of classes, 215, 218

of packages, 239240

nc program, 274, 286

__ne__() method, 93

__neg__() method, 91

network programs, 286, 288, 291

__new__() method, 89, 199201, 219

new_class() function, 216217

newline attribute, 260

newline characters, 260, 263, 301, 304

controlling, in output, 265

in input, 266

newlines attribute, 263

next() function, 139141, 307

next() method, 317

__next__() method, 97, 140, 152

None value, 6, 4344, 87, 102, 108110

normalize() function, 295

not in operator, 45, 57

not operator, 67, 43, 57, 92

NotADirectoryError exception, 316

NotImplemented object, 91, 93

NotImplementedError exception, 68, 93, 316

numbers

comparing, 42

complex, 301, 303, 305

converting

from objects, 9495

from strings, 11

to text, 250251

floating-point, 5, 3738, 250251, 288, 303, 308, 315

nonempty, 43

precision of, 251

random, 319

sum of, 312

numpy package, 50, 88, 96, 263

object class, 190, 307

inheriting from, 160, 191

objects, 2630, 7999, 153

attributes of, 56

built-in, 222223

checking against classes, 183

comparing, 42, 80, 9294

converting to built-in types, 9495

coroutine, 135

deep vs. shallow copies of, 83

first-class, 8587, 115

hash values of, 9394, 304

identity of, 80, 304

immutable, 102

implementing operators for, 79

initializing, 27

invoked likes functions, 98

iterable, 45

listing available methods of, 26

location of, 3839

management of, 8990

methods available on, 26

mutable, 41, 8384, 107108

reference count of, 8183

references to, 8384

representations of, 8485, 297, 308

serialization of, 268269

sorting, 94

state of, 154

types of, 79, 154

unpacking values of, 4546, 60

values of, 79, 8485

oct() function, 37, 95, 307

offset attribute, 317

open() function, 12, 15, 241, 256261, 279, 307

operating-system functions, 281, 316, 319

operators, 3940

arithmetic, 5, 40

augmented, 7, 41, 47, 92, 111

bitwise, 6, 40, 57

comparison, 6, 4243, 93

implementing, 79

logical, 67, 43, 57, 92

precedence rules for, 5658

or operator, 67, 43, 57, 92

__or__() method, 90

ord() function, 307

os module, 258, 281, 319

os._exit() method, 317

os.chdir(), os.getcwd() methods, 257

os.environ variable, 256

os.path module, 281282

os.system() function, 284

OSError exception, 6465, 6768, 277, 314, 316318

output, see input/output (I/O)

OverflowError exception, 67, 314, 316

__package__ attribute, 242

packages, 3334, 235242

data files of, 241242

deploying, 243244

exporting, 240241

importing files within, 34

imports within, 237

installing, 36, 244

locating, 36

namespaces of, 239240

naming, 243

submodules of, running as scripts, 238239

third-party, 50

pandas library, 277

parameters, see functions, arguments of

parent classes, see base classes

partial() function, 98

partition() method, 299, 311

pass statement, 7, 60, 65

Path class, 282

path separators, 281

__path__ attribute, 242

pathlib module, 257, 264, 281283

patterns, 176

performance, 55

exception handling and, 111

local variables and, 149151

optimizing, 210, 220221

type checks and, 81

PermissionError exception, 277, 316

pickle module, 268269

pip command, 3536, 244

poll() function, 285

pop() method, 27, 104, 300, 302, 307, 309

Popen class, 289

popitem() method, 302

__pos__() method, 91

POSIX standard, 281

pow() function, 5, 40, 307

__pow__() method, 90

power, in math, 5, 307

__prepare__() method, 219

primitive types, 3

print() function, 4, 1113, 84, 265, 308

ProcessLookupError exception, 316

programs, see also code

creating, 23

executing, 1, 3, 234235

loading, 228, 233

side effects in, 108

specifying interpreter in, 3

starting from packages, 244

structuring, 3435, 59, 235

terminating, 26, 69

properties, 180183

functions associated with, 207, 211

implementing, 211

@property decorator, 180182, 308

protocols, 8799

proxies, 104, 208

push() method, 27

.py suffix, 2, 3, 30, 226, 233

__pycache__ directory, 233

pytest library, 319

Python

applying design patterns to, 176

flexibility of, 86

indentation in, 4, 316

interactive mode of, 12

no internal variables in, 27

optimized mode of, 77

package index of, 243

runtime environment of, 254, 319

python shell, 1

PYTHONPATH environment variable, 234

__qualname__ attribute, 129, 222

__qualname__ variable, 215

Queue class, 292

quit(), in REPL, 2

__radd__() method, 9091

raise statement, 25, 64, 69

__rand__() method, 91

random module, 319

range() function, 2122, 308

raw_input() function, 315

__rdivmod__() method, 90

re module, 283284, 319

read() function, 258

read() method, 12, 262, 315

read-evaluation-print loop (REPL), 1

readable() method, 262

readinto() method, 262263

readline() method, 262, 315

readlines() method, 262

recursion, 114

limit of, 114, 143, 317

RecursionError exception, 317

reduce() function, 128

reference counting, 8183, 201

reference cycle, 203

ReferenceError exception, 317

registries, 194, 197, 222

regular expressions, 283284, 319

reload() function, 232

remote servers, 208

remove() method, 18, 50, 300, 307, 309

removeprefix() method, 299, 312

removesuffix() method, 299, 312

replace() method, 10, 299, 312

repr() function, 11, 85, 89, 252, 308

__repr__() method, 28, 8990, 154155, 159199

default implementation of, 160

requests library, 279, 294

resources, controlling, 99

return statement, 59, 109, 135, 140

reverse() method, 300, 307

reversed() function, 97, 308

__reversed__() method, 97, 308

rfind() method, 299, 312

__rfloordiv__() method, 90

rglob() function, 264

rindex(), rjust() methods, 299, 312

__rlshift__() method, 91

__rmatmul__(), __rmod__(), __rmul__()

methods, 90

__ror__(), __round__() methods, 91

round() function, 5, 40, 308

rpartition(), rsplit() methods, 299, 312

__rpow__() method, 90

__rrshift__() method, 91

__rshift__() method, 90

rstrip() method, 300, 312

__rsub__(), __rtruediv__() methods, 90

run() function, 135, 270271

RuntimeError exception, 68, 114, 316317

__rxor__() method, 91

sandbox, 36

scripts, 3233

seek(), seekable() methods, 262

select module, 271, 284285

select() function, 285

selectors module, 271, 285

self object, 27, 154, 158, 169, 220

send() method, 152

sep keyword, 265, 308

sequences

comparing, 43

mutable, 4950

operations on, 4750

set comprehension, 17, 53

set() function, 1718, 20, 47, 309

__set__() method, 211

__set_name__() method, 212

setattr() function, 157, 179, 310

__setattr__() method, 98, 207, 211

setdefault() method, 302

__setitem__() method, 9596, 167

sets, 1718

adding/removing items to/from, 50, 309

as literals, 38, 46

creating, 17, 309

empty, 18

immutable, 304

number of items in, 50, 306, 309

order of elements in, 17

processing, 5051, 5354, 309

__setstate__() method, 268

setup() function, 244

setup.py file, 244

setuptools module, 234, 243244

SHA-1 algorithm, 278

shallow copies, 83

shell, 1, 284, 319

short-circuit evaluation, 43

shutil module, 284, 319

side effects, 108

SIGINT signal, 69

signal module, 69

signature() function, 130131

__signature__ attribute, 131

Simple Mail Transfer Protocol (SMTP), 285

sin() function, 318

singletons, 176, 200

site-packages directory, 35, 244

sleep() function, 293

slice instance, 96

slice() function, 310

slicing operator, 10, 4749, 298, 307, 310, 313

implementing, 96

on lists, 14

on tuples, 16

__slots__ variable, 210211, 220221, 223

smtplib module, 285286

socket module, 286287

sort() method, 108, 307

sorted() function, 47, 114, 310

splatting, 46

split() function, 281

split() method, 10, 300, 312

splitlines() method, 300, 312

sprintf() function (C), 253

sqrt() function, 38, 318

stack frames, 132133

stack traceback objects, 65, 73

standard error, 263

standard input/output, 263, 304

start keyword, 62

startswith() method, 10, 300, 312

state machines, 176

statements, 59

executing, 303

@staticmethod decorator, 125, 173, 183, 213, 310

statistics module, 47, 319

stdin, stdout attributes, 289

StopAsyncIteration exception, 317

StopIteration exception, 67, 69, 97, 140141, 147, 307, 317

str() function, 11, 84, 94, 168, 248, 251252, 310

__str__() method, 94, 171, 310

default implementation of, 160

strategy pattern, 176

strerror attribute, 316

stride argument, 48, 49, 96

StringIO class, 279

strings, 3, 912

as literals, 38

as sequences, 47

checking characters of, 311

comparing, 43

concatenating, 1011, 310

converting

from non-string values, 11, 304

from objects, 94

to numbers, 11

converting to upper/lowercase, 310312

creating, 11, 310

encoding/decoding, 310

formatting, 9, 11, 250253, 310311

iterating over, 21

methods for, 10

nonempty, 43

number of items in, 10, 306, 310

processing, 310312

representations of, 8990

substrings of, 10, 310312

triple-quoted, 9, 38

whitespace in, 312

strip() method, 10, 300, 312

struct module, 288

struct_time object, 293

__sub__() method, 90

subclasses, 81, 160

checking classes against, 184, 305

implementing interfaces, 185

subprocess module, 256, 288289

subtraction, 5

subtypes, see subclasses

sum() function, 15, 47, 128, 312

super() function, 161, 189192, 199, 207, 313

superclasses, see base classes

swapcase() method, 300, 312

symmetric difference operation, 18, 50, 309

symmetric_difference() method, 309

symmetric_difference_update() method, 309

SyntaxError exception, 316, 317

sys module, 15, 319

sys.argv list, 33, 254255

sys.exit() function, 317

sys.getdefaultencoding() method, 259

sys.getfilesystemencoding() function, 257, 264

sys._getframe() function, 132

sys.getrecursionlimit() function, 114

sys.getrefcount() function, 82

sys.modules variable, 227, 232

sys.path variable, 31, 35, 226, 234, 244

sys.setrecursionlimit() function, 114

sys.stderr object, 69, 263264

sys.stdin, sys.stdout objects, 263264

SystemError exception, 317

SystemExit exception, 15, 26, 67, 69, 314, 317

TabError exception, 317

tabulation, 19, 311, 318

teletypewriter (TTY), 281

tell() method, 262

telnet program, 274, 286

tempfile module, 289290

testing

smaller tasks, 145

using file objects for, 279

text, 247248

converting from binary data, 274

encoding/decoding, 13, 248250, 255, 259

fitting to terminal width, 290

handling lines of, 260, 263

text attribute, 317

TextIOWrapper class, 260

textwrap module, 290

threading module, 271, 291293

threads, 271272

throw() method, 147, 152

time module, 293, 319

time() function, 293

time, handling, 293, 318319

TimeoutError exception, 316

title() method, 300, 312

to_bytes() method, 305

@trace decorator, 125126

traceback messages, 24, 315

traceback module, 73

__traceback__ attribute, 65, 73, 315

translate() method, 299300, 312

True value, 67, 38, 4345, 297298

__truediv__() method, 90

__trunc__() method, 91

truncate() method, 262

truncating division, 5

try statement, 2425, 64, 66, 110, 141

tuple type, 313

tuple() function, 47

tuples, 1517

as keys, 52

as literals, 38, 46

as sequences, 47

comparing, 42

concatenating, 16

creating, 15

empty, 43, 313

indexing, 16

iterating over, 17, 21

named, 109110

number of items in, 306, 313

returning values from functions with, 23, 109

slicing, 16

unpacking, 16

using in dictionaries, 19

turtle module, 319

type class, 218220, 313

type hints, 107, 129, 155, 179180, 222

creating methods from, 197

module-level, 242

with base classes, 184185

type() function, 70, 80, 154, 217, 313

type-based dispatch, 193194

TypeError exception, 43, 68, 95, 101, 103, 122, 191, 211, 305, 317

types, 79, 313

built-in, 167169

checking values against, 81, 305

conversions between, 9495

types module, 313

UDP servers, 287

unary minus/plus, 5

UnboundLocalError exception, 68, 111, 317

Unicode encoding

code-point values in, 307

in source code, 2

operations on text strings in, 294295

unicodedata module, 294295

UnicodeError exception, 6768, 249, 317

UnicodeXXXError exceptions, 317

union operation, 18, 50, 309

union() method, 309

unittest module, 319

universal newline mode, 260

UNIX

EOF character on, 2

executing files on, 3

low-level I/O control operations on, 278

newline characters on, 260

path separators on, 281

unpack() function, 288

update() method, 18, 168, 302

upper() method, 10, 300, 312

urlencode(), urlopen(), urlparse() functions, 293294

urllib package, 293294

urllib.parse package, 294

UserDict, UserList, UserString classes, 168

UTF-8 encoding, 2, 13, 249250

ValueError exception, 25, 6768, 317

values, 3, 79

checking against types, 81, 305

minimum/maximum, 307

printing, 265, 308

reducing, 128

representing, 8485

updating in place, 41

values() method, 21, 51, 302

variables, 3

associated types of, 3, 167

free, 114115

global, 111113

local, 24, 111113, 134, 149151

names of, 3

reassigning, 108

starred, 46

type hints for, 107

unbound, 317

vars() function, 313

venv command, 36

virtual environment, 36

walrus operator, see := operator

warning() function, 281

weak references, 204206, 317

weakref module, 204, 317

__weakref__ attribute, 206

web servers, 279

websites, 275, 294

while statement, 4, 79, 12, 39, 6062

Windows

EOF character on, 2

executing files on, 3

failed system calls on, 318

newline characters on, 260

path separators on, 281

WindowsError exception, 318

with statement, 12, 25, 7577, 99, 202, 256, 289290

wrap() function, 290

wrappers, 104, 208

@wraps decorator, 124125

writable() method, 262

write() function, 265

write() method, 13, 262

write_through attribute, 263

writelines() method, 262

xml package, 295296

xml.etree package, 295

__xor__() method, 90

yield from statement, 142143

yield statement, 97, 139142, 265

used as an expression, 146147

ZeroDivisionError exception, 67, 314, 318

zfill() method, 300, 312

zip() function, 62, 314