Hello, I’m Yue Chuang.

A brief introduction of PEP8

PEP8 is a guide to Python coding specifications that can help Python developers write elegant code and improve code readability.

Python PEP8 coding standards official documentation: www.python.org/dev/peps/pe…

Python PEP8 coding standard Chinese version: www.python.org/dev/peps/pe…

Second, code specification

For details, see the Official Python PEP8 coding specification document and the Chinese version of the Python PEP8 coding specification

(1) Code layout

  • Indent: Each indent contains four Spaces. It is not recommended to use Tab
  • Maximum line length: Each line has a maximum length of 79. Backslashes can be used for line breaks, but parentheses are recommended
  • Blank line: two empty lines between the top-level function and the defined class, and one empty line between the method definition in the class; A blank line between logically unrelated code segments within a function, and try not to have blank lines anywhere else

(2) The use of Spaces

  • Do not add a space before the closing parenthesis
  • Do not place Spaces before commas, colons, and semicolons
  • Do not place Spaces before the opening parentheses of functions, such as fun(1)
  • Do not precede the opening parenthesis of a sequence, as in list[2]
  • Operator with a space left and right, such as c = a + b
  • Do not write multiple statements on the same line
  • If, for, and while statements must start on a new line, even if there is only one statement executing them

conclusion

  • No space before it
  • Operator with a space left and right
  • Don’t write on the same line

(3) Code comments

  • Block comments: Block comments follow the annotated code, indented to the same level as the code. Block comments start with “#”
  • In-line comments: Along with the commented code, comments begin with “#” and are separated from the commented code by at least two Spaces
  • Docstrings: Docstrings are documentation written for all public modules, functions, classes, and methods. The docstrings are wrapped in triple quotes

(4) Naming conventions

  • Do not use the letters “L” (lowercase of L), “O” (uppercase O), “I” (uppercase I) for single-character variable names
  • Module and package names should be short and all lowercase
  • Function names should be lowercase, and you can use underscores to separate lowercase words
  • The first letter of a class name is usually capitalized
  • Constants are usually named in all uppercase

(5) Variable naming conventions

  • The value contains letters, digits, and underscores (_) and does not start with a number
  • Case sensitive
  • Familiar to understand, see the meaning
  • If two or more words are used, underline each other

AI yuecuang launched tutorials, including “Python language tutorials, C++ tutorials, algorithms/data structures tutorials, children’s programming”, all one-to-one teaching: one-to-one tutoring + one-to-one q&a + assignment + project practice, etc. QQ, wechat online, response at any time! V: Jiabcdefh