You are previewing Head First Python.

Head First Python

Cover of Head First Python by Paul Barry Published by O'Reilly Media, Inc.
  1. Head First Python
  2. Dedication
  3. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  4. A Note Regarding Supplemental Files
  5. Advance Praise for Head First Python
  6. Praise for other Head First books
  7. Author of Head First Python
  8. How to use This Book: Intro
    1. Who is this book for?
      1. Who should probably back away from this book?
    2. We know what you’re thinking
    3. We know what your brain is thinking
    4. Metacognition: thinking about thinking
    5. Here’s what WE did
    6. Here’s what YOU can do to bend your brain into submission
    7. Read Me
    8. The technical review team
    9. Acknowledgments
    10. Safari® Books Online
  9. 1. Meet Python: Everyone loves lists
    1. What’s to like about Python?
    2. Install Python 3
    3. Use IDLE to help learn Python
    4. Work effectively with IDLE
      1. TAB completion
      2. Recall code statements
      3. Edit recalled code
      4. Adjust IDLE’s preferences
    5. Deal with complex data
    6. Create simple Python lists
    7. Lists are like arrays
      1. Access list data using the square bracket notation
    8. Add more data to your list
    9. Work with your list data
      1. It’s time to iterate
    10. For loops work with lists of any size
    11. Store lists within lists
    12. Check a list for a list
    13. Complex data is hard to process
    14. Handle many levels of nested lists
    15. Don’t repeat code; create a function
    16. Create a function in Python
      1. What does your function need to do?
    17. Recursion to the rescue!
      1. What a great start!
    18. Your Python Toolbox
  10. 2. Sharing your Code: Modules of functions
    1. It’s too good not to share
    2. Turn your function into a module
    3. Modules are everywhere
    4. Comment your code
    5. Prepare your distribution
    6. Build your distribution
    7. A quick review of your distribution
    8. Import a module to use it
    9. Python’s modules implement namespaces
    10. Register with the PyPI website
    11. Upload your code to PyPI
    12. Welcome to the PyPI community
    13. With success comes responsibility
      1. Requests for change are inevitable
    14. Life’s full of choices
    15. Control behavior with an extra argument
      1. Take your function to the next level
    16. Before your write new code, think BIF
      1. The range() BIF iterates a fixed number of times
    17. Python tries its best to run your code
    18. Trace your code
    19. Work out what’s wrong
    20. Update PyPI with your new code
    21. You’ve changed your API
    22. Use optional arguments
    23. Your module supports both APIs
    24. Your API is still not right
    25. Your module’s reputation is restored
      1. Your Python skills are starting to build
    26. Your Python Toolbox
  11. 3. Files and Exceptions: Dealing with errors
    1. Data is external to your program
    2. It’s all lines of text
    3. Take a closer look at the data
    4. Know your data
    5. Know your methods and ask for help
    6. Know your data (better)
      1. The case of the missing colon
    7. Two very different approaches
    8. Add extra logic
    9. Handle exceptions
    10. Try first, then recover
      1. The try/except mechanism
    11. Identify the code to protect
    12. Take a pass on the error
    13. What about other errors?
      1. Handling missing files
    14. Add more error-checking code...
    15. ...Or add another level of exception handling
    16. So, which approach is best?
      1. Complexity is rarely a good thing
    17. You’re done...except for one small thing
    18. Be specific with your exceptions
    19. Your Python Toolbox
  12. 4. Persistence: Saving data to files
    1. Programs produce data
    2. Open your file in write mode
    3. Files are left open after an exception!
    4. Extend try with finally
    5. Knowing the type of error is not enough
    6. Use with to work with files
    7. Default formats are unsuitable for files
    8. Why not modify print_lol()?
    9. Pickle your data
    10. Save with dump and restore with load
      1. What if something goes wrong?
    11. Generic file I/O with pickle is the way to go!
    12. Your Python Toolbox
  13. 5. Comprehending data: Work that data!
    1. Coach Kelly needs your help
    2. Sort in one of two ways
    3. The trouble with time
    4. Comprehending lists
      1. The beauty of list comprehensions
    5. Iterate to remove duplicates
    6. Remove duplicates with sets
    7. Your Python Toolbox
  14. 6. Custom data Objects: Bundling code with data
    1. Coach Kelly is back (with a new file format)
    2. Use a dictionary to associate data
    3. Bundle your code and its data in a class
    4. Define a class
    5. Use class to define classes
      1. Creating object instances
    6. The importance of self
    7. Every method’s first argument is self
    8. Inherit from Python’s built-in list
    9. Coach Kelly is impressed
    10. Your Python Toolbox
  15. 7. Web Development: Putting it all together
    1. It’s good to share
      1. You’re a victim of your own success
    2. You can put your program on the Web
      1. A “webapp” is what you want
    3. What does your webapp need to do?
    4. Design your webapp with MVC
    5. Model your data
    6. View your interface
      1. YATE: Yet Another Template Engine
    7. Control your code
    8. CGI lets your web server run programs
    9. Display the list of athletes
    10. The dreaded 404 error!
    11. Create another CGI script
      1. But how do you know which athlete is selected?
    12. Enable CGI tracking to help with errors
    13. A small change can make all the difference
      1. It’s a small change, but it’s an important one
    14. Your webapp’s a hit!
    15. Your Python Toolbox
  16. 8. Mobile app Development: Small devices
    1. The world is getting smaller
      1. There’s more than just desktop computers out there
    2. Coach Kelly is on Android
      1. Run Python on the coach’s smartphone
    3. Don’t worry about Python 2
    4. Set up your development environment
      1. Download the Software Development Kit (SDK)
    5. Configure the SDK and emulator
      1. Add an Android platform
      2. Create a new Android Virtual Device (AVD)
    6. Install and configure Android Scripting
    7. Add Python to your SL4A installation
    8. Test Python on Android
      1. Take your Android emulator for a spin
    9. Define your app’s requirements
    10. The SL4A Android API
    11. Select from a list on Android
    12. The athlete’s data CGI script
      1. The complete Android app, so far
    13. The data appears to have changed type
    14. JSON can’t handle your custom datatypes
    15. Run your app on a real phone
      1. Step 1: Prepare your computer
      2. Step 2: Install AndFTP on your Android phone
    16. Configure AndFTP
    17. The coach is thrilled with his app
      1. Welcome to the future!
    18. Your Python Toolbox
  17. 9. Manage Your data: Handling input
    1. Your athlete times app has gone national
    2. Use a form or dialog to accept input
    3. Create an HTML form template
    4. The data is delivered to your CGI script
    5. Ask for input on your Android phone
    6. It’s time to update your server data
    7. Avoid race conditions
    8. You need a better data storage mechanism
    9. Use a database management system
    10. Python includes SQLite
    11. Exploit Python’s database API
    12. The database API as Python code
    13. A little database design goes a long way
    14. Define your database schema
    15. What does the data look like?
    16. Transfer the data from your pickle to SQLite
    17. What ID is assigned to which athlete?
    18. Insert your timing data
    19. SQLite data management tools
    20. Integrate SQLite with your existing webapp
    21. You still need the list of names
    22. Get an athlete’s details based on ID
    23. You need to amend your Android app, too
    24. Update your SQLite-based athlete data
    25. The NUAC is over the moon!
    26. Your Python Toolbox
  18. 10. Scaling your Webapp: Getting real
    1. There are whale sightings everywhere
    2. The HFWWG needs to automate
    3. Build your webapp with Google App Engine
    4. Download and install App Engine
      1. GAE uses Python 2.5
    5. Make sure App Engine is working
    6. App Engine uses the MVC pattern
    7. Model your data with App Engine
    8. What good is a model without a view?
      1. App Engine templates in an instant
    9. Use templates in App Engine
    10. Django’s form validation framework
    11. Check your form
    12. Controlling your App Engine webapp
      1. Improve the look of your form
    13. Restrict input by providing options
    14. Meet the “blank screen of death”
    15. Process the POST within your webapp
      1. App Engine handles requests as well as responses
    16. Put your data in the datastore
    17. Don’t break the “robustness principle”
    18. Accept almost any date and time
      1. There is a third option
      2. Use “db.StringProperty()” for dates and times
    19. It looks like you’re not quite done yet
    20. Sometimes, the tiniest change can make all the difference...
    21. Capture your user’s Google ID, too
    22. Deploy your webapp to Google’s cloud
    23. Your HFWWG webapp is deployed!
    24. Your Python Toolbox
  19. 11. Dealing with Complexity: Data wrangling
    1. What’s a good time goal for the next race?
    2. So... what’s the problem?
    3. Start with the data
      1. Take another look at the data
    4. Store each time as a dictionary
    5. Dissect the prediction code
    6. Get input from your user
      1. Use input() for input
    7. Getting input raises an issue...
    8. If it’s not in the dictionary, it can’t be found
    9. Search for the closest match
    10. The trouble is with time
    11. The time-to-seconds-to-time module
    12. The trouble is still with time...
    13. Port to Android
    14. Your Android app is a bunch of dialogs
      1. Get your Android app code ready
    15. Put your app together...
    16. Your app’s a wrap!
      1. And there’s no stopping you!
    17. Your Python Toolbox
    18. It’s time to go...
      1. This is just the beginning
  20. A. Leftovers: The Top Ten Things (we didn’t cover)
    1. #1: Using a “professional” IDE
    2. #2: Coping with scoping
    3. #3: Testing
    4. #4: Advanced language features
    5. #5: Regular expressions
    6. #6: More on web frameworks
    7. #7: Object relational mappers and NoSQL
      1. There’s NoSQL, too
    8. #8: Programming GUIs
    9. #9: Stuff to avoid
    10. #10: Other books
  21. Index
  22. About the Author
  23. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  24. Copyright
