程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Introduction to Python] 01 Python Comments, Lines and Indentation and PEP 8 Coding Specifications

編輯:Python

01 Python注釋、line with indent and PEP 8編碼規范

  • 注釋
    • 單行注釋
      • Explain a single line of code
      • Explain multiple lines of code
    • 多行注釋
    • 注意事項
  • 行與縮進
    • 縮進
    • 空行
    • 多行語句
    • 同一行顯示多條語句
    • 代碼組
  • PEP 8編碼規范

注釋

注釋(Comments)The role is used to prompt or explain to the user the role and function of some code,它可以出現在代碼中的任何位置.Python 解釋器在執行代碼時會忽略注釋,不做任何處理.

Python 支持兩種類型的注釋,分別是單行注釋和多行注釋.

單行注釋

PythonSingle-line comments are marked with a pound sign # 開頭,語法為:# 注釋內容 .

Python解釋器會忽略#after the entire line.

Explain a single line of code

A single-line comment when describing a single line of code is placed to the right of the line where the code is located:

print("HelloWorld") # This is a one-line comment explaining a single line of code

Explain multiple lines of code

Single-line comments describing multiple lines of code are placed on the line above the first line of code:

# This is a single-line comment explaining multiple lines of code
print("HelloWorld")
print("This is the first python program")

Tips:

  • Pycharm中執行/The shortcut key to uncomment a single line is CTRL+/ ,You can also select multiple lines at once to turn each line into a single-line comment.

多行注釋

PythonA multi-line comment can use three consecutive single quotes'''或者三個連續的雙引號""",語法為:

''' Use a multi-line comment with three consecutive single quotes Use a multi-line comment with three consecutive single quotes '''
""" Use a multi-line comment with three consecutive double quotes Use a multi-line comment with three consecutive double quotes """

注意事項

  1. PythonNested use of multi-line comments is not supported,Although the two types of multi-line comments can be nested(從技術上講,This is using string literals,而不是注釋),But if you have to comment out a block of code that already contains many other triple reference comments,The only safe option is to comment out each line.The following spelling is wrong(雖然可能IDE不報錯):

    """ 第一層 “”“ 第二層 ”“” """
    
  2. 當注釋符作為字符串的一部分出現時,It should be treated as part of normal code,例如:

    print("#HelloWorld")
    

    輸出的就是#HelloWorld .But more notably,Python中字符串如果以單引號、雙引號標識和三引號標識開頭,則字符串結尾也必須是對應的標識,Both can be used as the beginning and end of the string.

    However, when the string itself contains single quotes,The string can only be enclosed in double or triple double quotes:

    print("'HelloWorld'")
    print("""'HelloWorld'""")
    

    輸出均為:

    'HelloWorld'
    'HelloWorld'
    

    When the string itself contains double quotes,The string can only be enclosed in single or triple single quotes:

    print('"HelloWorld"')
    print('''"HelloWorld"''')
    

    輸出均為:

    "HelloWorld"
    "HelloWorld"
    

    When the string itself contains single and double quotes,The string can only be enclosed in triple single quotes:

    print(''''HelloWorld"''')
    

    輸出均為:

    'HelloWorld"
    

    When the string contains multiple lines,Use only triple quotes:

    print('''I Love You''')
    print("""I Love You""")
    

    輸出均為:

    I
    Love
    You
    I
    Love
    You
    

行與縮進

pythonThe most characteristic is the use of indentation and colons:來表示代碼塊,不需要使用大括號{} .

縮進

PythonIndenting the code can be achieved using the space bar or Tab鍵,縮進的空格數是可變的,But usually used4個空格長度作為一個縮進量(i.e. one by default Tab The number of spaces the key represents).

PythonThe code indentation requirements are very strict,同一個代碼塊的語句必須包含相同的縮進空格數.

if True:
print ("Hello")
print ("World")
else:
print ("Hello")
print ("World")

The above code is inconsistent with the indentation of the code block due to the indentation of the last line,會報以下錯誤:

IndentationError: unindent does not match any outer indentation level

Tips:

PycharmThe shortcut key for indenting multiple lines of code forward is to press after selecting multiple linesTab ,The shortcut key for indenting back is to select multiple lines and then pressShift+Tab .

空行

函數之間或類的方法之間用空行分隔,表示一段新的代碼的開始.類和函數入口之間也用一行空行分隔,以突出函數入口的開始.

空行與代碼縮進不同,Blank lines, although part of the program code, are notPython語法的一部分.So no blank lines are insertedPython解釋器運行也不會出錯.但是空行的作用在於分隔兩段不同功能或含義的代碼,便於日後代碼的維護或重構.

多行語句

如果一行PythonWhen the sentence is too long,可以用反斜槓\ 實現多行語句:

total = a + \
b + \
c

[], {}, 或()中的多行語句,不需要使用反斜槓\

total = (a +
b +
c)

Tips:

PEP 8Coding conventions recommend using parentheses to connect multi-line statements,而不推薦使用反斜槓\進行連接.

同一行顯示多條語句

Python可以在同一行中使用多條語句,語句之間使用分號; 分隔:

print("Hello"); print("World")

However, it is not suitable to write a multi-line statement on one linePEP 8代碼規范,並不推薦.

代碼組

縮進相同的一組語句構成一個代碼塊,我們稱之代碼組.

ifwhiledefclass這樣的復合語句,首行以關鍵字開始,以冒號:結束,Forms a code group with one or more lines of code following that line.

我們將首行及後面的代碼組稱為一個子句(clause).

if True:
print ("Hello")
print ("World")
else:
print ("Hello")
print ("World")

The above code contains two code groups,兩個字句.

PEP 8編碼規范

Python采用PEP 8編碼規范,官方介紹為:[https://peps.python.org/pep-0008/](PEP 8)

Some common encoding specifications are :

  1. 單行注釋#There should be a space after it:

    # 單行注釋
    
  2. A single-line comment placed to the right of the code should be two spaces away from the code:

    print("HelloWorld") #單行注釋
    
  3. The last line of the code is a blank line:

    print("HelloWorld")
    
  4. 每個importThe statement imports only one module at a time:

    import os
    import sys
    
  5. 在運算符兩側、between function arguments and after commas(逗號前不能有空格),使用空格進行分隔:

    a, b = 0
    c = a + b
    
  6. 不要在行尾添加分號,Also do not separate multiple statements on the same line with a semicolon:

    print("Hello"); print("World")
    
  7. 一般情況下,每行不超過80個字符,如果超過,使用小括號()將多行內容連接起來,instead of backslashes\進行連接.

    total = (a +
    b +
    c)
    

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved