1. 为什么要写注释
编写注释的主要目的是阐述代码要做什么,以及是如何做的。
注释会被python解释器忽略,不用执行。
在调试程序的过程中,注释还可以用来临时移除无用的代码。
在开发项目期间,你对各个部分的内容很清楚,但过段时间后,有些细节你可能不记得了。注释的
最大作用是提高程序的可读性,没有注释的程序简直就是折磨人!
2. 注释的两种方式
2.1 单行注释
在内容前面加#
eg:
#输出字符串
print("Hello,CSDN")
#输出整型变量
x = 7
print(x)
#输出浮点型
print(20.0 + 13**2)
2.2 多行注释
如果需要注释的文字有很多行,一行一行注释太麻烦,就需要用到多行注释;
在程序文件开头加上你的姓名,创作的日期,也用到自动生成的多行注释;
2.2.1 单引号'''
'''
1.多行注释:用三个单引号
2.设置一个整型变量
3.print打印输出
'''
x = 7
print(x)
2.2.2 双引号"""
"""
多行注释的第二种
三个双引号
"""
3. Python 之禅
要获悉如何编写优秀 Python 代码的指导原则,执行命令import this
就会得到
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
感兴趣的小伙伴可以自己尝试一下哈~
4. 快捷键
如果你觉得多行注释也很麻烦,可以用快捷键,
a:选中要注释的区域
b:按下Ctrl + / 键
就会变成,多个单行注释,如下:
# 1.我就是懒
# 2.多行注释里的六个引号也不想打
# 3.所以我选择用快捷键
标签:多行,better,Python,Day4,注释,print,than
From: https://blog.csdn.net/weixin_46027646/article/details/144248517