O'Reilly logo

Chapter 1. Meet Python: Everyone loves lists

image with no caption

You’re asking one question: “What makes Python different?” The short answer is: lots of things. The longer answers starts by stating that there’s lots that’s familiar, too. Python is a lot like any other general-purpose programming language, with statements, expressions, operators, functions, modules, methods, and classes. All the usual stuff, really. And then there’s the other stuff Python provides that makes the programmer’s life—your life—that little bit easier. You’ll start your tour of Python by learning about lists. But, before getting to that, there’s another important question that needs answering...

What’s to like about Python?

Lots. Rather than tell you, this book’s goal is to show you the greatness that is Python.

image with no caption

Before diving head first into Python, let’s get a bit of housekeeping out of the way.

To work with and execute the Python code in this book, you need a copy of the Python 3 interpreter on your computer. Like a lot of things to do with Python, it’s not difficult to install the interpreter. Assuming, of course, it’s not already there...

Install Python 3

Before you write and run Python code, you need to make sure the Python interpreter is on your computer. In this book, you’ll start out with Release 3 of Python, the very latest (and best) version of the language.

A release of Python might already be on your computer. Mac OS X comes with Python 2 preinstalled, as do most versions of Linux (which can also ship with Release 3). Windows, in contrast, doesn’t include any release of Python. Let’s check your computer for Python 3. Open up a command-line prompt and, if you are using Mac OS X or Linux, type:

