Index

Please note that index links to approximate location of each term.

Symbols

+ (addition), 26

+= (addition in place), 122

* (arbitrary arguments), 146

** (arbitrary keyword arguments), 148

{} (braces)

dictionaries, 92

sets, 104

@ (decorator), 221, 424

/ (division), 26

== (equality), 72, 74

** (exponent), 26

> (greater than), 75

>= (greater than or equal to), 75

# (hash mark), for comments, 29

!= (inequality), 74

< (less than), 75

<= (less than or equal to), 75

[] (list), 34

% (modulo), 116

+= (multiline strings), 115

* (multiplication), 26

\n (newline), 22

>>> (Python prompt), 4

- (subtraction), 26

\t (tab), 22

_ (underscore)

in file and folder names, 10

in numbers, 28

in variable names, 17

A

aliases, 151152, 178179

alice.py, 195197

Alien Invasion. See also Pygame

aliens, 256274

building fleet, 259262

checking edges, 265

collisions, with bullets, 267

collisions, with ship, 270273

controlling fleet direction, 264266

creating an alien, 256258

dropping fleet, 265266

reaching bottom of screen, 273274

rebuilding fleet, 268269

bullets, 247253, 266270

collisions, with aliens, 267

deleting old, 250251

firing, 249250

larger, 268

limiting number of, 251252

settings, 247

speeding up, 269

classes

Alien, 257

AlienInvasion, 229

Bullet, 247248

Button, 278279

GameStats, 271

Scoreboard, 286287

Settings, 232

Ship, 234235

ending the game, 274275

initializing dynamic settings, 283285

levels

modifying speed settings, 283285

resetting the speed, 285

displaying, 294296

moving fleet, 263266

planning, 228

Play button, 278283

Button class, 278279

deactivating, 282

drawing, 279280

hiding mouse cursor, 282283

resetting game, 281282

starting game, 281

reviewing the project, 256

scoring, 286298

all hits, 290

high score, 292294

increasing point values, 290291

level, 294296

number of ships, 296299

resetting, 289290

rounding and formatting, 291292

score attribute, 286

updating, 289

settings, storing, 232233

ship, 233244

adjusting speed, 242243

continuous movement, 239242

finding an image, 233234

limiting range, 243244

amusement_park.py, 8082

and keyword, 75

antialiasing, 279

API. See application programming interface

apostrophe.py, 2425

append() method, 3738

application programming interface (API), 355

API call, 355357

GitHub API, 368

Hacker News API, 368371

processing an API response, 357362

rate limits, 362

requesting data, 356357

visualizing results, 362368

arguments, 131. See also under functions

as keyword, 151152

assertions, 213, 217218

attributes, 159. See also under classes

B

banned_users.py, 7677

bicycles.py, 3435

Boolean values, 77

Bootstrap, 433. See also unxder Django

braces ({})

dictionaries, 92

sets, 104

break statement, 121

built-in functions, 467

C

calls (functions), 130, 132135

car.py, 162178

cars.py, 4345, 72

cities.py, 121

classes

attributes, 159

accessing, 160

default values, 163164

modifying, 164166

creating, 158161

importing, 173179

multiple classes, 175176

single classes, 174175

inheritance, 167172

attributes and methods, 169

child classes, 167170

composition, 170

__init__() method, 167169

instances as attributes, 170172

overriding methods, 170

parent classes, 170

subclasses, 168

super() function, 168

superclasses, 168

instances, 157

methods, 159

calling, 160

chaining, 185

__init()__ method, 159

modeling real-world objects, 172173

multiple instances, 161

naming conventions, 158

objects, 157

style guidelines, 181





comma-separated value files. See CSV files

comment.py, 29

comments, 2930

conditional tests, 7277. See also if statements

confirmed_users.py, 124125

constants, 28

continue statement, 122

counting.py, 117118, 122123

CSV files, 330341

csv.reader() function, 330333

