变量
- message = 'Hello Python world!' # message 是自定义的一个变量名, 将值'Hello Python world!'存储到该变量中
- print(message)
输出: Hello Python world!
- message = 'Hello Python Crash Course world!' # 允许对变量的值进行修改
- print(message)
输出: Hello Python Crash Course world!
变量的命名与使用
变量名只能包含字母, 数字, 下划线. 但是不能以数字开头.
变量名不能包含空格.
不要将 Python 关键字和函数名用作变量名, 例如 Print.
变量名应既简短又具有描述性.
慎用小写字母 l 和大写字母 O, 它因它们可能被人错看成数字 1 和 0.
就目前而言, 应使用小写的 Python 变量名. 在变量名中使用大写字母虽然不会导致错误, 但避免使用大写字母是个不错的主意.
了解错误
当运行有问题的代码时, 解释器会报错, 它会提供一个 Traceback. 这是一条记录, 指出了解释器尝试运行代码时, 在什么地方陷入了困境.
例如, 将变量 message 名拼写错
- message = 'Hello Python Crash Course world!'
- print(mesage)
报错内容如下
- Traceback (most recent call last):
- File "C:/Users/Administrator/Desktop/a.py", line 3, in <module>
- print(mesage)
- NameError: name 'mesage' is not defined
说明
File "C:/Users/Administrator/Desktop/a.py", line 3, in <module> 显示在哪个文件的第几行存在问题
print(mesage) 将有问题的语句列出来
NameError: name 'mesage' is not defined 告诉编程人员这个错误是因为变量没有被定义而引起的
字符串
字符串就是一系列字符; 在 Python 中, 用引号括起来的都是字符串, 其中的引号可以是单引号, 也可以是双引号
- 'This is a string.'
- "This is a string."
利用这种便利性, 可以在字符串中包含引用号和撇号
print('I told my friend,"Python is my favorite language!"')
输出: I told my friend, "Python is my favorite language!"
print("The language'Python'is named after Monty Python, not the snake.")
输出: The language 'Python' is named after Monty Python, not the snake.
注: 在使用时注意单引号与双引号之间的嵌套关系, 避免语法错误
使用方法修改字符串大小
- name = "ada lovelace"
- print(name.title()) # title()方法表示将每个单词的首字母都改为大写
输出: Ada Lovelace
方法是 Python 可对数据执行的操作; 通过'.'来调用方法; 方法后面都跟着一对括号
- print(name.upper()) # upper() 将字符串全部改为大写 ADA LOVELACE
- print(name.lower()) # lower() 将字符串全部改为小写 ada lovelace
合并 (拼接) 字符串
- first_name = 'ada'
- last_name = 'lovelace'
- full_name = first_name+" "+last_name
- print(full_name)
输出: ada lovelace
Python 使用加号 (+) 来合并字符串
使用制表符或换行符来添加空白
制表符用 \ t 表示
print("Hello\tPython")
输出: Hello Python
换行符用 \ n 表示
print("Hello\nPython")
输出:
Hello
Python
删除空白
- favorite_language = 'python' # 在末尾添加了一个空格
- print(favorite_language.rstrip()) # rstrip()方法可将字符串开头与结尾的空格去掉
注: rstrip()只是将去掉空格后的字符串返回, 并没有直接在原来的变量中操作, 所以 favorite_language 中的值并没有发生改变; 可以采用下面方式改变变量去掉空格后的值
- favorite_language.lstrip() # lstrip()删除开头的空格
- favorite_language.strip() # strip()删除两端空格
数字
整数
举例: 对整数进行加, 减, 乘, 除运算
2+3 2-3 2*3 2/3
注: 23 表示乘方运算
6**(1+3) # 用括号可以改变运算的顺序
浮点数
Python 将带小数点的数字都称为浮点数
- 0.1 + 0.1
- 0.2 - 0.1
- 2 * 0.1
- 3 / 0.2
注: 结果包含的小数位数可能是不确定的, 这是计算机表示数字的方式, 可忽略
- >>> 0.2 + 0.1
- 0.30000000000000004
使用 str()将数字转换成字符串
- age = 23
- message = "Happy"+str(age)+"rd Birthday!"
- print(message)
输出: Happy 23rd Birthday!
注释
注释让你能够使用自然语言在程序中添加说明
在 Python 中, 注释用 #号标识.# 号后面的内容都会被 Python 解释器忽略
- # 向大家问好
- print("Hello Python people!")
第一行将被忽略, 第二行才会被执行
- # 表示单行的注释, 用成对的三个引号可表示多行的注释, 引号可以是单引号也可以是双引号
- '''
- 大家好
- 这是多行注释
- '''print("Hello Python!")
Python 之禅
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!
来源: http://www.jianshu.com/p/89d9e12179a4