Python Zero foundation to introductory column
】 Learn a series of articles Python3
Development environment of , Follow up tutorials are also Python3
Coding standards
It exists in all kinds of programming languages , Maybe some languages are not very intuitive
If you are a novice, learn to write code , So from the beginning Memorize coding rules It will have a great impact on the future compilation of specifications !
Here is a brief introduction to several coding specifications that beginners should keep in mind , It is divided into several aspects to introduce , Let's have a look !
Python use PEP 8
As coding specification , among PEP
yes Python Enhancement Proposal
(Python Enhancement proposal ) Abbreviation ,8 It stands for Python Code style guide .
Let's take a look at the code in a picture
Compare with the picture above Two pieces of code in can find , They contain exactly the same code
But the coding format on the right obviously looks more regular than the code snippet on the left , It will also be easier to read 、 Carefree , Because it follows the most basic Python Code specification .
The following is divided into several parts to learn Python Code specification of
, Make our code more beautiful 、 beautiful !
Generally speaking , The declaration encoding format is required in the script
If python The source code file There is no declared encoding format ,python The interpreter will default to ASCII code
But one disadvantage of this is , In case of non ASCII Encoded character ,python The interpreter will report an error
With UTF-8 For example , The following two kinds of encoding format statements are in line with the rules .
# -*- coding: utf-8 -*-
# coding = utf-8
And other programming languages ( Such as Java、C Language ) Use braces “{}” Separate code blocks are different ,Python Using code indents and colons ( : ) To distinguish the layers between blocks of code .
stay Python in , For class definitions 、 Function definition 、 Flow control statement 、 Exception handling statements, etc , The colon at the end of the line and the indent of the next line , Indicates the beginning of the next block of code , The end of the indentation indicates the end of the code block .
Be careful ,Python Indent the code in , You can use spaces or Tab Key implementation . But whether it's manual typing spaces , Or use Tab key , Usually use 4 Space length as an indent
( By default , One Tab The key means 4 A space ).
about Python Indent rules , Beginners can understand ,Python Require lines of code that belong to the same scope , They must be indented by the same amount , But the exact amount of indentation , No hard and fast rules .
Correct sample code :
a=1
if a==1:
print(" correct ") # Indent 4 Empty space
else: # And if alignment
print(" error ") # Indent 4 Empty space
Error example code :
a=1
if a==1:
print(" correct ")
else:
print(" error ")
print("end") # To correct, just delete the space in front of this line of code
Just remember a little : Unified use 4 Space to indent , Do not use tab, No more tab Mixed with Spaces
Remember that , Generally speaking, indentation will not cause too much problem !
Python Use in # Annotate , We are using # When ,# There should be a space behind the number
# Comment part
#
# Comment part
When commenting in the line , At least two spaces should be added in the middle
print(" Hello , The world ") # notes
Space
General principles used :
Usually , On both sides of the operator 、 Between function arguments and both sides of commas , It is recommended to use spaces to separate .
Blank line
General principles used :
Using the necessary blank lines can increase the readability of the code , Usually defined at the top level ( Such as the definition of a function or class ) There are two lines in between , There is a blank line between the method definitions , In addition, a line can be left blank in the position used to separate certain functions .
The import should always be at the top of the file , After module comments and document strings , Before module global variables and constants .
Imports should be grouped from the most common to the least common , Leave a line blank between groups :
Every import Statement imports only one module , Try to avoid importing multiple modules at once
# recommend
import os
import sys
# Not recommended
import os,sys
Everyone should be familiar with the naming convention , But the explicit specifications of different programming languages are also different ~
Python General principles for naming recommendations :
Python in , Single and double quotation marks can be used correctly in the output statement , But there are also corresponding coding specifications
So let's not add quotation marks at will , It is best to follow the following specifications !
General principles for the use of quotation marks :
Python It is very different from the semicolon used in several other mainstream programming languages
Python You don't need a semicolon at the end of your code , and Java and C# And so on
Don't add a semicolon to the end of a line , And don't use semicolons to put two commands on the same line , for example :
# Not recommended
print("Hello") ; print("World")
About beginners Python The basic coding specification probably needs special attention !
We followed the coding rules from the beginning , Develop a good coding habit , It is also a good start for learning programming !