error checking, 338341

file headers, 330332

D

data analysis, 301

databases. See under Django

data visualization, 301. See also Matplotlib; Plotly

datetime module, 333335

death_valley_highs_lows.py, 339341

decorators, 221223, 423425

default values

class attributes, 163164

function parameters, 134135

definition (functions), 130

def keyword, 130

del statement

with dictionaries, 96

with lists, 3840

dice_visual_d6d10.py, 326327

dice_visual.py, 324326

dictionaries

defining, 92

empty, 94

formatting larger, 9697

KeyError, 98

key-value pairs, 92

adding, 9394

removing, 96

looping through

keys, 101102

keys in order, 102103

key-value pairs, 99101

values, 103104

methods

get(), 9798

items(), 99101





keys(), 101103

values(), 103104

nesting

dictionaries in dictionaries, 110111

dictionaries in lists, 105108

lists in dictionaries, 108109

ordering in, 94, 102103

sorting a list of, 370

values

accessing, 9293, 9798

modifying, 9496

die.py, 320

die_visual.py, 320321

dimensions.py, 6667

div (HTML), 437

division_calculator.py, 192195

Django. See also Git; Learning Log project

accounts app, 415423

creating app, 415416

logging out, 419420

login page, 416419

registration page, 420423

admin site, 381386

associating data with a user, 425430

Bootstrap, 434445

card, 443

collapsible navigation, 437

container, 440

django-boostrap5 app, 434

documentation, 444

HTML headers, 435436

jumbotron, 440441

list groups, 443

navigation bar, 436439

styling forms, 441442

commands

createsuperuser, 382

flush, 427

makemigrations, 381, 385, 426

migrate, 377

runserver, 377378, 383, 392

shell, 386

startapp, 379, 415

startproject, 376

creating new projects, 376





databases

cascading delete, 384

creating, 376

foreign keys, 384, 425

many-to-one relationships, 384

migrating, 377, 381, 385, 426

non-nullable field, 427

Postgres, 447

queries, 398, 428

querysets, 386387, 395, 398, 426428

resetting, 427

SQLite, 377

deployment, 445461, 493501

committing the project, 453

configuration files, 447450

creating Platform.sh project, 453455

creating superuser, 456457

custom error pages, 459460

deleting projects, 461

free trial limits, 446

gunicorn, 447

ignoring files, 452453

installing Platform.sh CLI, 446, 497500

installing platformshconfig, 446

other deployment approaches, 500

Platform.sh, 445

Postgres database, 447, 450451

psycopg2, 447

pushing a project, 455

pushing changes, 458, 460

requirements.txt, 446

securing project, 457460

settings, 451

SSH sessions, 456457

troubleshooting, 494501

using Git, 451

viewing project, 456

development server, 377378, 383, 392





documentation

model fields, 380

queries, 388

templates, 400

forms, 404423, 429430

csrf_token, 407

GET and POST requests, 406

ModelForm, 404, 408

processing forms, 405406, 409410, 412413, 421422, 429430

save() method, 405406, 409410, 430

templates, 407, 410411, 413, 417, 419, 422

validation, 404406

widgets, 408

HTML

anchor tag (<a>), 393

<body> element, 437

comments, 437

<div> elements, 437

<main> element, 440

margins, 440

padding, 440

<p> elements, 391

<span> elements, 438

HTTP 404 error, 428429, 459460

INSTALLED_APPS, 380

installing, 375376

localhost, 378

logging out, 419420

@login_required decorator, 423424

login template, 417

mapping URLs, 388390, 397398

migrating the database, 426427

models, 379

activating, 380381

defining, 379, 384

foreign keys, 384, 425

registering with admin, 382383, 385386

__str__() method, 380, 384

projects (vs. apps), 379

redirect() function, 405406

release cycle, 376

restricting access to data, 427430





settings

ALLOWED_HOSTS, 451

DEBUG, 457458

