Python Crash Course

Book description

Python Crash Course is a fast-paced, thorough introduction to programming with Python that will have you writing programs, solving problems, and making things that work in no time.

In the first half of the book, you'll learn about basic programming concepts, such as lists, dictionaries, classes, and loops, and practice writing clean and readable code with exercises for each topic. You'll also learn how to make your programs interactive and how to test your code safely before adding it to a project. In the second half of the book, you'll put your new knowledge into practice with three substantial projects: a Space Invaders-inspired arcade game, data visualizations with Python's super-handy libraries, and a simple web app you can deploy online.

As you work through Python Crash Course, you'll learn how to:

  • Use powerful Python libraries and tools, including matplotlib, NumPy, and Pygal
  • Make 2D games that respond to keypresses and mouse clicks, and that grow more difficult as the game progresses
  • Work with data to generate interactive visualizations
  • Create and customize simple web apps and deploy them safely online
  • Deal with mistakes and errors so you can solve your own programming problems


If you've been thinking seriously about digging into programming, Python Crash Course will get you up to speed and have you writing real programs fast. Why wait any longer? Start your engines and code!

Publisher resources

View/Submit Errata

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. About the Author
  5. About the Technical Reviewer
  6. Brief Contents
  7. Contents in Detail
  8. Acknowledgments
  9. Introduction
    1. Who Is This Book For?
    2. What Can You Expect to Learn?
    3. Why Python?
  10. Part I: Basics
    1. Chapter 1: Getting Started
      1. Setting Up Your Programming Environment
        1. Python 2 and Python 3
        2. Running Snippets of Python Code
        3. Hello World!
      2. Python on Different Operating Systems
        1. Python on Linux
          1. Checking Your Version of Python
          2. Installing a Text Editor
          3. Running the Hello World Program
          4. Running Python in a Terminal Session
        2. Python on OS X
          1. Checking Whether Python Is Installed
          2. Running Python in a Terminal Session
          3. Installing a Text Editor
          4. Configuring Sublime Text for Python 3
          5. Running the Hello World Program
        3. Python on Windows
          1. Installing Python
          2. Starting a Python Terminal Session
          3. Running Python in a Terminal Session
          4. Installing a Text Editor
          5. Configuring Geany
          6. Running the Hello World Program
      3. Troubleshooting Installation Issues
      4. Running Python Programs from a Terminal
        1. On Linux and OS X
        2. On Windows
        3. Exercise 1-1: python.org
        4. Exercise 1-2: Hello World Typos
        5. Exercise 1-3: Infinite Skills
      5. Summary
    2. Chapter 2: Variables and Simple Data Types
      1. What Really Happens When You Run hello_world.py
      2. Variables
        1. Naming and Using Variables
        2. Avoiding Name Errors When Using Variables
        3. Exercise 2-1: Simple Message
        4. Exercise 2-2: Simple Messages
      3. Strings
        1. Changing Case in a String with Methods
        2. Combining or Concatenating Strings
        3. Adding Whitespace to Strings with Tabs or Newlines
        4. Stripping Whitespace
        5. Avoiding Syntax Errors with Strings
        6. Printing in Python 2
        7. Exercise 2-3: Personal Message
        8. Exercise 2-4: Name Cases
        9. Exercise 2-5: Famous Quote
        10. Exercise 2-6: Famous Quote 2
        11. Exercise 2-7: Stripping Names
      4. Numbers
        1. Integers
        2. Floats
        3. Avoiding Type Errors with the str() Function
        4. Integers in Python 2
        5. Exercise 2-8: Number Eight
        6. Exercise 2-9: Favorite Number
      5. Comments
        1. How Do You Write Comments?
        2. What Kind of Comments Should You Write?
        3. Exercise 2-10: Adding Comments
      6. The Zen of Python
        1. Exercise 2-11: Zen of Python
      7. Summary
    3. Chapter 3: Introducing Lists
      1. What Is a List?
        1. Accessing Elements in a List
        2. Index Positions Start at 0, Not 1
        3. Using Individual Values from a List
        4. Exercise 3-1: Names
        5. Exercise 3-2: Greetings
        6. Exercise 3-3: Your Own List
      2. Changing, Adding, and Removing Elements
        1. Modifying Elements in a List
        2. Adding Elements to a List
          1. Appending Elements to the End of a List
          2. Inserting Elements into a List
        3. Removing Elements from a List
          1. Removing an Item Using the del Statement
          2. Removing an Item Using the pop() Method
          3. Popping Items from any Position in a List
          4. Removing an Item by Value
        4. Exercise 3-4: Guest List
        5. Exercise 3-5: Changing Guest List
        6. Exercise 3-6: More Guests
        7. Exercise 3-7: Shrinking Guest List
      3. Organizing a List
        1. Sorting a List Permanently with the sort() Method
        2. Sorting a List Temporarily with the sorted() Function
        3. Printing a List in Reverse Order
        4. Finding the Length of a List
        5. Exercise 3-8: Seeing the World
        6. Exercise 3-9: Dinner Guests
        7. Exercise 3-10: Every Function
      4. Avoiding Index Errors When Working with Lists
        1. Exercise 3-11: Intentional Error
      5. Summary
    4. Chapter 4: Working with Lists
      1. Looping Through an Entire List
        1. A Closer Look at Looping
        2. Doing More Work Within a for Loop
        3. Doing Something After a for Loop
      2. Avoiding Indentation Errors
        1. Forgetting to Indent
        2. Forgetting to Indent Additional Lines
        3. Indenting Unnecessarily
        4. Indenting Unnecessarily After the Loop
        5. Forgetting the Colon
        6. Exercise 4-1: Pizzas
        7. Exercise 4-2: Animals
      3. Making Numerical Lists
        1. Using the range() Function
        2. Using range() to Make a List of Numbers
        3. Simple Statistics with a List of Numbers
        4. List Comprehensions
        5. Exercise 4-3: Counting to Twenty
        6. Exercise 4-4: One Million
        7. Exercise 4-5: Summing a Million
        8. Exercise 4-6: Odd Numbers
        9. Exercise 4-7: Threes
        10. Exercise 4-8: Cubes
        11. Exercise 4-9: Cube Comprehension
      4. Working with Part of a List
        1. Slicing a List
        2. Looping Through a Slice
        3. Copying a List
        4. Exercise 4-10: Slices
        5. Exercise 4-11: My Pizzas, Your Pizzas
        6. Exercise 4-12: More Loops
      5. Tuples
        1. Defining a Tuple
        2. Looping Through All Values in a Tuple
        3. Writing over a Tuple
        4. Exercise 4-13: Buffet
      6. Styling Your Code
        1. The Style Guide
        2. Indentation
        3. Line Length
        4. Blank Lines
        5. Other Style Guidelines
        6. Exercise 4-14: PEP 8
        7. Exercise 4-15: Code Review
      7. Summary
    5. Chapter 5: If Statements
      1. A Simple Example
      2. Conditional Tests
        1. Checking for Equality
        2. Ignoring Case When Checking for Equality
        3. Checking for Inequality
        4. Numerical Comparisons
        5. Checking Multiple Conditions
          1. Using and to Check Multiple Conditions
          2. Using or to Check Multiple Conditions
        6. Checking Whether a Value Is in a List
        7. Checking Whether a Value Is Not in a List
        8. Boolean Expressions
        9. Exercise 5-1: Conditional Tests
        10. Exercise 5-2: More Conditional Tests
      3. if Statements
        1. Simple if Statements
        2. if-else Statements
        3. The if-elif-else Chain
        4. Using Multiple elif Blocks
        5. Omitting the else Block
        6. Testing Multiple Conditions
        7. Exercise 5-3: Alien Colors #1
        8. Exercise 5-4: Alien Colors #2
        9. Exercise 5-5: Alien Colors #3
        10. Exercise 5-6: Stages of Life
        11. Exercise 5-7: Favorite Fruit
      4. Using if Statements with Lists
        1. Checking for Special Items
        2. Checking That a List Is Not Empty
        3. Using Multiple Lists
        4. Exercise 5-8: Hello Admin
        5. Exercise 5-9: No Users
        6. Exercise 5-10: Checking Usernames
        7. Exercise 5-11: Ordinal Numbers
      5. Styling Your if Statements
        1. Exercise 5-12: Styling if statements
        2. Exercise 5-13: Your Ideas
      6. Summary
    6. Chapter 6: Dictionaries
      1. A Simple Dictionary
      2. Working with Dictionaries
        1. Accessing Values in a Dictionary
        2. Adding New Key-Value Pairs
        3. Starting with an Empty Dictionary
        4. Modifying Values in a Dictionary
        5. Removing Key-Value Pairs
        6. A Dictionary of Similar Objects
        7. Exercise 6-1: Person
        8. Exercise 6-2: Favorite Numbers
        9. Exercise 6-3: Glossary
      3. Looping Through a Dictionary
        1. Looping Through All Key-Value Pairs
        2. Looping Through All the Keys in a Dictionary
        3. Looping Through a Dictionary’s Keys in Order
        4. Looping Through All Values in a Dictionary
        5. Exercise 6-4: Glossary 2
        6. Exercise 6-5: Rivers
        7. Exercise 6-6: Polling
      4. Nesting
        1. A List of Dictionaries
        2. A List in a Dictionary
        3. A Dictionary in a Dictionary
        4. Exercise 6-7: People
        5. Exercise 6-8: Pets
        6. Exercise 6-9: Favorite Places
        7. Exercise 6-10: Favorite Numbers
        8. Exercise 6-11: Cities
        9. Exercise 6-12: Extensions
      5. Summary
    7. Chapter 7: User Input and while Loops
      1. How the input() Function Works
        1. Writing Clear Prompts
        2. Using int() to Accept Numerical Input
        3. The Modulo Operator
        4. Accepting Input in Python 2.7
        5. Exercise 7-1: Rental Car
        6. Exercise 7-2: Restaurant Seating
        7. Exercise 7-3: Multiples of Ten
      2. Introducing while Loops
        1. The while Loop in Action
        2. Letting the User Choose When to Quit
        3. Using a Flag
        4. Using break to Exit a Loop
        5. Using continue in a Loop
        6. Avoiding Infinite Loops
        7. Exercise 7-4: Pizza Toppings
        8. Exercise 7-5: Movie Tickets
        9. Exercise 7-6: Three Exits
        10. Exercise 7-7: Infinity
      3. Using a while Loop with Lists and Dictionaries
        1. Moving Items from One List to Another
        2. Removing All Instances of Specific Values from a List
        3. Filling a Dictionary with User Input
        4. Exercise 7-8: Deli
        5. Exercise 7-9: No Pastrami
        6. Exercise 7-10: Dream Vacation
      4. Summary
    8. Chapter 8: Functions
      1. Defining a Function
        1. Passing Information to a Function
        2. Arguments and Parameters
        3. Exercise 8-1: Message
        4. Exercise 8-2: Favorite Book
      2. Passing Arguments
        1. Positional Arguments
          1. Multiple Function Calls
          2. Order Matters in Positional Arguments
        2. Keyword Arguments
        3. Default Values
        4. Equivalent Function Calls
        5. Avoiding Argument Errors
        6. Exercise 8-3: T-Shirt
        7. Exercise 8-4: Large Shirts
        8. Exercise 8-5: Cities
      3. Return Values
        1. Returning a Simple Value
        2. Making an Argument Optional
        3. Returning a Dictionary
        4. Using a Function with a while Loop
        5. Exercise 8-6: City Names
        6. Exercise 8-7: Album
        7. Exercise 8-8: User Albums
      4. Passing a List
        1. Modifying a List in a Function
        2. Preventing a Function from Modifying a List
        3. Exercise 8-9: Magicians
        4. Exercise 8-10: Great Magicians
        5. Exercise 8-11: Unchanged Magicians
      5. Passing an Arbitrary Number of Arguments
        1. Mixing Positional and Arbitrary Arguments
        2. Using Arbitrary Keyword Arguments
        3. Exercise 8-12: Sandwiches
        4. Exercise 8-13: User Profile
        5. Exercise 8-14: Cars
      6. Storing Your Functions in Modules
        1. Importing an Entire Module
        2. Importing Specific Functions
        3. Using as to Give a Function an Alias
        4. Using as to Give a Module an Alias
        5. Importing All Functions in a Module
      7. Styling Functions
        1. Exercise 8-15: Printing Models
        2. Exercise 8-16: Imports
        3. Exercise 8-17: Styling Functions
      8. Summary
    9. Chapter 9: Classes
      1. Creating and Using a Class
        1. Creating the Dog Class
          1. The __init__() Method
          2. Creating Classes in Python 2.7
        2. Making an Instance from a Class
          1. Accessing Attributes
          2. Calling Methods
          3. Creating Multiple Instances
        3. Exercise 9-1: Restaurant
        4. Exercise 9-2: Three Restaurants
        5. Exercise 9-3: Users
      2. Working with Classes and Instances
        1. The Car Class
        2. Setting a Default Value for an Attribute
        3. Modifying Attribute Values
          1. Modifying an Attribute’s Value Directly
          2. Modifying an Attribute’s Value Through a Method
          3. Incrementing an Attribute’s Value Through a Method
        4. Exercise 9-4: Number Served
        5. Exercise 9-5: Login Attempts
      3. Inheritance
        1. The __init__() Method for a Child Class
        2. Inheritance in Python 2.7
        3. Defining Attributes and Methods for the Child Class
        4. Overriding Methods from the Parent Class
        5. Instances as Attributes
        6. Modeling Real-World Objects
        7. Exercise 9-6: Ice Cream Stand
        8. Exercise 9-7: Admin
        9. Exercise 9-8: Privileges
        10. Exercise 9-9: Battery Upgrade
      4. Importing Classes
        1. Importing a Single Class
        2. Storing Multiple Classes in a Module
        3. Importing Multiple Classes from a Module
        4. Importing an Entire Module
        5. Importing All Classes from a Module
        6. Importing a Module into a Module
        7. Finding Your Own Workflow
        8. Exercise 9-10: Imported Restaurant
        9. Exercise 9-11: Imported Admin
        10. Exercise 9-12: Multiple Modules
      5. The Python Standard Library
        1. Exercise 9-13: OrderedDict Rewrite
        2. Exercise 9-14: Dice
        3. Exercise 9-15: Python Module of the Week
      6. Styling Classes
      7. Summary
    10. Chapter 10: Files and Exceptions
      1. Reading from a File
        1. Reading an Entire File
        2. File Paths
        3. Reading Line by Line
        4. Making a List of Lines from a File
        5. Working with a File’s Contents
        6. Large Files: One Million Digits
        7. Is Your Birthday Contained in Pi?
        8. Exercise 10-1: Learning Python
        9. Exercise 10-2: Learning C
      2. Writing to a File
        1. Writing to an Empty File
        2. Writing Multiple Lines
        3. Appending to a File
        4. Exercise 10-3: Guest
        5. Exercise 10-4: Guest Book
        6. Exercise 10-5: Programming Poll
      3. Exceptions
        1. Handling the ZeroDivisionError Exception
        2. Using try-except Blocks
        3. Using Exceptions to Prevent Crashes
        4. The else Block
        5. Handling the FileNotFoundError Exception
        6. Analyzing Text
        7. Working with Multiple Files
        8. Failing Silently
        9. Deciding Which Errors to Report
        10. Exercise 10-6: Addition
        11. Exercise 10-7: Addition Calculator
        12. Exercise 10-8: Cats and Dogs
        13. Exercise 10-9: Silent Cats and Dogs
        14. Exercise 10-10: Common Words
      4. Storing Data
        1. Using json.dump() and json.load()
        2. Saving and Reading User-Generated Data
        3. Refactoring
        4. Exercise 10-11: Favorite Number
        5. Exercise 10-12: Favorite Number Remembered
        6. Exercise 10-13: Verify User
      5. Summary
    11. Chapter 11: Testing Your Code
      1. Testing a Function
        1. Unit Tests and Test Cases
        2. A Passing Test
        3. A Failing Test
        4. Responding to a Failed Test
        5. Adding New Tests
        6. Exercise 11-1: City, Country
        7. Exercise 11-2: Population
      2. Testing a Class
        1. A Variety of Assert Methods
        2. A Class to Test
        3. Testing the AnonymousSurvey Class
        4. The setUp() Method
        5. Exercise 11-3: Employee
      3. Summary
  11. Part II: Projects
  12. Project 1: Alien Invasion
    1. Chapter 12: A Ship That Fires Bullets
      1. Planning Your Project
      2. Installing Pygame
        1. Installing Python Packages with pip
          1. Checking for pip on Linux and OS X
          2. Checking for pip on Windows
          3. Installing pip
          4. Installing pip on Linux and OS X
          5. Installing pip on Windows
        2. Installing Pygame on Linux
        3. Installing Pygame on OS X
        4. Installing Pygame on Windows
      3. Starting the Game Project
        1. Creating a Pygame Window and Responding to User Input
        2. Setting the Background Color
        3. Creating a Settings Class
      4. Adding the Ship Image
        1. Creating the Ship Class
        2. Drawing the Ship to the Screen
      5. Refactoring: the game_functions Module
        1. The check_events() Function
        2. The update_screen() Function
        3. Exercise 12-1: Blue Sky
        4. Exercise 12-2: Game Character
      6. Piloting the Ship
        1. Responding to a Keypress
        2. Allowing Continuous Movement
        3. Moving Both Left and Right
        4. Adjusting the Ship’s Speed
        5. Limiting the Ship’s Range
        6. Refactoring check_events()
      7. A Quick Recap
        1. alien_invasion.py
        2. settings.py
        3. game_functions.py
        4. ship.py
        5. Exercise 12-3: Rocket
        6. Exercise 12-4: Keys
      8. Shooting Bullets
        1. Adding the Bullet Settings
        2. Creating the Bullet Class
        3. Storing Bullets in a Group
        4. Firing Bullets
        5. Deleting Old Bullets
        6. Limiting the Number of Bullets
        7. Creating the update_bullets() Function
        8. Creating the fire_bullet() Function
        9. Exercise 12-5: Sideways Shooter
      9. Summary
    2. Chapter 13: Aliens!
      1. Reviewing Your Project
      2. Creating the First Alien
        1. Creating the Alien Class
        2. Creating an Instance of the Alien
        3. Making the Alien Appear Onscreen
      3. Building the Alien Fleet
        1. Determining How Many Aliens Fit in a Row
        2. Creating Rows of Aliens
        3. Creating the Fleet
        4. Refactoring create_fleet()
        5. Adding Rows
        6. Exercise 13-1: Stars
        7. Exercise 13-2: Better Stars
      4. Making the Fleet Move
        1. Moving the Aliens Right
        2. Creating Settings for Fleet Direction
        3. Checking to See Whether an Alien Has Hit the Edge
        4. Dropping the Fleet and Changing Direction
        5. Exercise 13-3: Raindrops
        6. Exercise 13-4: Steady Rain
      5. Shooting Aliens
        1. Detecting Bullet Collisions
        2. Making Larger Bullets for Testing
        3. Repopulating the Fleet
        4. Speeding Up the Bullets
        5. Refactoring update_bullets()
        6. Exercise 13-5: Catch
      6. Ending the Game
        1. Detecting Alien-Ship Collisions
        2. Responding to Alien-Ship Collisions
        3. Aliens that Reach the Bottom of the Screen
        4. Game Over!
      7. Identifying When Parts of the Game Should Run
        1. Exercise 13-6: Game Over
      8. Summary
    3. Chapter 14: Scoring
      1. Adding the Play Button
        1. Creating a Button Class
        2. Drawing the Button to the Screen
        3. Starting the Game
        4. Resetting the Game
        5. Deactivating the Play Button
        6. Hiding the Mouse Cursor
        7. Exercise 14-1: Press P to Play
        8. Exercise 14-2: Target Practice
      2. Leveling Up
        1. Modifying the Speed Settings
        2. Resetting the Speed
        3. Exercise 14-3: Challenging Target Practice
      3. Scoring
        1. Displaying the Score
        2. Making a Scoreboard
        3. Updating the Score as Aliens Are Shot Down
        4. Making Sure to Score All Hits
        5. Increasing Point Values
        6. Rounding the Score
        7. High Scores
        8. Displaying the Level
        9. Displaying the Number of Ships
        10. Exercise 14-4: All-Time High Score
        11. Exercise 14-5: Refactoring
        12. Exercise 14-6: Expanding Alien Invasion
      4. Summary
  13. Project 2: Data Visualization
    1. Chapter 15: Generating Data
      1. Installing matplotlib
        1. On Linux
        2. On OS X
        3. On Windows
        4. Testing matplotlib
        5. The matplotlib Gallery
      2. Plotting a Simple Line Graph
        1. Changing the Label Type and Graph Thickness
        2. Correcting the Plot
        3. Plotting and Styling Individual Points with scatter()
        4. Plotting a Series of Points with scatter()
        5. Calculating Data Automatically
        6. Removing Outlines from Data Points
        7. Defining Custom Colors
        8. Using a Colormap
        9. Saving Your Plots Automatically
        10. Exercise 15-1: Cubes
        11. Exercise 15-2: Colored Cubes
      3. Random Walks
        1. Creating the RandomWalk() Class
        2. Choosing Directions
        3. Plotting the Random Walk
        4. Generating Multiple Random Walks
        5. Styling the Walk
        6. Coloring the Points
        7. Plotting the Starting and Ending Points
        8. Cleaning Up the Axes
        9. Adding Plot Points
        10. Altering the Size to Fill the Screen
        11. Exercise 15-3: Molecular Motion
        12. Exercise 15-4: Modified Random Walks
        13. Exercise 15-5: Refactoring
      4. Rolling Dice with Pygal
        1. Installing Pygal
        2. The Pygal Gallery
        3. Creating the Die Class
        4. Rolling the Die
        5. Analyzing the Results
        6. Making a Histogram
        7. Rolling Two Dice
        8. Rolling Dice of Different Sizes
        9. Exercise 15-6: Automatic Labels
        10. Exercise 15-7: Two D8s
        11. Exercise 15-8: Three Dice
        12. Exercise 15-9: Multiplication
        13. Exercise 15-10: Practicing with Both Libraries
      5. Summary
    2. Chapter 16: Downloading Data
      1. The CSV File Format
        1. Parsing the CSV File Headers
        2. Printing the Headers and Their Positions
        3. Extracting and Reading Data
        4. Plotting Data in a Temperature Chart
        5. The datetime Module
        6. Plotting Dates
        7. Plotting a Longer Timeframe
        8. Plotting a Second Data Series
        9. Shading an Area in the Chart
        10. Error-Checking
        11. Exercise 16-1: San Francisco
        12. Exercise 16-2: Sitka-Death Valley Comparison
        13. Exercise 16-3: Rainfall
        14. Exercise 16-4: Explore
      2. Mapping Global Data Sets: JSON Format
        1. Downloading World Population Data
        2. Extracting Relevant Data
        3. Converting Strings into Numerical Values
        4. Obtaining Two-Digit Country Codes
        5. Building a World Map
        6. Plotting Numerical Data on a World Map
        7. Plotting a Complete Population Map
        8. Grouping Countries by Population
        9. Styling World Maps in Pygal
        10. Lightening the Color Theme
        11. Exercise 16-5: All Countries
        12. Exercise 16-6: Gross Domestic Product
        13. Exercise 16-7: Choose Your Own Data
        14. Exercise 16-8: Testing the country_codes Module
      3. Summary
    3. Chapter 17: Working with APIs
      1. Using a Web API
        1. Git and GitHub
        2. Requesting Data Using an API Call
        3. Installing Requests
        4. Processing an API Response
        5. Working with the Response Dictionary
        6. Summarizing the Top Repositories
        7. Monitoring API Rate Limits
      2. Visualizing Repositories Using Pygal
        1. Refining Pygal Charts
        2. Adding Custom Tooltips
        3. Plotting the Data
        4. Adding Clickable Links to Our Graph
      3. The Hacker News API
        1. Exercise 17-1: Other Languages
        2. Exercise 17-2: Active Discussions
        3. Exercise 17-3: Testing python_repos.py
      4. Summary
  14. Project 3: Web Applications
    1. Chapter 18: Getting Started with Django
      1. Setting Up a Project
        1. Writing a Spec
        2. Creating a Virtual Environment
        3. Installing virtualenv
        4. Activating the Virtual Environment
        5. Installing Django
        6. Creating a Project in Django
        7. Creating the Database
        8. Viewing the Project
        9. Exercise 18-1: New Projects
      2. Starting an App
        1. Defining Models
        2. Activating Models
        3. The Django Admin Site
          1. Setting Up a Superuser
          2. Registering a Model with the Admin Site
          3. Adding Topics
        4. Defining the Entry Model
        5. Migrating the Entry Model
        6. Registering Entry with the Admin Site
        7. The Django Shell
        8. Exercise 18-2: Short Entries
        9. Exercise 18-3: The Django API
        10. Exercise 18-4: Pizzeria
      3. Making Pages: The Learning Log Home Page
        1. Mapping a URL
        2. Writing a View
        3. Writing a Template
        4. Exercise 18-5: Meal Planner
        5. Exercise 18-6: Pizzeria Home Page
      4. Building Additional Pages
        1. Template Inheritance
          1. The Parent Template
          2. The Child Template
        2. The Topics Page
          1. The Topics URL Pattern
          2. The Topics View
          3. The Topics Template
        3. Individual Topic Pages
          1. The Topic URL Pattern
          2. The Topic View
          3. The Topic Template
          4. Links from the Topics Page
        4. Exercise 18-7: Template Documentation
        5. Exercise 18-8: Pizzeria Pages
      5. Summary
    2. Chapter 19: User Accounts
      1. Allowing Users to Enter Data
        1. Adding New Topics
          1. The Topic ModelForm
          2. The new_topic URL
          3. The new_topic() View Function
          4. GET and POST Requests
          5. The new_topic Template
          6. Linking to the new_topic Page
        2. Adding New Entries
          1. The Entry ModelForm
          2. The new_entry URL
          3. The new_entry() View Function
          4. The new_entry Template
          5. Linking to the new_entry Page
        3. Editing Entries
          1. The edit_entry URL
          2. The edit_entry() View Function
          3. The edit_entry Template
          4. Linking to the edit_entry Page
        4. Exercise 19-1: Blog
      2. Setting Up User Accounts
        1. The users App
          1. Adding users to settings.py
          2. Including the URLs from users
        2. The Login Page
          1. The login Template
          2. Linking to the Login Page
          3. Using the Login Page
        3. Logging Out
          1. The logout URL
          2. The logout_view() View Function
          3. Linking to the logout View
        4. The Registration Page
          1. The register URL
          2. The register() View Function
          3. The register Template
          4. Linking to the Registration Page
        5. Exercise 19-2: Blog Accounts
      3. Allowing Users to Own Their Data
        1. Restricting Access with @login_required
          1. Restricting Access to the Topics Page
          2. Restricting Access Throughout Learning Log
        2. Connecting Data to Certain Users
          1. Modifying the Topic Model
          2. Identifying Existing Users
          3. Migrating the Database
        3. Restricting Topics Access to Appropriate Users
        4. Protecting a User’s Topics
        5. Protecting the edit_entry Page
        6. Associating New Topics with the Current User
        7. Exercise 19-3: Refactoring
        8. Exercise 19-4: Protecting new_entry
        9. Exercise 19-5: Protected Blog
      4. Summary
    3. Chapter 20: Styling and Deploying an App
      1. Styling Learning Log
        1. The django-bootstrap3 App
        2. Using Bootstrap to Style Learning Log
        3. Modifying base.html
          1. Defining the HTML Headers
          2. Defining the Navigation Bar
          3. Defining the Main Part of the Page
        4. Styling the Home Page Using a Jumbotron
        5. Styling the Login Page
        6. Styling the new_topic Page
        7. Styling the Topics Page
        8. Styling the Entries on the Topic Page
        9. Exercise 20-1: Other Forms
        10. Exercise 20-2: Stylish Blog
      2. Deploying Learning Log
        1. Making a Heroku Account
        2. Installing the Heroku Toolbelt
        3. Installing Required Packages
        4. Creating a Packages List with a requirements.txt File
        5. Specifying the Python Runtime
        6. Modifying settings.py for Heroku
        7. Making a Procfile to Start Processes
        8. Modifying wsgi.py for Heroku
        9. Making a Directory for Static Files
        10. Using the gunicorn Server Locally
        11. Using Git to Track the Project’s Files
          1. Installing Git
          2. Configuring Git
          3. Ignoring Files
          4. Committing the Project
        12. Pushing to Heroku
        13. Setting Up the Database on Heroku
        14. Refining the Heroku Deployment
          1. Creating a Superuser on Heroku
          2. Creating a User-Friendly URL on Heroku
        15. Securing the Live Project
        16. Committing and Pushing Changes
        17. Creating Custom Error Pages
          1. Making Custom Templates
          2. Viewing the Error Pages Locally
          3. Pushing the Changes to Heroku
          4. Using the get_object_or_404() Method
        18. Ongoing Development
        19. The SECRET_KEY Setting
        20. Deleting a Project on Heroku
        21. Exercise 20-3: Live Blog
        22. Exercise 20-4: More 404s
        23. Exercise 20-5: Extended Learning Log
      3. Summary
  15. Afterword
  16. Appendix A: Installing Python
    1. Python on Linux
      1. Finding the Installed Version
      2. Installing Python 3 on Linux
    2. Python on OS X
      1. Finding the Installed Version
      2. Using Homebrew to Install Python 3
        1. Installing Homebrew
        2. Installing Python 3
    3. Python on Windows
      1. Installing Python 3 on Windows
      2. Finding the Python Interpreter
      3. Adding Python to Your Path Variable
    4. Python Keywords and Built-in Functions
      1. Python Keywords
      2. Python Built-in Functions
  17. Appendix B: Text Editors
    1. Geany
      1. Installing Geany on Linux
      2. Installing Geany on Windows
      3. Running Python Programs in Geany
      4. Customizing Geany Settings
        1. Converting Tabs to Spaces
        2. Setting the Line Length Indicator
        3. Indenting and Unindenting Code Blocks
        4. Commenting Out Blocks of Code
    2. Sublime Text
      1. Installing Sublime Text on OS X
      2. Installing Sublime Text on Linux
      3. Installing Sublime Text on Windows
      4. Running Python Programs in Sublime Text
      5. Configuring Sublime Text
      6. Customizing Sublime Text Settings
        1. Converting Tabs to Spaces
        2. Setting the Line Length Indicator
        3. Indenting and Unindenting Code Blocks
        4. Commenting Out Blocks of Code
    3. IDLE
      1. Installing IDLE on Linux
      2. Installing IDLE on OS X
      3. Installing IDLE on Windows
      4. Customizing IDLE Settings
        1. Indenting and Unindenting Code Blocks
        2. Commenting Out Blocks of Code
    4. Emacs and vim
  18. Appendix C: Getting Help
    1. First Steps
      1. Try It Again
      2. Take a Break
      3. Refer to This Book’s Resources
    2. Searching Online
      1. Stack Overflow
      2. The Official Python Documentation
      3. Official Library Documentation
      4. r/learnpython
      5. Blog Posts
    3. IRC (Internet Relay Chat)
      1. Make an IRC Account
      2. Channels to Join
      3. IRC Culture
  19. Appendix D: Using Git for Version Control
    1. Installing Git
      1. Installing Git on Linux
      2. Installing Git on OS X
      3. Installing Git on Windows
      4. Configuring Git
    2. Making a Project
    3. Ignoring Files
    4. Initializing a Repository
    5. Checking the Status
    6. Adding Files to the Repository
    7. Making a Commit
    8. Checking the Log
    9. The Second Commit
    10. Reverting a Change
    11. Checking Out Previous Commits
    12. Deleting the Repository
  20. Index
  21. Resources

Product information

  • Title: Python Crash Course
  • Author(s): Eric Matthes
  • Release date: November 2015
  • Publisher(s): No Starch Press
  • ISBN: 9781593276034