命名
- 变量名只能包含字母、数字和下划线。变量名可以字母或下划线打头,但不能以数字打
头 - 变量名不能包含空格,但可使用下划线来分隔其中的单词。
- 不要将Python关键字和函数名用作变量名,
- 变量名应既简短又具有描述性
- 慎用小写字母l和大写字母O,因为它们可能被人错看成数字1和0。
字符串
引号
- 用引号括起的都是字符串,其中的引号可以是单引号,也可以是双引号
- 通过同时使用"" ‘’,或者(例如str的内容为’,表示方法为"’"或者’’’)
修改大小写
- a.title() :以首字母大写的方式显示每个单词,即将每个单词的首字母都改为大写
- a.upper():单词的每个字母改为大写
- a.lower():单词的每个字母改为小写
合并(拼接)
- 使用‘+’
添加空白
- 在编程中,空白泛指任何非打印字符,如空格、制表符和换行符。
- \n:换行
- \t:制表符
删除空白
- a.strip():删除字符串a首尾空白
- a.rstrip():删除字符串a末尾空白
- a.lstrip():删除字符串a开头空白
数字
整数
- 运算:+ - * / ; 乘方:**(3 **2=9)
浮点数
- 浮点数运算后包含的小数位数是不确定的
str(num)
- 非字符串值表示为字符串
注释
-#
内容
- 阐述代码要做什么,以及是如何做的。
python之禅
在终端输入import this
你会看到关于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!