常用内置函数
![image]()
1.abs() 绝对值
print(abs(123))
print(abs(-123))
2.all()判断全部元素的真假 any()判断任意元素的真假
# l = [11,22,33,0]
print(all(l)) # 所有的元素都为True结果才是True
print(any(l)) # 所有的元素只要有一个为True结果就为True
![image]()
3.bin() oct() hex() 进制数
print(bin(123))
print(oct(123))
print(hex(123))
4.bytes()转成字节类型 str()转成字符串
res = '金牌班 最牛逼'
res1 = bytes(res,'utf8')
print(res1)
res2 = str(res1,'utf8')
print(res2)
res1 = res.encode('utf8')
print(res1)
res2 = res1.decode('utf8')
print(res2)
![image]()
5.callable() 是否可调用(能不能加括号运行)
s1 = 'jason'
def index():
pass
print(callable(s1),callable(index)) # False True
6.chr()根据数字找ASCII码对应的字母 ord()根据字母找对应的数
print(chr(65)) # 根据ASCII码转数字找字符
print(ord('A')) # 65
![image]()
7.complex() 复数
print(complex(123)) # (123+0j)
8.dir() 查看当前对象可以调用的名字
def index():
pass
print(dir(index))
print(index.__name__) # 查看当前函数的名字
![image]()
9.divmod(被除数,除数),结果返回商和余数
print(divmod(101,10))
"""总数据100 每页10条 10页"""
"""总数据99 每页10条 10页"""
"""总数据101 每页10条 11页"""
num,more = divmod(233,10)
if more:
num += 1
print('总共需要%s页'%num)
10.eval() exec() 将字符串中的数据内容加载并执行
# eval()只能识别简单的语法 exec()可以识别复杂语法 都是将字符串中的数据内容加载并执行
res = """
你好啊
for i in range(10):
print(i)
"""
res = """
print('hello world')
"""
eval(res)
exec(res)
![image]()
11.isinstance() 判断是否属于某个数据类型
print(isinstance(123,float)) # False
print(isinstance(123,int)) # True
12.pow(数,次方) 计算数字的几次方
print(pow(4,3)) # 64
![image]()
13.round() 可以理解为五舍六入
print(round(4.8)) # 5
print(round(4.6)) # 5
print(round(8.5)) # 8
14.sum() 求和函数
l = [11,22,333,44,55,66]
print(sum(l)) # 531
![image]()
15、常用的匿名函数内置方法
# 结合匿名函数一起使用的函数
1、map()映射函数
map函数语法格式:map(函数名,可迭代对象)
l = [1, 2, 3, 4, ]
name_list = ['jason', 'jenny', 'tom', 'jane', 'kevin']
n = [4, 5, 6, 6, 7]
w = ['NB', 'SB', 'KB', 'LB']
name = 'jason'
res = lambda x: x
print(list(map(res, name))) # ['j', 'a', 's', 'o', 'n']
res1 = lambda x: x ** 2
print(list(map(res1, l))) # [1, 4, 9, 16]
res2 = lambda x, y: x + y
print(list(map(res2, l, n))) # [5, 7, 9, 10]
# 2、zip()拉链函数
# zip函数陆续从每个可迭代对象里取出一个元素,组成元组的形式,最后形成一个新的数据集
生成的新对象的元素个数和最短的对象的长度一致
print(list(zip(name_list, l))) # [('jason', 1), ('jenny', 2), ('tom', 3), ('jane', 4)]
print(list(zip((1, 2, 3, 4), [5, 6, 7, 8]))) # [(1, 5), (2, 6), (3, 7), (4, 8)]
print(list(zip(l,n,w,name_list)))
print(zip(name_list, l)) # <zip object at 0x000002248D94EA08>
# 3、filter()过滤函数
filter函数语法结构
filter('函数名/None','可迭代对象')
name = [10, 23, 34, 56, 78, 45, 65]
res = lambda x: x > 40
print(list(filter(res, name))) # [56, 78, 45, 65]
print(list(filter(None, name))) # [10, 23, 34, 56, 78, 45, 65]
# 4、max()取最大值、min()取最小值函数
# 语法结构
max\min('*args',key=None)
l = [12, 321, 213, 243, 567, 868]
print(max(l)) # 868
l1 = ['jason','alex','BLOG']
print(max(l1)) # jason
'''
对字符串进行比较的时候,根据ASCII码表对应的数字进行比较
'''
dic = {
'jason': 213,
'bony': 80980,
'Alex': 1000,
'kevin': 3247
}
print(max(dic)) # kevin
print(max(dic, key=lambda d: dic[d])) # bony
# 5、reduce()归总
# reduce()语法结构
reduce('函数','可迭代对象','初始值')
from functools import reduce
l = [321, 2131, 4234, 534]
name = '1234'
dic = {
'jason': 213,
'bony': 80980,
'Alex': 1000,
'kevin': 3247
}
print(reduce(lambda x, y: x + y, l)) # 7220
print(reduce(lambda x, y: x + y, l, 100)) # 7320
print(reduce(lambda x, y: x + y, dic)) # jasonbonyAlexkevin
print(reduce(lambda x, y: x + y, name)) # 1234
'''
reduce函数每次从可迭代对象里选择两个元素进行操作,
再把操作后的对象和后面的一个元素进行操作,依次类推
'''
![image]()