Python 教程
/ 注釋
注釋
創(chuàng)建注釋
注釋以 #
開(kāi)頭,Python 將忽略它們:
實(shí)例
#This is a comment print("Hello, World!")
運(yùn)行實(shí)例
注釋可以放在一行的末尾,Python 將忽略該行的其余部分:
實(shí)例
print("Hello, World!") #This is a comment
運(yùn)行實(shí)例
注釋不必是解釋代碼的文本,它也可以用來(lái)阻止 Python 執(zhí)行代碼:
實(shí)例
#print("Hello, World!") print("Cheers, Mate!")
運(yùn)行實(shí)例
多行注釋
Python 實(shí)際上沒(méi)有多行注釋的語(yǔ)法。
要添加多行注釋?zhuān)梢詾槊啃胁迦胍粋€(gè) #
:
實(shí)例
#This is a comment #written in #more than just one line print("Hello, World!")
運(yùn)行實(shí)例
或者,以不完全符合預(yù)期的方式,您可以使用多行字符串。
由于 Python 將忽略未分配給變量的字符串文字,因此您可以在代碼中添加多行字符串(三引號(hào)),并在其中添加注釋?zhuān)?/p>
實(shí)例
""" This is a comment written in more than just one line """ print("Hello, World!")
運(yùn)行實(shí)例
只要字符串未分配給變量,Python 就會(huì)讀取代碼,然后忽略它,這樣您就已經(jīng)完成了多行注釋。