3.1. Statements and SyntaxSome rules and certain symbols are used with regard to statements in Python:
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 IndentationAs 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
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. ModulesEach 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. |