本章学习内容
- 如何使用变量,如何创建描述性变量名以及如何消除名称错误和语法错误
- 字符串是什么,以及如何使用小写、大写和首字母大写方式显示字符串
- 使用空白来显示整洁的输出,以及如何剔除字符串中多余的空白
- 如何使用整数和浮点数,使用数值数据时需要注意的意外行为
- 如何编写说明性注释,让代码更容易理解
注意事项
变量
- 变量名只能包含字母、数字和下划线。变量名可以字母或下划线打头,但不能以数字打头,例如,可将变量命名为message_1,但不能将其命名为1_message。
- 变量名不能包含空格,但可使用下划线来分隔其中的单词。例如,变量名greeting_message可行,但变量名greetingmessage会引发错误。
- 不要将Python关键字和函数名用作变量名,即不要使用Python保留用于特殊用途的单词,如print。
- 变量名应既简短又具有描述性。例如,student_name比s_n好,name_length比length_of_persons_name好。
- 慎用小写字母l和大写字母O,因为它们可能被人错看成数字1和0。
- 应尽量使用小写的Python变量名
- 变量名称错误通常意味着两种情况:要么是使用变量前忘记了给它赋值,要么是输入 变量名时拼写不正确。
字符串
- 在Python中,用引号括起的都是字符串,其中的引号可以是单引号,也可以是双引号,这种灵活性让你能够在字符串中包含引号和撇号
注释
- 编写注释的主要目的是阐述代码要做什么,以及是如何做的。通过编写注释,以清晰的自然语言对解决方案进行概述,可节省很多时间。
- 编写有意义的注释。作为新手,最值得养成的习惯之一是,在代码中编写清晰、简洁的注释。
- 如果不确定是否要编写注释,就问问自己,找到合理的解决方案前,是否考虑了多个解决方案。如果答案是肯定的,就编写注释对你的解决方案进行说明。相比回过头去再添加注释,删除多余的注释要容易得多。
Python 之禅
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!

来源:https://www.cnblogs.com/eternal-immortal/p/9708767.html