Chapter 3. Basic Statements

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 details that were skipped and introduce the rest of Python's basic procedural statements. We stop short when statements that have to do with larger program units ”functions, classes, modules, and exceptions ”are reached. Since these statements lead to more sophisticated programming ideas, we'll give them each a chapter of their own. More exotic statements like exec (which compiles and executes code we create as strings) and assert are covered later in the book.

Table  3.1. Python Statements

Statement

Role

Examples

Assignment

references

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

Calls

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

iteration

 for x in mylist: print x 

While/else

General loops

 while 1: print 'hello' 

Pass

placeholder

 while 1: pass 

Continue

Loop jumps

 while 1:      if not line: break 

Try/except/finally

exceptions

 try: action()  except: print 'action error' 

Raise

exception

 raise endSearch, location 

Import, From

Module access

 import sys; from sys import stdin 

Def, Return

functions

 def f(a, b, c=1, *d): return a+b+c+d[0] 

Class

Building objects

 class subclass: staticData = [] 

Global

Namespaces

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

Del

Deleting things

 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 


Learning Python
Learning Python: Powerful Object-Oriented Programming
ISBN: 0596158068
EAN: 2147483647
Year: 1999
Pages: 156
Authors: Mark Lutz

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net