Step-by-step Python (1) Basic logic Control examples and coding style specifications

Source: Internet
Author: User

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.