
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
3.2 赋值与注释

Python变量在使用前必须赋值,否则会报错。与其他语言相同,Python也使用等号“=”作为赋值符号,例如:

在Python中单行注释以“#”开头,单行注释可以单独占一行,也可以在同一行的代码右边出现,例如:


需要注意的是,一行中“#”右侧的所有字符均被认为是注释内容,因此下述代码中的“print(test)”将不被执行。

当注释内容超过一行时,可以在每行开头都使用“#”形成多行注释,还可以使用“'''”(连续3个英文半角单引号)和“"""”(连续3个英文半角双引号)将多行注释内容包括起来,例如:


我们知道,Python源程序文件实际上是后缀为.py的文本文件,而文本文件在存储时会使用相应的字符集,中文字符集通常是UTF-8或GBK。而在编写Python程序的时候,避免不了会出现或是用到中文,此时需要在文件开头加上使用的字符集的中文注释。如果程序文件开头不声明使用的是什么字符集,那么默认使用ASCII字符集读取程序文件,此时即使中文内容包含在注释里面,代码仍然会报错。声明文档使用的字符集的方法如下:

或是:
