Chapter 3. Basic Statements

Now that we’ve seen Python’s fundamental built-in object types, we’re going to move on in this chapter to explore its basic statement types. In simple terms, statements are the things you write to tell Python what your programs should do. If programs do things with stuff, statements are the way you specify what sort of things a program does. By and large, Python is a procedural, statement-based language; by combining statements, you specify a procedure Python performs to satisfy a program’s goals.

Another way to understand the role of statements is to revisit the concept hierarchy we introduced in Chapter 2. In that chapter we talked about built-in objects; now we climb the hierarchy to the next level:

  1. Programs are composed of modules.

  2. Modules contain statements.

  3. Statements create and process objects.

Statements process the objects we’ve already seen. Moreover, statements are where objects spring into existence (e.g., in assignment statement expressions), and some statements create entirely new kinds of objects (functions, classes, and so on). And although we won’t discuss this in detail until Chapter 5, statements always exist in modules, which themselves are managed with statements.

Table 3.1 summarizes Python’s statement set. We’ve introduced a few of these already; for instance, in Chapter 2, we saw that the del statement deletes data structure components, the assignment statement creates references to objects, and so on. In this chapter, we fill in ...

Get Learning Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.