(1) Basic logic Control Example and coding style specification
1.while dead Loop
2.for Cycle
3.if,elif,else Branch Judgment
4. Coding style (official recommendation)
Version: Python3.4
1.while dead Loop
Import time >>> i = 0 while 1: ... + = 1 ... Print (i) ... Time.sleep (3123^ctraceback (most recent) :"<stdin> " in <module>keyboardinterrupt
2.for Cycle
for in range (3): ... Print ("" + str ( i)) ... + = 1 ... Print ("" +== 1= 1= 2= 2
3.if,elif,else Branch Judgment
More if. py
x = Int (input ("Please enter an integer:"))ifX <0:x=0Print('negative changed to zero')elifx = =0:Print('Zero')elifx = = 1: Print(' Single')Else: Print(' More')
If-1if 0 if 1 if2 more
4. Coding style (official recommendation)
Use 4 spaces to indent rather than TAB.
Between small indents (which can be nested deeper) and large indents (easier to read), 4 spaces are a good compromise. TAB has caused some confusion, preferably deprecated.
Wrap the line to make sure it doesn't exceed 79 characters.
This helps small-screen users to read, or allows large displays to display several code files side-by-side.
Use empty lines to separate functions and classes, and large chunks of code in functions.
If possible, comment exclusive line
Working with document Strings
Place the space on either side of the operator and behind the comma, but no space is added to the brackets: a = f (1, 2) + g (3, 4) .
Uniform functions and class naming.
The recommended class names are named with the hump , and the function and method names are underlined in lowercase _ and _ . Always use self as the first parameter of the method (for knowledge of classes and methods see First Class ).
Do not use fancy coding if the purpose of your code is to be in an internationalized environment. Python by default, UTF-8, even ordinary ASCII always works best.
Also, do not use identifiers that are non-ASCII characters unless the code is read or maintained in a different language.
Step-by-step Python (1) Basic logic Control examples and coding style specifications