View the Index
Total Page:16
File Type:pdf, Size:1020Kb
INDEX Symbols creating the fleet, 258–264 dropping the fleet, 267 + (addition), 26 reaching bottom of * (asterisk) operator, 147 screen, 276 {} (braces), 92, 105 rebuilding the fleet, 270 / (division), 26 bullets, 246–252 ** (double asterisk) operator, 149 collisions, with aliens, == (equality operator), 72–73 268–269, 291–292 ** (exponent), 26 deleting old, 250 // (floor division), 260 firing, 249 > (greater than), 75 limiting number of, 251 >= (greater than or equal to), 75 making larger, 270 # (hash mark), for comments, 29 settings, 247 != (inequality operator), 74 speeding up, 271 < (less than), 75 classes <= (less than or equal to), 75 Alien, 256–258 % (modulo operator), 116–117, 122 Bullet, 247–248 * (multiplication), 26 Button, 280–281 \n (newline), 22 GameStats, 273 ! (not), 74 Scoreboard, 288–289 += operator, 115 Settings, 231 [] (square brackets), 34 Ship, 233–235 - (subtraction), 26 ending the game, 276 \t (tab), 22 files alien_invasion.py, 229 A bullet.py, 247 addition (+), 26 button.py, 280 aliases, 152 game_stats.py, 273 alice.py, 197–199 scoreboard.py, 288 Alien Invasion project. See also settings.py, 231 Pygame ship.bmp, 233 aliens initializing dynamic checking edges, 266 settings, 286 collisions, with bullets, levels 268–269, 291–292 adding, 285–287 collisions, with ship, 272–275 modifying speed controlling fleet settings, 285 direction, 266 resetting the speed, 287 creating an alien, 256 planning, 228 Alien Invasion project, continued asterisk (*) operator, 147 Play button attributes, 159. See also classes: adding, 280–285 attributes deactivating, 284 drawing, 281 B hiding the mouse cursor, 284 banned_users.py, 77 resetting the game, 283 bicycles.py, 34–36 starting the game, 283 .bmp (bitmap) image files, 232 scoring, 288–300 body all hits, 292 of a function, 130 high score, 294 of an HTML file, 440 increasing point values, 292 Boolean values, 77, 456 level, 296–298 Bootstrap, 438–447 number of ships, 298–300 braces ({}), 92, 105 resetting, 291 built-in functions, 471 rounding and formatting, 293–294 C score attribute, 288 CamelCase, 181 updating, 291 car.py, 162–179 settings, storing, 231 cars.py, 43–45, 72 ship cities.py, 121 adjusting speed, 241–243 classes continuous movement, attributes, 159 239–241 accessing, 160 finding an image, 232 default values, 163 limiting range, 243 modifying, 164–166 amusement_park.py, 80–83 creating, 158–162 and keyword, 75 importing, 174 –179 API (application programming all classes from a interface), 359 module, 177 calls, 359 multiple classes, 175–177 for GitHub, 371 single class, 174 –175 for Hacker News, 372–375 inheritance, 167–173 processing responses, 361–365 attributes and methods, 169 rate limits, 365 child classes, 167 requesting data, 360 __init__() method, 167–169 visualizing results, 366–371 instances as attributes, apostrophe.py, 24 170 –172 append() method, 37 overriding methods, 170 application programming interface. parent classes, 167 See API (application subclasses, 168 programming interface) super() function, 168 arguments, 131–137. See also superclasses, 168 functions: arguments instances, 157 arithmetic, 26 methods, 159 as keyword, 152 calling, 160 assert methods, 212, 216 __init__() method, 159 496 Index modeling real-world objects, 173 key-value pairs, 99 multiple instances, 161 values, 104 naming conventions, 159 ordering in, 94 objects, 157 sorting a list of, 374 styling guidelines, 181 values comma-separated value files. See accessing, 93 CSV (comma-separated modifying, 95 value) files die.py, 324 comment.py, 29 die_visual.py, 325–327 comments, 29–30 dimensions.py, 66–67 conditional tests, 72–77. See also Discord, 48 if statements div (HTML), 441 confirmed_users.py, 124 division (/), 26 constants, 28 division_calculator.py, 194–197 counting.py, 118, 122 Django, 379. See also Heroku; CSV (comma-separated value) files, Learning Log project 334–346 admin site, 387–392 error-checking, 343–345 registering models, 388, 391 parsing headers, 334–335 associating data with users, 435 reading data, 336 Bootstrap, 438–447 card, 446 D collapsible navigation, 440 container element, 443 data analysis, 305 django-bootstrap4 app, 438 databases. See Django: databases HTML headers, 439–440 data visualization, 305. See also jumbotron, 443 Matplotlib; Plotly navigation bar, 440–442 datetime module, 337–339 styling forms, 444–445 death_valley_highs_lows.py, 343–345 commands decorators, 429 createsuperuser, 388 default values, 134 flush, 433 class attributes, 163 makemigrations, 387, 391, 432 function parameters, 134 migrate, 383 def keyword, 130 shell, 392 del statement, 39 startapp, 385, 421 dice_visual.py, 328–330 startproject, 382 dictionaries creating a project, 381 defining, 92 databases empty, 94 cascading delete, 390 formatting larger, 97 creating, 382 get() method, 98 foreign keys, 390 KeyError, 98 many-to-one key-value pairs, 92–99 relationships, 390 adding, 93 migrating, 383, 391 removing, 96 queries, 404, 433 looping through, 99–105 querysets, 392–393, 401 keys, 101 required (non-nullable) keys in order, 103 fields, 432 Index 497 Django, continued registration page, 426–428 databases, continued release cycle, 381 resetting, 433 restricting access to data, SQLite, 383 433–435 decorators, 429 restricting access to pages, deployment. See Heroku 428–435 development server, 383, 389 settings.py documentation INSTALLED APPS, 386, 421, 438 Django, 379 LOGIN_URL, 429 models, 386 SECRET_KEY, 461 queries, 394 shell, 392, 431 templates, 406 starting an app, 385 forms, 410–420 starting a new project, 382 action argument, 413 static files, 448 cross-site request styling. See Django: Bootstrap forgery, 413 superuser, 387 displaying, 413 templates, 397 GET and POST requests, 412 anchor tags, 399 ModelForm, 410, 414 block tags, 399 pre-filling with data, 419 context dictionary, 401 processing, 412, 416 filters, 405 validation, 410 for loop, 402 widgets, 414 indentation in, 399 get_object_or_404() method, 460 inheritance, 398 hashes (for passwords), 388 linebreaks filter, 405 HTML template tags, 399 div element, 441 third-party apps, 438 main element, 442 URLs margin, 443 capturing values, 404 padding, 443 namespaces, 399 span element, 442 URL patterns, 395–396 HTTP 404 error, 434 url template tag, 399 INSTALLED_APPS, 386, 421, 438 user ID values, 431 installing, 381 users localhost, 383 default login view, 422 logging out, 424 displaying message to login page, 422 logged-in user, 424 @login_required, 429 logging in a user, 427 login template, 422 UserCreationForm, 427 mapping URLs, 395–396 versions, 381 migrating the database, views, 396 383, 391 retrieving objects, 401, 404 models, 385–393, 431 docstrings, 130 privileges, 387 dog.py, 158–162 projects (vs. apps), 384 dot notation, 151, 160 redirect() function, 411 double asterisk (**) operator, 149 498 Index E writing to by appending, 193 earthquakes. See mapping empty files, 191 earthquakes multiple lines, 192 electric_car.py, 168 –173 first_numbers.py, 57 electric_car.py module, 178 flags, 120 enumerate() function, 335 floats, 26 environment variables, 456 foods.py, 63–65 epoch time, 366 for loops, 49. See also dictionaries; eq_explore_data.py, 348–351 lists equality operator (==), 72–73 formatted_name.py, 138–140 eq_world_map.py, 351–357 full_name.py, 21–22 even_numbers.py, 58 functions, 129 even_or_odd.py, 117 alias (as), 152 exceptions, 183, 194–202 arguments, 131–137 deciding which errors to arbitrary keyword, 148 report, 201 arbitrary number of, 147 else blocks, 196 avoiding errors, 136 failing silently, 200 keyword, 133 FileNotFoundError, 197 lists as, 143 –146 handling, 194 mixing positional and try-except blocks, 194 arbitrary, 148 using to prevent crashes, 195 optional, 138 ZeroDivisionError, 194 order of, 133 exponents (**), 26 positional, 132–133 built-in, 471 F calling, 130–137 favorite_languages.py, 97–98 equivalent calls, 135–136 FileNotFoundError, 197 multiple times, 132 file_reader.py, 184–188 defining, 130 files dictionaries, returning, 140 closing, 185 lists in file paths, 185 modifying, 143 –145 absolute, 186 protecting, 145 relative, 186 modules, 150–155 opening, 184 alias (as), 153 append mode, 192 importing all read mode, 192 functions (*), 153 write mode, 192 importing entire reading from, 184–190 modules, 150 entire files, 184–185 importing specific line by line, 187 functions, 152 making a list of lines, 188 parameters, default working with contents, 188 values for, 134 working with large passing information to. See files, 189 functions: arguments return values, 137–142 styling, 154 Index 499 G H games. See Alien Invasion project; Hacker News, 372 Pygame hash mark (#), for comments, 29 Geany, 476 head, of an HTML file, 440 GET requests, 412 HEAD (Git), 492 getting help Hello World, 9 Discord, 484 hello_git.py, 486–491 IRC (Internet Relay Chat), hello_world.py, 10, 15–19 482–483 Heroku, 437. See also Django; Git; official Python documentation, Learning Log project 481 Bash shell, 454 online resources, 480 CLI, installing, 448 r/learnpython, 482 commands rubber duck debugging, 480 config, 458 Slack, 483 destroy, 462 Stack Overflow, 481 login, 453 three main questions, 479 open, 453 Git, 360, 450 ps, 453 branches, 452, 488 rename, 455 commits, 360, 450 run, 454 checking out, 491–493 set, 458 making, 452, 457, 460, 488, databases, setting up, 454 490, 493 django-heroku package, 448 configuring, 451, 486 documentation, 453 detached HEAD, 492 environment variables, setting, files 456–458 adding, 452, 460, 488, 493 error pages, custom, 458–460 ignoring, 451, 487 free plan, limitations of, HEAD, 492