On Windows, use this command:

image with no caption

Do this!

If Python 3 is missing from your computer, download a copy for your favorite OS from the website.

image with no caption

When you install Python 3, you also get IDLE, Python’s simple—yet surprisingly useful—integrated development environment. IDLE includes a color syntax-highlighting editor, a debugger, the Python Shell, and a complete copy of Python 3’s online documentation set.

Let’s take a quick look at IDLE.

Use IDLE to help learn Python

IDLE lets you write code in its full-featured code editor as well as experiment with code at the Python Shell. You’ll use the code editor later in this book but, when learning Python, IDLE’s shell really rocks, because it lets you try out new Python code as you go.

When you first start IDLE, you are presented with the “triple chevron” prompt (>>>) at which you enter code. The shell takes your code statement and immediately executes it for you, displaying any results produced on screen.

IDLE knows all about Python syntax and offers “completion hints” that pop up when you use a built-in function like print(). Python programmers generally refer to built-in functions as BIFs. The print() BIF displays messages to standard output (usually the screen).

image with no caption

IDLE uses colored syntax to highlight your code. By default, built-in functions are purple, strings are green, and language keywords (like if) are orange. Any results produced are in blue. If you hate these color choices, don’t worry; you can easily change them by adjusting IDLE’s preferences.

IDLE also knows all about Python’s indentation syntax, which requires code blocks be indented. When you start with Python, this can be hard to get used to, but IDLE keeps you straight by automatically indenting as needed.