INSTALLED_APPS, 380381, 415416, 434

LOGIN_REDIRECT_URL, 417418

LOGIN_URL, 424

LOGOUT_REDIRECT_URL, 420

SECRET_KEY, 451

shell, 386387, 426427

starting an app, 379

styling. See Django: Bootstrap

superusers, 382, 456457

templates

block tags, 393

child template, 393394

context dictionary, 395

filters, 399

forms in, 407

indentation in, 393

inheritance, 392394

linebreaks, 399

links in, 392393, 399

loops in, 395397

parent template, 392393

template tags, 393

timestamps in, 398399

user object, 418

writing, 390392

URLs. See Django: mapping URLs

UserCreationForm, 421422

user ID values, 426

versions, 376

view functions, 388, 390

virtual environments, 374375

docstrings, 130, 153, 181

dog.py, 158162

dot notation, 150, 160

E

earthquakes. See mapping earthquakes

electric_car.py, 167173

module, 177179

encoding argument, 195196

enumerate() function, 331

eq_explore_data.py, 343347

equality operator (==), 72, 74

eq_world_map.py, 347352

even_numbers.py, 58

even_or_odd.py, 117

exceptions, 183, 192199

deciding which errors to report, 199

else block, 194195

failing silently, 198199

FileNotFound error, 195196

handling exceptions, 192196

preventing crashes, 193195

try-except blocks, 193

ZeroDivisionError, 192195

exponents (**), 26

F

favorite_languages.py, 9697, 100104, 109

file_reader.py, 184187

files

encoding argument, 195196

FileNotFound error, 195196

file paths, 186

absolute, 186

exists() method, 203204

pathlib module, 184

Path objects, 184186, 330

relative, 186

from strings, 198

on Windows, 186

read_text() method, 185, 195196

splitlines() method, 186187

write_text() method, 190191

first_numbers.py, 57

fixtures, 221223

flags, 120121

floats, 2628

foods.py, 6364

for loops, 4956, 99104. See also dictionaries; lists

formatted_name.py, 137139

f-strings

format specifiers, 291292

using variables in, 2021

full_name.py, 21

functions, 129155

arguments

arbitrary, 146149

default values, 134135

errors, 136

keyword, 133134

lists as, 142145

optional, 138139

positional, 131133

body, 130

built-in, 467

calling functions, 130, 132135

defining, 130

importing, 149153

aliases, 151152

entire modules, 150151

specific functions, 151

modifying a list in a function, 142145

modules, 149153

parameters, 131

return values, 137141

style guidelines, 153

G

GeoJSON files, 342347, 350351

GET requests, 406. See Django: forms

getting help

Discord, 480

official Python documentation, 479480

online resources, xxxv, 478

r/learnpython, 480

rubber duck debugging, 478

searching online, 479

Slack, 481

Stack Overflow, 479

three main questions, 477478

Git, 356, 451453, 483492. See also Django: deployment

abandoning changes, 488489

adding files, 486

branches, 486

checking out previous commits, 489491

commits, 486488

configuring, 452, 484

deleting a repository, 491492

.gitignore, 484

HEAD, 490

ignoring files, 484

initializing a repository, 485

installing, 484

log, 487

repositories, 356

status, 485486

GitHub, 356

greeter.py, 114115, 130131

greet_users.py, 142

H

Hacker News API, 368371

