数学运算
1 abs(x) # 求绝对值 2 3 a = abs(-9) 4 print(a)
1 round(number, ndigits=None) #四舍五入取整数,ndigits表示取多少小数位。默认none 2 3 a = round(3.6) 4 b = round(3.4631,2) 5 print(a,b)
1 pow(*args, **kwargs) #幂运算,如果是两个参数就是x**y,三个参数是x**y % z 2 a = pow(2,3) #2**3 3 print(a) 4 5 a = pow(2,3,3) #2**3%3 6 print(a)
1 divmod(a, b) #获得除法结果和余数 2 a = divmod(22, 3) 3 print(a) #(7, 1)
1 max(arg1, arg2, *args[, key]) #获得最大值 2 a = max(324,123,56,125,75) 3 print(a)
1 min(arg1, arg2, *args[, key]) #获得最小值 2 a = min(324,123,56,125,75) 3 print(a)
1 sum(iterable[, start]) #求和 2 a = sum([324,123,34,654,23]) 3 print(a)
类型转换
1 int(x, base=10) #整形 ,base为所给参数的进制,默认十进制 2 a = int(36) 3 b = int(‘36‘,base=8) 4 c = int(‘0100111‘,base=2) 5 d = int(‘36‘,base=16) 6 print(a) #36 7 print(b) #30 8 print(c) #39 9 print(d) #54
1 float([x]) #浮点数 2 a = float(5) 3 print(a)
1 str(object=b‘‘, encoding=‘utf-8‘, errors=‘strict‘) #字符串 2 a = str(‘时间‘) 3 print(a)
1 complex([real[, imag]]) #复数 2 a = complex(3,6) 3 print(a) #(3+6j)
1 ord(c) #字符对应的数字 2 a = ord(‘S‘) 3 print(a) #83
1 chr(i) #数字对应的字符 2 a = chr(123) 3 print(a) #{
1 bool([x]) #布尔值 2 a = bool(0) 3 print(a) 4 #在Python中,下列对象都相当于False: [], (), {}, 0, None, 0.0, ‘‘
1 bin(x) #转换成二进制 2 a = bin(56) 3 print(a) #0b111000
1 hex(x) #转换成十六进制 2 a = hex(56) 3 print(a) #0x38
1 oct(x) #转换成八进制 2 a = oct(56) 3 print(a) #0o70
1 list([iterable]) #转换成列表 2 a = list((11,24,35,46,76)) 3 print(a)
1 tuple([iterable]) #转换成元组 2 a = tuple([11,24,35,46,76]) 3 print(a)
1 slice(start, stop[, step]) #实现切片对象,主要用在切片操作函数里的参数传递。 2 a = slice(4,20,2) 3 l = list(range(30)) 4 print(l[a]) #[4, 6, 8, 10, 12, 14, 16, 18] 5 #如果写成slice(20),表示为slice(none,20,none)
1 dict(**kwarg) #转换为字典 2 a = dict(aa=4,bb=20,cc=2) 3 print(a) #{‘aa‘: 4, ‘bb‘: 20, ‘cc‘: 2}
序列
1 all(iterable) #给的值都为True时返回True,否则返回false 2 a = all([True,1,"good"]) 3 print(a)
1 any(iterable) #给的值有一个为True就返回True 2 a = any(["", 0, False, [], None]) 3 print(a)
sorted(iterable[, key][, reverse]) #排序 a = sorted([15,2,22,6,32,5]) print(a) #[2, 5, 6, 15, 22, 32]
reversed(seq) #翻转 a = reversed([15,2,22,6,32,5]) for i in a: print(i)
编译、执行
1 repr(object) #转换成解释器读取的字符串形式 2 a = repr("中文") 3 print(a)
1 compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) 2 #将source编译为代码或者AST对象。代码对象能够通过exec语句来执行或者eval()进行求值。 3 #参数source:字符串或者AST(Abstract Syntax Trees)对象。 4 #参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。 5 #参数model:指定编译代码的种类。可以指定为 ‘exec’,’eval’,’single’。 6 #参数flag和dont_inherit:这两个参数暂不介绍,可选参数 7 8 a = "for i in range(0,10):print(i)" 9 cmpcode = compile(a,‘‘,‘exec‘) 10 exec(cmpcode) 11 12 str = "3 * 4 + 5" 13 a = compile(str,‘‘,‘eval‘) 14 eval(a)
eval(expression, globals=None, locals=None) #将str格式的字符串转换并计算,globals为全局变量,locals为局部变量 a = eval(‘2 + 87 - 6 * 2‘) print(a)
exec(object[, globals[, locals]]) #执行代码 exec(‘print("goof")‘)
其他
1 input([prompt]) #输入 2 a = input("please input word:") 3 print(a)
globals() #在当前作用域下查看全局变量 b=‘xiaodeng‘ print(globals) #<built-in function globals> print(globals()) #{ #‘b‘: ‘xiaodeng‘, #‘__builtins__‘: <module ‘__builtin__‘ (built-in)>, #‘__file__‘: ‘C:\\Users\\Administrator\\Desktop\\Test\\Test.py‘, #‘__package__‘: None, #‘__name__‘: ‘__main__‘, #‘__doc__‘: "\nglobals(...)\n globals() -> dictionary\n \n Return the #dictionary containing the current scope‘s global variables.\n" #}
1 locals() #查看局部变量 2 def foo(arg, a): 3 x = 1 4 y = ‘xxxxxx‘ 5 for i in range(10): 6 j = 1 7 k = i 8 print(locals()) 9 10 foo(10,4) 11 12 #{‘k‘: 9, ‘j‘: 1, ‘i‘: 9, ‘y‘: ‘xxxxxx‘, ‘x‘: 1, ‘a‘: 4, ‘arg‘: 10}
1 type(name, bases, dict) #查看类型 2 a = (11,22,33,44) 3 b = [11,22,33,44] 4 c = {‘aa‘:1,‘bb‘:2} 5 print(type(a)) 6 print(type(b)) 7 print(type(c)) 8 9 #<class ‘tuple‘> 10 #<class ‘list‘> 11 #<class ‘dict‘>
1 dir([object]) 当前范围的变量 2 print(dir()) 3 #[‘__annotations__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘]
help([object]) #帮助 help(repr)
1 len(s) #字符串长度 2 a = len("laugh") 3 b = len([11,22,44]) 4 print(a) 5 print(b)
1 open(file, mode=‘r‘, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) #操作文件
enumerate(iterable, start=0) #遍历所有元素并加上序列号 a = [‘aa‘,‘bb‘,‘cc‘,‘dd‘,‘ee‘,‘ff‘] i = enumerate(a,1) for k,v in i: print(k,v) #1 aa #2 bb #3 cc #4 dd #5 ee #6 ff
zip(*iterables) #打包 a = [‘aa‘,‘bb‘,‘cc‘,‘dd‘] b = [11,22,33,44] i = zip(a,b) print(list(i)) #[(‘aa‘, 11), (‘bb‘, 22), (‘cc‘, 33), (‘dd‘, 44)]
iter(object[, sentinel]) #迭代 a = [‘aa‘,‘bb‘,‘cc‘,‘dd‘] b = ‘you are right‘ i1 = iter(a) i2 = iter(b) for j1 in i1: print(j1) for j2 in i2: print(j2)
map(function, iterable, ...) #提供的函数对指定序列做映射 a = [11,22,33,44,55,66] m = map(lambda x : x + 3 ,a) print(list(m)) #[14, 25, 36, 47, 58, 69]
filter(function, iterable) #对函数对指定序列进行过滤 def s(x): return x>36 a = [11,22,33,44,55,66] m = filter(s,a) print(list(m)) #[44, 55, 66]
ascii(object) 跟repr()函数一样,返回一个可打印的对象字符串方式表示
bytearray([source[, encoding[, errors]]]) #返回一个新字节数组。这个数组里的元素是可变的,并且每个元素的值范围: 0 <= x < 256 #1. 如果source是一个字符串,那么必须给出endcoding是什么样编码的,以便转换为合适的字节保存。 #2. 如果source是一个整数,那么这个数组将初始化为空字节。 #3. 如果source是一个有缓冲区接口的对象,那么只读的接口初始到数组里。 #4. 如果source是一个迭代对象,那么这个迭代对象的元素都必须符合0 <= x < 256,以便可以初始化到数组里。
callable(object) #检查对象object是否可调用。如果返回True,object仍然可能调用失败;但如果返回False,调用对象ojbect绝对不会成功。 callable(0) print(callable("mystring")) def add(a, b): return a + b print(callable(add)) #False #True
frozenset([iterable]) #返回一个冻结的集合 a = [1,2,4,5,6,7] b = frozenset(a) print(b) #frozenset({1, 2, 4, 5, 6, 7})
isinstance(object, classinfo) #来判断一个对象是否是指定的类型 a = 56 b = isinstance(a,int) print(b) #True
issubclass(class, classinfo) #判断类参数class是否是类型参数classinfo的子类 class fa(): pass class ch(fa): pass a = issubclass(ch,fa) print(a) #True
set() #集合 a = ‘good‘ b = set(a) print(b) b.add(‘er‘) print(b) #{‘d‘, ‘g‘, ‘o‘} #{‘d‘, ‘g‘, ‘o‘, ‘er‘}
vars() #返回对象object的属性和属性值的字典对象 print(vars()) class Foo: a = 1 print(vars(Foo)) #{‘Foo‘: <class ‘__main__.Foo‘>, ‘B‘: <class ‘__main__.B‘>, ‘__name__‘: ‘__main__‘, ‘__doc__‘: None, ‘__spec__‘: None,... #{‘__module__‘: ‘__main__‘, ‘a‘: 1, ‘__dict__‘: <attribute ‘__dict__‘ of ‘Foo‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘Foo‘ objects>, ‘__doc__‘: None}
__import__(name, globals=None, locals=None, fromlist=(), level=0) #动态调用函数
原文:http://www.cnblogs.com/huaxiaosong/p/6251067.html