IDLE knows Python’s syntax and helps you conform to the Python indentation rules.

Work effectively with IDLE

IDLE has lots of features, but you need to know about only a few of them to get going.

TAB completion

Start to type in some code, and then press the TAB key. IDLE will offer suggestions to help you complete your statement.

image with no caption

Recall code statements

Press Alt-P to recall the previous code statement entered into IDLE or press Alt-N to move to the next code statement (assuming there is one). Both key combinations can be used to cycle rapidly through all of the code you’ve entered into IDLE, re-executing any code statements as needed.

Alt-P for Previous

Alt-N for Next


Unless you’re on a Mac, in which case it’s Ctrl-P and Ctrl-N.

Edit recalled code

Once you recall your code statement, you can edit it and move around the statement using the arrow keys. It’s possible to edit any statement that you’ve previously entered, even code statements that span multiple lines.

Adjust IDLE’s preferences

IDLE’s preferences dialog lets you adjust its default behavior to your tastes. There are four tabs of settings to tweak. You can control font and tab behavior, the colors used to syntax highlight, the behavior of certain key-combinations, and IDLE’s start-up settings. So, if shocking pink strings is really your thing, IDLE gives you the power to change how your code looks on screen.

image with no caption

Deal with complex data

Any program of any worth that you create has to work with data. Sometimes, the data is simple and straightforward—easy to work with. Other times, the data you have to work with is complex in its structure and meaning, forcing you to work hard to make sense of it all, let alone write code to process it.

To tame complexity, you can often arrange your data as a list: there’s the list of customers, your friend’s list, the shopping list, and your to-do list (to name a few). Arranging data in lists is so common that Python makes it easy for you to create and process lists in code.

Let’s look at some complex data before learning how to create and process list data with Python.

image with no caption

On first glance, this collection of data does indeed look quite complex. However, the data appears to conform to some sort of structure: there’s a line for a list of basic movie facts, then another line for the lead actor(s), followed by a third line listing the movie’s supporting actors.

This looks like a structure you can work with...

Create simple Python lists

Let’s start with the following simple list of movie titles and work up from there:

Here’s the same list written in a way that Python understands:

image with no caption

To turn the human-friendly list into a Python-friendly one, follow this four-step process:

  1. Convert each of the names into strings by surrounding the data with quotes.

  2. Separate each of the list items from the next with a comma.

  3. Surround the list of items with opening and closing square brackets.

  4. Assign the list to an identifier (movies in the preceding code) using the assignment operator (=).

It’s perfectly OK to put your list creation code all on one line, assuming, of course, that you have room:

image with no caption
image with no caption

No, because Python’s variable identifiers don’t have a type.

Many other programming languages insist that every identifier used in code has type information declared for it. Not so with Python: identifiers are simply names that refer to a data object of some type.

Think of Python’s list as a high-level collection. The type of the data items is not important to the list. It’s OK to state that your movies list is a “collection of strings,” but Python doesn’t need to be told this. All Python needs to know is that you need a list, you’ve given it a name, and the list has some data items in it.

Lists are like arrays

When you create a list in Python, the interpreter creates an array-like data structure in memory to hold your data, with your data items stacked from the bottom up. Like array technology in other programming languages, the first slot in the stack is numbered 0, the second is numbered 1, the third is numbered 2, and so on:

