程序里的注釋是很重要的。它們可以用自然語(yǔ)言告訴你某段代碼的功能是什么。在你想要臨時(shí)移除一段代碼時(shí),你還可以用注解的方式將這段代碼臨時(shí)禁用。接下來(lái)的練習(xí)將讓你學(xué)會(huì)注釋:
1 2 3 4 5 6 7 8 9 | # A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print "I could have code like this." # and the comment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print "This will run."
|
從現(xiàn)在開(kāi)始,我將用這樣的方式來(lái)寫(xiě)代碼。我一直在強(qiáng)調(diào)“完全相同”,不過(guò)你也不必按照字面意思理解。你的程序在屏幕上的顯示可能會(huì)有些不同,不過(guò)重要的是你在文本編輯器中輸入的文本的正確性。事實(shí)上,我可以用任何編輯器寫(xiě)出這段程序,而且內(nèi)容是完全一樣的。
$ python ex2.py
I could have code like this.
This will run.
$
再次說(shuō)明,我不會(huì)再貼各種屏幕截圖了。你應(yīng)該明白上面的內(nèi)容是輸出內(nèi)容的字面翻譯,而 $ python ... 和最后的 $ 之間才是你應(yīng)該關(guān)心的內(nèi)容。