In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
What are the knowledge points of this article "Python basic coding specifications?" most people do not understand, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Python basic coding specifications have what" article.
Python coding specification
Coding specifications exist in all kinds of programming languages, and some languages may not be very intuitive.
If you are a novice learning to write code, then memorizing the coding rules at the beginning will not have a great impact on future writing specifications!
The following is a brief introduction to some beginners to keep in mind several coding specifications, divided into several aspects to introduce, let's take a look at it!
Python uses PEP 8 as the coding specification, where PEP is the abbreviation of Python Enhancement Proposal (Python Enhancement recommendation), and 8 represents the style guide for Python code.
Let's take a look at the code in a picture first.
Comparing the two pieces of code in the figure above, we can see that they contain exactly the same code.
But the coding format on the right obviously looks more organized than the code snippet on the left, and it will be easier and easier to read because it follows the most basic Python coding specifications.
The following is divided into several parts to learn the coding specifications of Python to make our code more beautiful and beautiful!
Declare encoding format
In general, a declaration encoding format is required in a script
If the python source file does not declare an encoding format, the python interpreter uses ASCII encoding by default
But the drawback is that once non-ASCII-encoded characters appear, the python interpreter will report an error
In the case of UTF-8, the following two encoding format declarations are regular.
#-*-coding: utf-8-*-# coding = utf-8 indentation rule
Unlike other programming languages (such as Java and C) that use curly braces "{}" to separate code blocks, Python uses code indentation and colons (:) to distinguish the layers between code blocks.
In Python, for class definitions, function definitions, flow control statements, exception handling statements, etc., the colon at the end of the line and the indentation of the next line indicate the beginning of the next code block, and the end of the indentation indicates the end of the code block.
Note that code indentation can be implemented in Python using spaces or the Tab key. But whether you type spaces manually or use the Tab key, you usually use four space lengths as an indent (by default, a Tab key represents four spaces).
For Python indentation rules, beginners can understand that Python requires lines of code that belong to the same scope, their indentation must be the same, but the specific amount of indentation is not mandatory.
Correct sample code:
A=1if aegis 1: print ("correct") # indent 4 white space placeholders else: # align with if print ("error") # indent 4 white space placeholders
Error sample code:
A=1if axioms 1: print ("correct") else: print ("error") print ("end") # to correct, just delete the space in front of this line of code
Just remember one thing: indent 4 spaces uniformly, do not use tab, and do not mix tab and spaces
With this in mind, indentation is generally not a big problem!
Comment section
Python uses # to annotate. When we use #, we need a space after the # sign.
# comment section # # comment section
When commenting on an inline, you should add at least two spaces in the middle
Print ("Hello, World") # Note spaces
General principles for the use of spaces:
There is a space on each side of the binary operator, and the spaces on both sides of the arithmetic operator can be used flexibly, but both sides must be consistent.
Don't add spaces before commas, semicolons, and colons, but add them after them (except at the end of the line).
In the argument list of the function, there is a space after the comma.
Do not add spaces on both sides of the default equal sign in the parameter list of the function
Do not add spaces after the left parenthesis and before the closing parenthesis
No spaces should be added before the left parenthesis of a parameter list, index or slice
In general, spaces are recommended for separation on both sides of the operator, between function arguments, and on both sides of the comma.
Blank line usage
General principles for the use of blank lines:
Two lines between coding format declaration, module import, constant and global variable declaration, top-level definition, and executing code
Two lines between the top-level definitions and one line between the method definitions
Within a function or method, you can leave a line where necessary to enhance the rhythm, but continuous blank lines should be avoided
Use the necessary blank lines to increase the readability of the code, usually two lines between top-level definitions, such as the definition of a function or class, a blank line between method definitions, and a blank line at a location used to separate certain functions.
Module import part
Imports should always be placed at the top of the file, after module comments and documentation strings, and before module global variables and constants.
Imports should be grouped in the order from the most generic to the least generic, with a blank line between the groups:
Standard library import
Third-party library import
Application specifies import
Import only one module per import statement, try to avoid importing more than one module at a time
# recommended import osimport sys# does not recommend import os,sys naming convention
Everyone should be familiar with the naming conventions, but the explicit specifications between different programming languages are also different.
General principles that Python naming recommendations follow:
Try to name the module in lowercase, keep the first letter lowercase, and try not to use an underscore.
The class name uses the CamelCase naming style, the initial letter is capitalized, and the private class can begin with an underscore.
Function names are always lowercase. If there are multiple words, they are separated by underscores.
Private functions can start with an underscore
The variable names should be lowercase. If there are more than one word, separate them with an underscore.
Constants are in full uppercase. If there are more than one word, use an underscore to separate them.
Usage of quotation marks
In Python, it is correct to use single and double quotation marks in output statements, but there are also corresponding coding specifications.
So let's not add quotation marks at will, it's best to follow the following specifications!
General principles for the use of quotation marks:
Natural language uses double quotation marks
Machine identification using single quotation marks
Regular expressions use double quotation marks
The document string (docstring) uses three double quotes
Semicolon usage
The semicolon use of Python is very different from that of other mainstream programming languages.
There is no need to add a semicolon at the end of the code of Python, but Java and C # need to be added.
Do not add a semicolon at the end of a line, and do not use a semicolon to put two commands on the same line, for example:
# print ("Hello") is not recommended; the above print ("World") is about "what are the basic coding specifications of Python". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.