内置方法
abs() 取绝对值
>>> abs(-4)
all()
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
总结:如果iterable的所有元素不为0、''、False或者iterable为空,all(iterable)返回True,否则返回False。
注意:空元组、空列表返回值为True。
>>>all(['a', 'b', 'c', 'd']) # 列表list,元素都不为空或0
True
>>> all(['a', 'b', '', 'd']) # 列表list,存在一个为空的元素
False
>>> all([0, 1,2, 3]) # 列表list,存在一个为0的元素
False
>>> all(('a', 'b', 'c', 'd')) # 元组tuple,元素都不为空或0
True
>>> all(('a', 'b', '', 'd')) # 元组tuple,存在一个为空的元素
False
>>> all((0, 1,2, 3)) # 元组tuple,存在一个为0的元素
False
>>> all([]) # 空列表
True
>>> all(()) # 空元组
True
any()
Return True if bool(x) is True for any x in the iterable.
If the iterable is empty, return False.
总结:如果都为空、0、false,则返回false,如果不都为空、0、false,则返回true。
>>>any(['a', 'b', 'c', 'd']) # 列表list,元素都不为空或0
True
>>> any(['a', 'b', '', 'd']) # 列表list,存在一个为空的元素
True
>>> any([0, '', False]) # 列表list,元素全为0,'',false
False
>>> any(('a', 'b', 'c', 'd')) # 元组tuple,元素都不为空或0
True
>>> any(('a', 'b', '', 'd')) # 元组tuple,存在一个为空的元素
True
>>> any((0, '', False)) # 元组tuple,元素全为0,'',false
False
>>> any([]) # 空列表
False
>>> any(()) # 空元组
dict() 把一个数据转成字典,或生成一个空字典
dict语法:
class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
参数说明
**kwargs -- 关键字
mapping -- 元素的容器。
iterable -- 可迭代对象。
返回一个字典,代码示例如下
>>>dict() # 创建空字典
{}
>>> dict(a='a', b='b', t='t') # 传入关键字
{'a': 'a', 'b': 'b', 't': 't'}
>>> dict(zip(['one', 'two', 'three'], [1, 2, 3])) # 映射函数方式来构造字典
{'three': 3, 'two': 2, 'one': 1}
>>> dict([('one', 1), ('two', 2), ('three', 3)]) # 可迭代对象方式来构造字典
{'three': 3, 'two': 2, 'one': 1}
>>>
max&min
从列表中取最大值或者最小值
>>> a = [1,4,8,-4,0]
>>> min(a)
-4
>>> max(a)
8
bool判断真假
非零即真
>>> bool(0)
False
>>> bool(False)
False
>>>
dir
打印当前程序的所有变量
help
帮助
divmod
>>> divmod(10, 3)
(3, 1)
sorted
>>> d
{0: 399, 1: -49, 2: -48, 3: -47, 4: -46}
>>> sorted(d.items(), key=lambda x:x[1])
[(1, -49), (2, -48), (3, -47), (4, -46), (0, 399)]
>>>
**chr & ord **
>>> ord("A")
65
>>> chr(65)
'A'
map & filter
>>> def f(x):
... return x * x
...
>>> r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> list(r)
[1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> list(map(lambda x: x*x, [1,2,3,4,5]))
[1, 4, 9, 16, 25]
>>> list(filter(lambda x: x>3, [1,2,3,4,5]))
[4, 5]
>>>
reduce()
import functools
l = [1, 2, 3, 4, 5]
res = functools.reduce(lambda x, y: x+y, l)
res_x = functools.reduce(lambda x, y: x*y, l, 10)
res_p = functools.reduce(lambda x, y: x+y, l, 10)
print(res, res_x, res_p)
pow()
>>> pow(2,3) # 2**3
8
>>> pow(2,3,5) # 2**3%5
3
>>>
print()
print()默认结尾为" "
>>> print(s)
life is sort, i use python
>>> print(s, end="+++")
life is sort, i use python+++>>>
print()直接打印到文件
sep : 分隔符
msg = "又回到最初的起点"
f = open("print_tofile","w")
print(msg,"记忆中你青涩的脸",sep="|",end="",file=f) # 直接打印到文件
print(msg,"记忆中你青涩的脸",sep="|",end="",file=f)
callable
是否能被调用
>>> def f():
... passd
...
>>> callable(f)
True
>>> a = 666
>>> callable(a)
False
>>>
locals()
打印函数的局部变量
gobals()
打印全局变量
zip()
>>> a = [1,2,3,4,5]
>>> b = ["a", "b", "c"]
>>> list(zip(a,b))
[(1, 'a'), (2, 'b'), (3, 'c')]
>>>
complex
复数
>>> complex(1,2)
(1+2j)
round
保留小数
>>> round(1.234554, 3)
1.235
>>>
execc&eval
exec可以将多行字符串转化成代码,但是没有返回值。
eval只能将一行字符串转化成代码,但是有返回值。
n = "1+2/5"
res = eval(n)
res2 = exec(n)
print(res, res2) # 1.4 None
code = """
for i in range(3):
print(i)
"""
exec(code)
eval(code) # SyntaxError: invalid syntax