Python 基础速查表
数据类型
| Integer | -256, 15 |
|---|---|
| Float | -253.23, 1.253e-10 |
| String | “Hello”, ‘Goodbye’, “””Multiline””” |
| Boolean | True, False |
| List | [ value, … ] |
| Tuple | ( value, … ) |
| Dictionary | { key: value, … } |
| Set | { value, value, … } |
语句
| If 语句``` if expression: statements elif expression: statements else: statements
**While Loop**
while expression: statements
**For Loop**
for var in collection: statements
**Counting For Loop**
for i in range(start, end [, step]): statements (start is included; end is not)
|| --- |<a name="articleHeader2"></a>#### 算术运算符| x + y | 加 | x - y | 减 || --- | --- | --- | --- || x * y | 乘 | x / y | 除 || x % y | 取模 | x ** y | xy || Assignment shortcuts: x _op_= y<br />示例: x += 1 递增 x | | | |<a name="articleHeader3"></a>#### 比较运算符| x< y | 小于 | x <= y | 小于等于 || --- | --- | --- | --- || x > y | 大于 | x >= y | 大于等于 || x == y | 相等 | x != y | 不等 |<a name="articleHeader4"></a>#### 布尔运算符| not x | x and y | x or y || --- | --- | --- |<a name="articleHeader5"></a>#### 转换函数| int(_expr_) | 将_expr_转成整型 || --- | --- || float(_expr_) | 将_expr_转成浮点型 || str(_expr_) | 将_expr_转成字符串 || chr(_num_) | ASCII char _num_ |<a name="articleHeader6"></a>#### String / List / Tuple 操作| len(_s_) | _s_长度 || --- | --- || _s_[_i_] | 取_s_中的第_i_个值 (从0开始) || _s_[_start_ :_end_] | 从开始(包括)到结束(不包括)的片段 || _x_ in _s_ | **如果x包含在s中则为true** || _x_ not in_s_ | **如果x不包含在s中,则为true** || _s_ + _t_ | 把s与t的相连接 || _s_ * _n_ | 将_s_复制_n_份 || sorted(_s_) | 对s进行排序 || _s_.index(_item_) | item在s中的位置 |<a name="articleHeader7"></a>#### 更多字符串操作| _s_.lower() | 转成小写 || --- | --- || _s_.replace(_old_,_new_) | 把 s 中的 old 替换成 new || _s_.split( _delim_ ) | 由delim分隔的子字符串列表 || _s_.strip() | 用于移除字符串头尾的空格 || _s_.upper() | 转成大写 || 更多 [http://docs.python.org/library/stdtypes.html#string-methods](http://docs.python.org/library/stdtypes.html#string-methods) | |<a name="articleHeader8"></a>#### Mutating List 操作| del _lst_[_i_] | 删除列表中的第i个项目 || --- | --- || _lst_.append(_e_) | 将e追加到lst中 || _lst_.insert(_i_, _e_) | 在第i个项目前插入_e_ || _lst_.sort() | 排序_lst_ |<a name="articleHeader9"></a>#### 字典操作| len(_d_) | d中的项目数 || --- | --- || del _d_[_key_] | 根据key从d中删除 || _key_ in _d_ | 如果d包含key,则为true || _d_.keys() | 返回d中的key列表 |<a name="articleHeader10"></a>#### 函数定义|
def name(arg1, arg2, …): statements return expr
|| --- |<a name="articleHeader11"></a>#### Environment| sys.argv | 命令行参数列表(argv [0]可执行) || --- | --- || os.environ | 环境变量字典 || os.curdir | 当前目录路径 ||
import sys; print(sys.argv) or from sys import argv; print(argv)
| |<a name="articleHeader12"></a>#### 实用的函数| exit( _code_ ) | 使用exitcode终止程序 || --- | --- || raw_input("_prompt_") | 从stdin打印 _prompt_ 和 readline() || 在Python 3使用input("_prompt_") | |<a name="articleHeader13"></a>#### 字符串格式化|
“Hello, {0} {1}”.format(“abe”, “jones”) Hello, abe jones “Hello, {fn} {ln}”.format(fn=”abe”, ln=”jones”) Hello, abe jones “You owe me ${0:,.2f}”.format(253422.3) You owe me $253,422.30 now = datetime.now() ‘{:%Y-%m-%d %H:%M:%S}’.format(now) 2016-12-16 15:04:33 ``` | | —- |
代码片段
| 循环序列 for index, value in enumerate(seq): print(”{} : {}”.format(index, value)) 循环字典 for key in sorted(dict): print(dict[key]) 读取一个文件 with open(“filename”, “r”) as f: for line in f: line = line.rstrip(”\n”) # Strip newline print(line) |
|---|
