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.

As you’ve seen, there are no variable type declarations in <strong>Python</strong>; this fact alonemakes for a much simpler language syntax than what you may be used to. But, formost new users, the lack of the braces and semicolons used to mark blocks and statementsin many other languages seems to be the most novel syntactic feature of<strong>Python</strong>, so let’s explore what this means in more detail.Block Delimiters<strong>Python</strong> detects block boundaries automatically, by line indentation—that is, theempty space to the left of your code. All statements indented the same distance tothe right belong to the same block of code. In other words, the statements within ablock line up vertically, as in a column. The block ends when the end of the file or alesser-indented line is encountered, and more deeply nested blocks are simplyindented further to the right than the statements in the enclosing block.For instance, Figure 12-1 demonstrates the block structure of the following code:x = 1if x:y = 2if y:print 'block2'print 'block1'print 'block0'Block0Header-line:Block1Header-line:Block2Block0Block1Figure 12-1. Nested blocks of code: a nested block starts with a statement indented further to theright, and ends with either a statement that is indented less, or the end of the file.This code contains three blocks: the first (the top-level code of the file) is notindented at all, the second (within the outer if statement) is indented four spaces,and the third (the print statement under the nested if) is indented eight spaces.240 | Chapter 12: if Tests

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

Saved successfully!

Ooh no, something went wrong!