image with no caption

Access list data using the square bracket notation

As with arrays, you can access the data item in a list slot using the standard square bracket offset notation:

image with no caption

Let’s use IDLE to learn a bit about how lists work.

Add more data to your list

With your list of movie names created, now you need to add more of the movie buff’s complex data to it. You have a choice here:

image with no caption

Either strategy works. Which works best for you depends on what you are trying to do. Let’s recall what the movie buff’s data looks like:

image with no caption

The next piece of data you need to add to your list is a number (which represents the year the movie was released), and it must be inserted after each movie name. Let’s do that and see what happens.

image with no caption

No, not madness, just the way Python works.

Python lists can contain data of mixed type. It’s perfectly OK to mix strings with numbers within the same Python list. In fact, you can mix more than just strings and numbers; you can store data of any type in a single list, if you like.

Recall that a Python list is a high-level collection, designed from the get-go to store a collection of “related things.” What type those things have is of little interest to the list, because the list exists merely to provide the mechanism to store data in list form.

So, if you really need to store data of mixed type in a list, Python won’t stop you.

Work with your list data

You often need to iterate over your list and perform some action on each item as you go along. Of course, it is always possible to do something like this, which works but does not scale:

image with no caption

This code works as expected, making the data from the list appear on screen. However, if the code is later amended to add another favorite movie to the list, the list-processing code stops working as expected, because the list-processing code does not mention the third item.

Big deal: all you need to do is add another print() statement, right?

Yes, adding one extra print() statement works for one extra movie, but what if you need to add another hundred favorite movies? The scale of the problem defeats you, because adding all those extra print() statements becomes such a chore that you would rather find an excuse not to have to do.

It’s time to iterate

Processing every list item is such a common requirement that Python makes it especially convenient, with the built-in for loop. Consider this code, which is a rewrite of the previous code to use a for loop:

image with no caption

Using a for loop scales and works with any size list.

For loops work with lists of any size

Python’s for loop exists to process lists and other iterations in Python. Lists are the most common iterated data structure in Python, and when you need to iterate a list, it’s best to use for:

image with no caption

The list-processing code is referred to by Python programmers as the suite.

The target identifier is like any other name in your code. As your list is iterated over, the target identifier is assigned each of the data values in your list, in turn. This means that each time the loop code executes, the target identifier refers to a different data value. The loop keeps iterating until it exhausts all of your list’s data, no matter how big or small your list is.

An alternative to using for is to code the iteration with a while loop. Consider these two snippets of Python code, which perform the same action:

image with no caption

These while and for statements do the same thing.

Store lists within lists

As you’ve seen, lists can hold data of mixed type. But it gets even better than that: lists can hold collections of anything, including other lists. Simply embed the inner list within the enclosing list as needed.

Looking closely at the movie buff’s data, it is possible to determine a structure which looks much like a list of lists:

image with no caption

In Python, you can turn this real list of data into code with little or no effort. All you need to remember is that every list is a collection of items separated from each other with commas and surrounded with square brackets. And, of course, any list item can itself be another list:

So, a list within a list is possible, as is a list within a list within a list (as this example code demonstrates). In fact, it’s possible to nest lists within lists to most any level with Python. And you can manipulate every list with its own list methods and access it with the square bracket notation:

image with no caption
image with no caption

Yes, that’s correct: the loop code isn’t complete.

At the moment, the code within the loop simply prints each list item, and when it finds a list at a slot, it simply displays the entire list on screen. After all, the inner list is just another list item as far as the outer enclosing list is concerned. What’s we need here is some mechanism to spot that an item in a list is in fact another list and take the appropriate action.

That sounds a little tricky. But can Python help?

Check a list for a list

Each time you process an item in your list, you need to check to see if the item is another list. If the item is a list, you need to process the nested list before processing the next item in your outer list. Deciding what to do when in Python follows the familiar if... else... pattern:

image with no caption

No surprises here, as the if statement in Python works pretty much as expected. But what condition do you need to check? You need a way to determine if the item currently being processed is a list. Luckily, Python ships with a BIF that can help here: isinstance().

