O'Reilly logo

Head First Python, 2nd Edition by Paul Barry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

Numbers

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required