对程序中代码添加的说明,就是注释。注释是人能读懂的自然语言,不随程序运行,帮助人员理解对应程序的含义。
Python中用 # 标识来表示注释,# 后面的内容会被解释器忽略,也就是不被运行。
# 打印出温度
print('Now the temperature is 23 ℃')
D:\Python\python.exe "F:/py practice/day ady up/【Day 4 of Learning Python 】.py"
Now the temperature is 23 ℃
Process finished with exit code 0
在运行的过程中 ,Python编译器忽略了第一行带 # 的代码,从第二行代码开始执行。
总结:
代码中写注释的主要目的是表明该句代码的功能,方便他人或自己在阅读时的理解。
练习:
对Day 2 of Learning Python 中的代码加入注释:
# 对变量message赋值,并进行换行和制表符的操作
message = "NOTICE:\n\tit will rain tomorrow. please take your umbrella!"
# 打印变量message
print(message.title())
D:\Python\python.exe "F:/PY练习/practice/Day 2 of Learning Python.py"
Notice:
It Will Rain Tomorrow. Please Take Your Umbrella!
Process finished with exit code 0