What’s cool about the isinstance() BIF is that it lets you check if a specific identifier holds data of a specific type:

Complex data is hard to process

The movie buff’s data is complex. Let’s take another look at a subset of the data and your Python code that processes it.

image with no caption
image with no caption
image with no caption

Brain Power

Can you spot the problem with your Python code as it is currently written? What do you think needs to happen to your code to allow it to process the movie buff’s data correctly?

Handle many levels of nested lists

The data and your code are not in sync.

The movie buff’s data is a list that contains a nested list that itself contains a nested list. The trouble is that your code knows only how to process a list nested inside an enclosing list.

The solution, of course, is to add more code to handle the additionally nested list. By looking at the existing code, it’s easy to spot the code you need to repeat:

image with no caption
image with no caption

That’s more list data and more Python code.

The data has to be embedded as another nested list within the already deeply nested list of supporting actors. That’s possible to do, even though it makes your head hurt just to think about a list of lists of lists of lists! Amending your code is just a matter of adding another for loop and an if statement.

That doesn’t sound like too much trouble, does it?

image with no caption

Adding another nested loop is a huge pain.

Your data is getting more complex (that mind-bending list of lists of lists of lists) and, as a consequence, your code is getting overly complex, too (that brain-exploding for loop inside a for loop inside a for loop). And overly complex code is rarely a good thing...

image with no caption

Don’t repeat code; create a function

Take a look at the code that you’ve created so far, which (in an effort to save you from having your brain explode) has already been amended to process yet another nested list. Notice anything?

image with no caption

Your code now contains a lot of repeated code. It’s also a mess to look at, even though it works with the movie buff’s amended data. All that nesting of for loops is hard to read, and it’s even harder to ensure that the else suites are associated with the correct if statement.

There has to be a better way...but what to do?

When code repeats in this way, most programmers look for a way to take the general pattern of the code and turn it into a reusable function. And Python programmers think this way, too. Creating a reusable function lets you invoke the function as needed, as opposed to cutting and pasting existing code.

So, let’s turn the repeating code into a function.

Create a function in Python

A function in Python is a named suite of code, which can also take an optional list of arguments if required.

You define a Python function using the def statement, providing a name for your function and specifying either an empty or populated argument list within parentheses. The standard form looks something like this:

image with no caption

What does your function need to do?

Your function needs to take a list and process each item in the list. If it finds a nested list within the first list, the function needs to repeat. It can do this by invoking itself on the nested list. In other words, the function needs to recur—that is, invoke itself from within the funtion code suite.

Recursion to the rescue!

The use of a recursive function has allowed you to reduce 14 lines of messy, hard-to-understand, brain-hurting code into a six-line function. Unlike the earlier code that needs to be amended to support additional nested lists (should the movie buff require them), the recursive function does not need to change to process any depth of nested lists properly.

Python 3 defaults its recursion limit to 1,000, which is a lot of lists of lists of lists of lists...and this limit can be changed should you ever need even more depth than that.

image with no caption

What a great start!

By taking advantage of functions and recursion, you’ve solved the code complexity problems that had crept into your earlier list-processing code.

By creating print_lol(), you’ve produced a reusable chunk of code that can be put to use in many places in your (and others) programs.

You’re well on your way to putting Python to work!

Your Python Toolbox

You’ve got Chapter 1 under your belt and you’ve added some key Python goodies to your toolbox.

Python Lingo

  • “BIF” - a built-in function.

  • “Suite” - a block of Python code, which is indented to indicate grouping.

  • “Batteries included” - a way of referring to the fact that Python comes with most everything you’ll need to get going quickly and productively.

IDLE Notes

  • The IDLE shell lets you experiment with your code as you write it.

  • Adjust IDLE’s preferences to suit the way you work.

  • Remember: when working with the shell, use Alt-P for Previous and use Alt-N for Next (but use Ctrl if you’re on a Mac).

The best content for your career. Discover unlimited learning on demand for around $1/day.