Python 基础速查表

数据类型

Integer -256, 15
Float -253.23, 1.253e-10
String “­Hel­lo”, ‘Goodbye’, “­”­”­Mul­til­ine­”­”­”
Boolean True, False
List [ value, … ]
Tuple ( value, … )
Dictionary { key: value, … }
Set { value, value, … }

语句

| If 语句``` if expre­ssion: ­ ­sta­tements elif expre­ssion: ­ ­sta­tements else: ­ ­sta­tements

  1. **While Loop**

while expre­ssion: ­ ­sta­tements

  1. **For Loop**

for var in colle­ction: ­ ­sta­tements

  1. **Counting For Loop**

for i in range(­st­art, end [, step]): ­ ­sta­tements (start is included; end is not)

  1. |
  2. | --- |
  3. <a name="articleHeader2"></a>
  4. #### 算术运算符
  5. | x + y | | x - y | |
  6. | --- | --- | --- | --- |
  7. | x * y | | x / y | |
  8. | x % y | 取模 | x ** y | xy |
  9. | Assignment shortcuts: x _op_= y<br />示例: x += 1 递增 x | | | |
  10. <a name="articleHeader3"></a>
  11. #### 比较运算符
  12. | x< y | 小于 | x <= y | 小于等于 |
  13. | --- | --- | --- | --- |
  14. | x > y | 大于 | x >= y | 大于等于 |
  15. | x == y | 相等 | x != y | 不等 |
  16. <a name="articleHeader4"></a>
  17. #### 布尔运算符
  18. | not x | x and y | x or y |
  19. | --- | --- | --- |
  20. <a name="articleHeader5"></a>
  21. #### 转换函数
  22. | int(_e­xpr_) | _expr_转成整型 |
  23. | --- | --- |
  24. | float_expr_) | _expr_转成浮点型 |
  25. | str(_e­xpr_) | _expr_转成字符串 |
  26. | chr(_num_) | ASCII char _num_ |
  27. <a name="articleHeader6"></a>
  28. #### String / List / Tuple 操作
  29. | len(_s_) | _s_长度 |
  30. | --- | --- |
  31. | _s_[_i_] | _s_中的第_i_个值 (从0开始) |
  32. | _s_[_s­tart_ :_end_] | 从开始(包括)到结束(不包括)的片段 |
  33. | _x_ in _s_ | **如果x包含在s中则为true** |
  34. | _x_ not in_s_ | **如果x不包含在s中,则为true** |
  35. | _s_ + _t_ | st的相连接 |
  36. | _s_ * _n_ | _s_复制_n_ |
  37. | sorted­(_s_) | s进行排序 |
  38. | _s_.in­dex­(_i­tem_) | items中的位置 |
  39. <a name="articleHeader7"></a>
  40. #### 更多字符串操作
  41. | _s_.lo­wer() | 转成小写 |
  42. | --- | --- |
  43. | _s_.re­pla­ce_old_,_new_) | s 中的 old 替换成 new |
  44. | _s_.split( _delim_ ) | delim分隔的子字符串列表 |
  45. | _s_.st­rip() | 用于移除字符串头尾的空格 |
  46. | _s_.up­per() | 转成大写 |
  47. | 更多 [http:/­/do­cs.p­yt­hon.or­g/l­ibr­ary­/st­dty­pes.ht­ml#­str­ing­-me­thods](http://docs.python.org/library/stdtypes.html#string-methods) | |
  48. <a name="articleHeader8"></a>
  49. #### Mutating List 操作
  50. | del _lst_[_i_] | 删除列表中的第i个项目 |
  51. | --- | --- |
  52. | _lst_.a­pp­end­(_e_) | e追加到lst |
  53. | _lst_.i­ns­ert­(_i_, _e_) | 在第i个项目前插入_e_ |
  54. | _lst_.s­ort() | 排序_lst_ |
  55. <a name="articleHeader9"></a>
  56. #### 字典操作
  57. | len(_d_) | d中的项目数 |
  58. | --- | --- |
  59. | del _d_[_key_] | 根据keyd中删除 |
  60. | _key_ in _d_ | 如果d包含key,则为true |
  61. | _d_.keys() | 返回d中的key列表 |
  62. <a name="articleHeader10"></a>
  63. #### 函数定义
  64. |

def name­(a­rg1, arg2, …): ­ ­st­ate­ments ­ ­return expr

  1. |
  2. | --- |
  3. <a name="articleHeader11"></a>
  4. #### Enviro­nment
  5. | sys.argv | 命令行参数列表(argv [0]可执行) |
  6. | --- | --- |
  7. | os.environ | 环境变量字典 |
  8. | os.curdir | 当前目录路径 |
  9. |

import sys; print(­sys.ar­gv)­ ­ ­ ­ or from sys import argv; print(­argv)

  1. | |
  2. <a name="articleHeader12"></a>
  3. #### 实用的函数
  4. | exit( _code_ ) | 使用exitcode终止程序 |
  5. | --- | --- |
  6. | raw_in­put­("_p­rom­pt_­") | stdin打印 _prompt_ readline() |
  7. | Python 3使用input"­_pr­omp­t_") | |
  8. <a name="articleHeader13"></a>
  9. #### 字符串格式化
  10. |

“­Hello, {0} {1}”.fo­rma­t(“a­be”, “­jon­es”) Hello, abe jones “­Hello, {fn} {ln}”.f­orm­at(­fn=­”­abe­”, ln=”­jon­es”) Hello, abe jones “You owe me ${0:,.2­f}­”.fo­rma­t(2­534­22.3) You owe me $253,4­22.30 now = dateti­me.n­ow() ‘{:%Y-­%m-%d %H:%M:­%S}­’.f­orm­at(now) 2016-­12-16 15:04:33 ``` | | —- |

代码片段

循环序列
for index, value in enumer­ate­(seq):
­ ­pri­nt(­”{} : {}”.f­or­mat­(index, value))

循环字典
for key in sorted­(dict):
­ ­pri­nt(­dic­t[key])

读取一个文件
with open(“f­ile­nam­e”, “­r”) as f:
­ for line in f:
­ ­ ­ line = line.r­str­ip(­”­\n”) # Strip newline
­ ­ ­ ­pri­nt(­line)