python学习手册 简记

来源:互联网 发布:php批量解压文件修改 编辑:程序博客网 时间:2024/05/21 18:40

正则例子

匹配hello开头 world结尾字符串 中间的任意字符保存在group中.

import rematch = re.match('Hello[ \t]*(.*)world','Hello    python world')match.group(1)'python'
match = re.match('/(.*)/(.*)/(.*)','/usr/home/lumberjack')match.groups()('usr','home','lumberjack')

列表解析

[row[1] for row in M if row[1]%2 == 0]

字典

对字典的key排序并输出

#D是一个字典for key in sorted(D):    print(key,"=>",D[key])

类型检验

if isinstance(L,list):    print('yes')

表达式操作

x or y   # 逻辑或 只有x为假的的时候才会计算yx and y  # 逻辑与 只有x为真的时候才会计算yx//y     # floor 除法会把余数舍弃比较操作符可以连续使用: X<Y<Z 的结果与 X<Y and Y<Z 相同

数字

浮点数缺乏精确性

In [30]: 0.1+0.1+0.1-0.3Out[30]: 5.551115123125783e-17In [31]: round(0.1+0.1+0.1-0.3)Out[31]: 0.0

集合

S.add((1,2,3))# 也可以利用并集添加元素S | {(1,2,3)}{1.23, (1, 2, 3)}
0 0