hash mark (#), for comments, 29

hello_git.py, 484491

hello_world.py, 1012, 1519

hidden files, 448, 485

hn_article.py, 368369

hn_submissions.py, 369371

I

IDE (integrated development environment), 469470

if statements

and keyword, 75

Boolean expressions, 77

checking for

equality (==), 72

inequality (!=), 74

item in list, 76

item not in list, 76

list not empty, 8687

elif statement, 8083

else statement, 7980

if statements and lists, 8588

ignoring case, 7374

numerical comparisons, 7476

or keyword, 76

simple, 78

style guidelines, 89

testing multiple conditions, 8283

immutable, 65

import *, 152, 177

import this, 3031

indentation errors, 5356

index errors, 4647

inheritance, 167173. See also under classes

input() function, 114116

numerical input, 115116

writing prompts, 114115

insert() method, 38

itemgetter() function, 370

items() method, 99101

J

JSON files

GeoJSON files, 342347, 350351

JSON data format, 201

json.dumps() function, 201204, 343344, 368

json.loads() function, 201204, 343344

K

keys() method, 101103

key-value pairs, 92. See also dictionaries

keyword arguments, 133134

keywords, 466

L

language_survey.py, 219

Learning Log project, 373

files, 392

404.html, 459

500.html, 459

accounts/urls.py, 416, 420

accounts/views.py, 421422

admin.py, 382383

base.html, 392393, 396, 418419, 422, 435440

edit_entry.html, 413

forms.py, 404, 408409

.gitignore, 452453

index.html, 390394, 440441

learning_logs/urls.py, 389390, 394395, 397398, 405, 409, 412

learning_logs/views.py, 390, 395, 398, 405406, 409410, 412413, 423425, 428430

ll_project/urls.py, 388389, 416

login.html, 417, 441442

models.py, 379380, 384

new_entry.html, 410

new_topic.html, 407

.platform.app.yaml, 448450





register.html, 422

requirements.txt, 446447

routes.yaml, 450

services.yaml, 450

settings.py, 380381, 415418, 420, 424, 434, 451, 457460

topic.html, 398399, 443444

topics.html, 395396, 442443

ongoing development, 460

pages, 391

edit entry, 412414

home page, 388394

login page, 416419

new entry, 408411

new topic, 404408

registration, 420423

topic, 397400

topics, 394397

writing a specification (spec), 374

len() function, 4445

library, 184

Linux

Python

checking installed version, 8

setting up, 812, 465466

terminals

running programs from, 12

starting Python session, 9

troubleshooting installation issues, 10

VS Code, installing, 9

lists, 33

as arguments, 142145

comprehensions, 5960

copying, 6364

elements

accessing, 34

accessing last, 35

adding with append(), 3738

adding with insert(), 38

identifying unique, 104

modifying, 3637

removing with del, 3839

removing with pop(), 3940

removing with remove(), 4041

empty, 3738

enumerate() function, 331

errors

indentation, 5356

index, 46

for loops, 4956

nested, 108109, 261262

indexes, 3435

negative index, 35

zero index, 3435

len() function, 4445

naming, 3334

nesting

dictionaries in lists, 105108

lists in dictionaries, 108109

numerical lists, 5660

max() function, 59

min() function, 59

range() function, 5859

sum() function, 59

removing all occurrences of a value, 125

slices, 6162

sorting

reverse() method, 44

sorted() function, 4344

sort() method, 43

square brackets, 34

logical errors, 54

lstrip() method, 2223

M

macOS

.DS_Store files, ignoring, 453

Homebrew package manager, 499

Python

checking installed version, 7

setting up, 712, 464465

terminals

running programs from, 12

starting Python session, 7

troubleshooting installation issues, 10

VS Code, installing, 8

magicians.py, 4956

magic_number.py, 74

making_pizzas.py, 150152





mapping earthquakes, 342352. See also Plotly

downloading data, 343, 352

GeoJSON files, 342347, 350351

latitude-longitude ordering, 345

location data, 346347

magnitudes, 346

world map, 347348

Matplotlib

axes

set_aspect() method, 313314

removing, 317

ax objects, 303

colormaps, 310311

fig objects, 303

figsize argument, 318

formatting plots

alpha argument, 337338

built-in styles, 306

custom colors, 310

labels, 303304

line thickness, 303304

plot size, 318

shading, 337338

tick labels, 309310

gallery, 302

installing, 302

plot() method, 303306

pyplot module, 302303

savefig() method, 311

saving plots, 311

scatter() method, 306311

simple line graph, 302306

subplots() function, 303

methods, 20

helper methods, 237

modules, 149152, 173179. See also classes: importing; functions: importing

modulo operator (%), 116117

motorcycles.py, 3641

mountain_poll.py, 125126

mpl_squares.py, 302306

my_car.py, 174175

my_cars.py, 176179

my_electric_car.py, 176





N

name errors, 1718

name_function.py, 211217

name.py, 20

names.py, 211212

nesting. See dictionaries: nesting; lists: for loops

newline (\n), 2122

next() function, 330331

None, 98, 140

number_reader.py, 202

numbers, 2628

arithmetic, 26

constants, 28

exponents, 26

floats, 2627

formatting, 291292

integers, 26

mixing integers and floats, 2728

order of operations, 26

round() function, 291292

underscores in, 28

number_writer.py, 201

O

object-oriented programming (OOP), 157. See also classes

or keyword, 76. See also if statements

P

pandas, 320

parameters, 131

parrot.py, 114, 118121

pass statement, 198199

paths. See files: file paths

PEP 8, 6869

person.py, 139140

pets.py, 125, 132136

pip, 210211

installing Django, 374376

installing Matplotlib, 302

installing Plotly, 320

installing Pygame, 228

installing pytest, 211

installing Requests, 357

Linux, installing pip, 465466

updating, 210





pi_string.py, 187189

pizza.py, 146148

Platform.sh. See Django: deployment

players.py, 6162

Plotly, 302, 319. See also mapping earthquakes; rolling dice

chart types, 322

customizing plots, 323, 325326, 364

documentation, 368

fig.show() method, 322

fig.write_html() method, 327

formatting plots

axis labels, 323

color scales, 349350

hover text, 350351, 365366

links in charts, 366367

marker colors, 349350, 367

tick marks, 325326

titles, 323

tooltips, 365366

update_layout() method, 325326, 364

update_traces() method, 367

gallery, 320

histograms, 322

installing, 320

plotly.express module, 322, 347, 368

px alias, 322

px.bar() function, 322323, 363367

saving figures, 327

scatter_geo() function, 347352

pop() method, 3940

positional arguments, 131133. See also functions: arguments

POST requests, 406. See also Django: forms

printing_models.py, 143145

Project Gutenberg, 196197

prompts, 114115

.py file extension, 1516

Pygame. See also Alien Invasion

background colors, 231232

clock.tick() method, 230231

collisions, 266267, 270271, 289290

creating an empty window, 229230

cursor, hiding, 282283





displaying text, 278280

ending games, 274275

event loops, 229230

frame rates, 230231

fullscreen mode, 245

groups

adding elements, 249250

defining, 248249

drawing all elements in, 249250, 257258

emptying, 268269

looping through, 249251

removing elements from, 250251

updating all elements in, 248249

images, 234236

installing, 228

levels, 283285

Play button, 278283

print() calls in, 251

quitting, 244245

rect objects, 234235

creating from scratch, 247248

get_rect() method, 234235

positioning, 234235, 238243, 247248, 256262, 278, 286298

size attribute, 261

responding to input, 230

events, 230

keypresses, 238242

mouse clicks, 281283

screen coordinates, 235

surfaces, 230

testing games, 268

pytest. See testing code

Python

>>> prompt, 4

built-in functions, 467

checking installed version, 466

installing

on Linux, 465466

on macOS, 711, 464465

on Windows, 56, 463464





interpreter, 1516

keywords, 466

Python Enhancement Proposal (PEP), 68

standard library, 179180

terminal sessions, 4

on Linux, 9

on macOS, 78

on Windows, 6

versions, 4

why use Python, xxxvi

python_repos.py, 357362

python_repos_visual.py, 362367

Q

quit values, 118

R

random_walk.py, 312313

random walks, 312318

choice() function, 313

coloring points, 315316

fill_walk() method, 312313

generating multiple walks, 314315

plotting, 313314

RandomWalk class, 312313

starting and ending points, 316317

range() function, 5859

read_text() method, 185, 195196

refactoring, 204206, 237238, 260, 269270

remember_me.py, 202206

removeprefix() method, 24

removesuffix() method, 25

Requests package, installing, 357

return values, 137141

rollercoaster.py, 116

rolling dice, 319327. See also Plotly

analyzing results, 321322

Die class, 320

different-size dice, 326327

randint() function, 320

rolling two dice, 324326

rubber duck debugging, 478

rstrip() method, 2223

rw_visual.py, 313318





S

scatter_squares.py, 306311

sets, 103104

sitka_highs_lows.py, 336338

sitka_highs.py, 330336

sleep() function, 272

slices, 6164

sorted() function, 4344, 102103

sort() method, 43

splitlines() method, 186187

split() method, 196197

SQLite database, 376377

square_numbers.py, 5859

squares.py, 5960

Stack Overflow, 479

storing data, 201204. See also JSON files

saving and reading data, 202204

strings, 1925

changing case, 20

f-strings, 2021, 291292

methods

lower(), 20

lstrip(), 2223

removeprefix(), 2324

removesuffix(), 25

rstrip(), 2223

split(), 196197

splitlines(), 186187

strip(), 2223

title(), 20

upper(), 20

multiline, 115

newlines in, 2122

single and double quotes, 19, 2425

tabs in, 2122

variables in, 2021

whitespace in, 2123

strip() method, 2223

strptime() method, 333335

style guidelines, 6869

blank lines, 69

CamelCase, 181

classes, 181

dictionaries, 9697

functions, 153

if statements, 89

indentation, 68





line length, 69

PEP 8, 68

survey.py, 218

syntax errors, 24

avoiding with strings, 2425

syntax highlighting, 16

T

tab (\t), 2122

templates. See under Django

testing code, 209223

assertions, 213, 217218

failing tests, 214216

full coverage, 212

naming tests, 213

passing tests, 212214

pytest, 209223

fixtures, 221223

installing, 210211

running tests, 213214

test cases, 212

testing classes, 217223

testing functions, 211217

unit tests, 212

test_name_function.py, 212217

test_survey.py, 220223

text editors and IDEs. See also VS Code

Emacs and Vim, 475

Geany, 474

IDLE, 474

Jupyter Notebooks, 475

PyCharm, 475

Sublime Text, 474

third-party package, 210

toppings.py, 74, 8283

tracebacks, 10, 1718, 192, 195196

try-except blocks. See exceptions

tuples, 6567

defining, 65

for loop, 6667

writing over, 67

type errors, 66

U

underscore (_)

in file and folder names, 10

in numbers, 28

in variable names, 17





unit tests, 212

user_profile.py, 148149

V

values() method, 103104

variables, 1619, 28

constants, 28

as labels, 1819

multiple assignment, 28

name errors, 1718

naming conventions, 17

values, 16

venv module, 374375

version control. See Git

virtual environments, 374375

voting.py, 7880

VS Code, 45

configuring, 470473

features, 469470

installing

on Linux, 9

on macOS, 8

on Windows, 6

Python extension, 910

opening files with Python, 185

Python extension, 9

running files, 10

shortcuts, 473474

tabs and spaces, 471

W

weather data, 330341. See also CSV files; Matplotlib

while loops, 117126

active flag, 120121

break statement, 121

continue statement, 122

infinite loops, 122123

moving items between lists, 124

quit values, 118

removing all items from list, 125

whitespace, 2123. See also strings

Windows

file paths, 186

Python

setting up, 56, 912, 463464

troubleshooting installation, 10

terminals

running programs from, 12

starting Python session, 6

VS Code, installing, 6

word_count.py, 197199

write_message.py, 190191

write_text() method, 190191

Z

Zen of Python, 3031

ZeroDivisionError, 192195