Section 3.1. Statements and Syntax


3.1. Statements and Syntax

Some rules and certain symbols are used with regard to statements in Python:

  • Hash mark ( # ) indicates Python comments

  • NEWLINE ( \n ) is the standard line separator (one statement per line)

  • Backslash ( \ ) continues a line

  • Semicolon ( ; ) joins two statements on a line

  • Colon ( : ) separates a header line from its suite

  • Statements (code blocks) grouped as suites

  • Suites delimited via indentation

  • Python files organized as modules

3.1.1. Comments ( # )

First things first: Although Python is one of the easiest languages to read, it does not preclude the programmer from proper and adequate usage and placement of comments in the code. Like many of its Unix scripting brethren, Python comment statements begin with the pound sign or hash symbol (#). A comment can begin anywhere on a line. All characters following the # to the end of the line are ignored by the interpreter. Use them wisely and judiciously.

3.1.2. Continuation ( \ )

Python statements are, in general, delimited by NEWLINEs, meaning one statement per line. Single statements can be broken up into multiple lines by use of the backslash. The backslash symbol ( \ ) can be placed before a NEWLINE to continue the current statement onto the next line.

  # check conditions   if (weather_is_hot == 1) and \      (shark_warnings == 0):          send_goto_beach_mesg_to_pager()


There are two exceptions where lines can be continued without backslashes. A single statement can take up more than one line when enclosing operators are used, i.e., parentheses, square brackets, or braces, and when NEWLINEs are contained in strings enclosed in triple quotes.

  # display a string with triple quotes   print '''hi there, this is a long message for you   that goes over multiple lines... you will find   out soon that triple quotes in Python allows   this kind of fun! it is like a day on the beach!'''   # set some variables   go_surf, get_a_tan_while, boat_size, toll_money = (1,      'windsurfing', 40.0, -2.00)


Given a choice between using the backslash and grouping components you can break up with a NEWLINE, i.e., with parentheses, we recommend the latter as it is more readable.

3.1.3. Multiple Statement Groups as Suites ( : )

Groups of individual statements making up a single code block are called "suites" in Python (as we introduced in Chapter 2). Compound or complex statements, such as if, while, def, and class, are those that require a header line and a suite. Header lines begin the statement (with the keyword) and terminate with a colon ( tt ) and are followed by one or more lines that make up the suite. We will refer to the combination of a header line and a suite as a clause.

3.1.4. Suites Delimited via Indentation

As we introduced in Section 2.10, Python employs indentation as a means of delimiting blocks of code. Code at inner levels are indented via spaces or tabs. Indentation requires exact indentation; in other words, all the lines of code in a suite must be indented at the exact same level (e.g., same number of spaces). Indented lines starting at different positions or column numbers are not allowed; each line would be considered part of another suite and would more than likely result in syntax errors.

Core Style: Indent with four spaces and avoid using tabs

As someone who is perhaps new to block delimitation using whitespace, a first obvious question might be: How many spaces should I use? We think that two is too short, and six to eight is too many, so we suggest four spaces for everyone. Also, because tabs vary in the number of spaces depending on your system, we recommend not using tabs if there is any hint of cross-platform development. Both of these style guidelines are also supported by Guido van Rossum, the creator of Python, and documented in the Python Style Guide. You will find the same suggestions in our style guide in Section 3.4.


A new code block is recognized when the amount of indentation has increased, an d its termination is signaled by a "dedentation," or a reduction of indentation matching a previous level's. Code that is not indented, i.e., the highest level of code, is considered the "main" portion of the script.

The decision to create code blocks in Python using indentation was based on the belief that grouping code in this manner is more elegant and contributes to the ease of reading to which we alluded earlier. It also helps avoid "dangling-else"-type problems, including ungrouped single statement clauses (those where a C if statement does not use braces at all, but has two indented statements following). The second statement will execute regardless of the conditional, leading to more programmer confusion until the light bulb finally blinks on.

Finally, no "holy brace wars" can occur when using indentation. In C (also C++ and Java), starting braces may be placed on the same line as the header statement, or may start the very next line, or may be indented on the next line. Some like it one way, some prefer the other, etc. You get the picture.

3.1.5. Multiple Statements on a Single Line ( ; )

The semicolon ( ; ) allows multiple statements on a single line given that neither statement starts a new code block. Here is a sample snip using the semicolon:

  import sys; x = 'foo'; sys.stdout.write(x + '\n')


We caution the reader to be wary of chaining multiple statements on individual lines as it makes code much less readable, thus less "Pythonic."

3.1.6. Modules

Each Python script is considered a module. Modules have a physical presence as disk files. When a module gets large enough or has diverse enough functionality, it may make sense to move some of the code out to another module. Code that resides in modules may belong to an application (i.e., a script that is directly executed), or may be executable code in a library-type module that may be "imported" from another module for invocation. As we mentioned in the last chapter, modules can contain blocks of code to run, class declarations, function declarations, or any combination of all of those.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

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