3.1. Statements and Syntax

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

  • Hash mark ( # ) indicates Python comments

  • NEWLINE ( ) 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 thing's 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 (1) container objects are broken up between elements over multiple lines, and when (2) 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 )

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 which require a header line and a suite. Header lines begin the statement (with the keyword) and terminate with a colon ( : ) and are followed by one or more lines which 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 is not allowed; each line would be considered part of another suite and would more than likely result in syntax errors.

A new code block is recognized when the amount of indentation has increased, and 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 creating 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 which 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, others prefer the other, etc. You get the picture.

We should also mention a minor performance improvement which can occur since each missing brace means one less byte to load during execution. Sure these are pennies on their own, but add up to hundreds and thousands of bytes over a 24×7×365 environment across a global network such as the Internet and you have something you can see. See below in Section 3.4 for tips and style guidelines on indentation.

3.1.5. Multiple Statements on a Single Line ( ; )

The semicolon ( ; ) allows multiple statements on the 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 + '
')

We caution the reader to be wary of the amount of usage of chaining multiple statements on individual lines since it makes code much less readable. You decide:

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

In our example, separating the code to individual lines makes for remarkably improved reader-friendliness.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset