天天看点

Introducing Python Statements---Learning Python Chapter10

1. Statements(语句): the thins you tell Python what your programs should do.

     in a informal way: programs are "do things with stuff', then statements are the way you specify what sort of things a program does. 

2. Python statements: assignment/calls and other expressions/print calls/for else/while else/break/continue/def/return/yield....

Introducing Python Statements---Learning Python Chapter10
Introducing Python Statements---Learning Python Chapter10

3. syntax indentation :It essentially means that you must line up your code vertically, in columns, according to its logical structure. The net effect is to make your code more consistent and

readable (unlike much of the code written in C-like languages).

Python is a WYSIWYG: What You See Is What You Get-The way code looks is the way it runs, regardless of who coded it.

4. 1) the end of a line terminates the statement on that line(without semicolons like C)

     2) Nested statements are blocked and associated by their physical indentation(without braces like C)

5. Statement rule special cases:

1) a = 1; b = 2; print(a + b) # Three statements on one line, this only works when the statements are not compound statements ( in other words, you can only chain simple     statements)

2) you can make a single statement span across multiple lines.(use [ ], ( ), { })

    Any code enclosed in these constructs can cross multiple lines: your statement doesn’t end until Python reaches the line containing the closing part of the pair.

3) if x > y: print(x): code single-line if/while/for statements, only works when the body of the compound statement itself does not contain any compound statement

In general, even though it’s not always required, if you keep all your statements on individual lines and always indent your nested blocks, your code will be easier to read and change in the future

6. if..elif...else

   while...else

   for...else

   try ...except..else/finally (else part will be run if no exception happens)