Chapter 2. List Data: Working with Ordered Data

image with no caption

All programs process data, and Python programs are no exception.

In fact, take a look around: data is everywhere. A lot of, if not most, programming is all about data: acquiring data, processing data, understanding data. To work with data effectively, you need somewhere to put your data when processing it. Python shines in this regard, thanks (in no small part) to its inclusion of a handful of widely applicable data structures: lists, dictionaries, tuples, and sets. In this chapter, we’ll preview all four, before spending the majority of this chapter digging deeper into lists (and we’ll deep-dive into the other three in the next chapter). We’re covering these data structures early, as most of what you’ll likely do with Python will revolve around working with data.

Numbers, Strings...and Objects

Working with a single data value in Python works just like you’d expect it to. Assign a value to a variable, and you’re all set. With help from the shell, let’s look at some examples to recall what we learned in the last chapter.


Let’s assume that this example has already imported the random module. We then call the random.randint function to generate a random number between 1 and 60, which is then assigned to the wait_time variable. As the generated number is an integer, that’s what type wait_time is in this instance:

>>> wait_time = random.randint(1, ...

Get Head First Python, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.