12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

In general, top-level (unnested) code must start in column 1. Nested blocks can startin any column; indentation may consist of any number of spaces and tabs, as long asit’s the same for all the statements in a given single block. That is, <strong>Python</strong> doesn’tcare how you indent your code; it only cares that it’s done consistently. Technically,tabs count for enough spaces to move the current column number up to a multiple of8, but it’s usually not a good idea to mix tabs and spaces within a block—use one orthe other.The only major place in <strong>Python</strong> where whitespace matters is when it’s used to the leftof your code, for indentation; in most other contexts, space can be coded or not.However, indentation is really part of <strong>Python</strong> syntax, not just a stylistic suggestion:all the statements within any given single block must be indented to the same level,or <strong>Python</strong> reports a syntax error. This is intentional—because you don’t need toexplicitly mark the start and end of a nested block of code, some of the syntacticclutter found in other languages is unnecessary in <strong>Python</strong>.Making indentation part of the syntax model also enforces consistency, a crucialcomponent of readability in structured programming languages like <strong>Python</strong>.<strong>Python</strong>’s syntax is sometimes described as “what you see is what you get”—theindentation of each line of code unambiguously tells readers what it is associatedwith. This consistent appearance makes <strong>Python</strong> code easier to maintain and reuse.Consistently indented code always satisfies <strong>Python</strong>’s rules. Moreover, most texteditors (including IDLE) make it easy to follow <strong>Python</strong>’s indentation model by automaticallyindenting code as you type it.Statement DelimitersA statement in <strong>Python</strong> normally ends at the end of the line on which it appears.When a statement is too long to fit on a single line, though, a few special rules maybe used to make it span multiple lines:• Statements may span multiple lines if you’re continuing an open syntactic pair.<strong>Python</strong> lets you continue typing a statement on the next line if you’re codingsomething enclosed in a (), {}, or[] pair. For instance, expressions in parentheses,and dictionary and list literals, can span any number of lines; your statementdoesn’t end until the <strong>Python</strong> interpreter reaches the line on which you typethe closing part of the pair (a ), }, or]). Continuation lines can start at anyindentation level, and should all be vertically aligned.• Statements may span multiple lines if they end in a backslash. This is a somewhatoutdated feature, but if a statement needs to span multiple lines, you canalso add a backslash (\) at the end of the prior line to indicate you’re continuingon the next line. Because you can also continue by adding parentheses aroundlong constructs, backslashes are almost never used.<strong>Python</strong> Syntax Rules | 241

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!