Head first python

Page 42

dealing with data

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. I’ve been making lists of movie data for years and would love to be able to process it on my laptop...

A highly organized movie buff

There sure is a lot of data listed here.

The Holy Grail, 1975, Terry Jones & Terry Gilliam, 91 mins Graham Chapman Michael Palin, John Cleese, Terry Gilliam, Eric Idle & Terry Jones The Life of Brian, 1979, Terry Jones, 94 mins Graham Chapman Michael Palin, John Cleese, Terry Gilliam, Eric Idle & Terry Jones The Meaning of Life, 1983, Terry Jones, 107 mins The six Monty Python cast members The Holy Grail, 1975, Terry Jones & Terry Gilliam, 91 mins Graham Chapman Michael Palin, John Cleese, Terry Gilliam, Eric Idle & Terry Jones The Life of Brian, 1979, Terry Jones, 94 mins Graham Chapman Michael Palin, John Cleese, Terry Gilliam, Eric Idle & Terry Jones The Meaning of Life, 1983, Terry Jones, 107 mins The six Monty Python cast members The Holy Grail, 1975, Terry Jones & Terry Gilliam, 91 mins Graham Chapman Michael Palin, John Cleese, Terry Gilliam, Eric Idle & Terry Jones The Life of Brian, 1979, Terry Jones, 94 mins Graham Chapman Michael Palin, John Cleese, Terry Gilliam, Eric Idle & Terry Jones The Meaning of Life, 1983, Terry Jones, 107 mins The six Monty Python cast members

The Holy Grail, 1975, Terry Jones & Terry Gilliam, 91 mins Graham Chapman Michael Palin, John Cleese, Terry Gilliam, Eric Idle & Terry Jones The Life of Brian, 1979, Terry Jones, 94 mins Graham Chapman Michael Palin, John Cleese, Terry Gilliam, Eric Idle & Terry Jones The Meaning of Life, 1983, Terry Jones, 107 mins The six Monty Python cast members Graham Chapman, Michael Palin, John Cleese, Terry Gilliam, Eric Idle & Terry Jones

This data is complex, too.

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… 6

Chapter 1


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.