DekGenius.com
[ Team LiB ] Previous Section Next Section

Chapter 8. Assignment, Expressions, and Print

Now that we've seen Python's core built-in object types, this chapter explores its fundamental statement forms. 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. Python is a procedural, statement-based language; by combining statements, you specify a procedure that Python performs to satisfy a program's goals.

Another way to understand the role of statements is to revisit the concept hierarchy introduced in Chapter 4, which talked about built-in objects and the expressions used to manipulate them. This chapter climbs the hierarchy to the next level:

  1. Programs are composed of modules.

  2. Modules contain statements.

  3. Statements contain expressions.

  4. Expressions create and process objects.

At its core, Python syntax is composed of statements and expressions. Expressions process objects, and are embedded in statements. Statements code the larger logic of a program's operation—they use and direct expressions to process the objects we've already seen. Moreover, statements are where objects spring into existence (e.g., in expressions within assignment statements), and some statements create entirely new kinds of objects (functions, classes, and so on). Statements always exist in modules, which themselves are managed with statements.

Table 8-1 summarizes Python's statement set. Part III deals with entries in the table through break and continue. You've informally been introduced to a few of the statements in Table 8-1. Part III will fill in details that were skipped earlier, introduce the rest of Python's procedural statement set, and cover the overall syntax model.

Table 8-1. Python statements

Statement

Role

Example

Assignment

Creating references

curly, moe, larry = 'good', 'bad', 'ugly'

Calls

Running functions

stdout.write("spam, ham, toast\n")

print

Printing objects

print 'The Killer', joke

if/elif/else

Selecting actions

if "python" in text: print text

for/else

Sequence iteration

for x in mylist: print x

while/else

General loops

while 1: print 'hello'

pass

Empty placeholder

while 1: pass

break, continue

Loop jumps

while 1: if not line: break

try/except/ finally

Catching exceptions

try: action( )

except: print 'action error'

raise

Triggering exception

raise endSearch, location

import, from

Module access

import sys; from sys import stdin

def, return, yield

Building functions

def f(a, b, c=1, *d): return a+b+c+d[0]def gen(n): for i in n, yield i*2

class

Building objects

class subclass: staticData = [ ]

global

Namespaces

def function( ): global x, y; x = 'new'

del

Deleting references

del data[k]; del data[i:j]; del obj.attr

exec

Running code strings

exec "import " + modName in gdict, ldict

assert

Debugging checks

assert X > Y

Statements that have to do with larger program units—functions, classes, modules, and exceptions—lead to larger programming ideas, so they will each have a section of their own. More exotic statements like exec (which compiles and executes code constructed as strings) are covered later in the book, or in Python standard documentation.

    [ Team LiB ] Previous Section Next Section