python学习——常用的内置函数汇总

类型转换函数

在这里插入图片描述

数学函数

在这里插入图片描述

常用的迭代器操作函数

在这里插入图片描述实操:

from cv2.gapi import descr_of

lst = [55, 42, 37, 2, 66, 23, 18, 99]

# (1) 排序操作
asc_lst = sorted(lst) # 升序
desc_lst = sorted(lst, reverse=True) # 降序
print(asc_lst)
print(desc_lst)

# (2) reversed 倒序
new_lst = reversed(lst)
print(new_lst)
print(list(new_lst))

# (3) zip
x = ['a', 'b', 'c', 'd', 'e']
y = [1, 2, 3, 4, 5]
zipobj = zip(x, y)
print(zipobj)
print(list(zipobj))

# (4) enumerate
enum = enumerate(y, start=1)
print(list(enum))

# (5) all
lst2 = [10, 20, '', 30]
print(all(lst2))
lst2 = [10, 20, '有空即为false', 30]
print(all(lst2))

# (6) any
lst2 = ['', '', '', '']
print(any(lst2))
lst2 = [10, 20, '全空即为false', 30]
print(any(lst2))

# (7) next
zipped = zip(x, y)
print(next(zipped))
print(next(zipped))
print(next(zipped))
print(next(zipped))

# (8) filter
def fun(n):
    return n % 2 == 1 # 奇数为True, 偶数为False
obj =  filter(fun, [1, 2, 3, 4, 5]) # 函数作参数时不用参数
print(list(obj))

# (9) map
def upper(s):
    return s.upper()
new_lst2 = ['hello', 'world']
obj2 = map(upper, new_lst2) # 函数作参数时不用参数
print(list(obj2))

常用的其他内置函数

在这里插入图片描述

# (1) format()
print(format(3.14, '20')) # 数值默认右对齐
print(format('3.14', '20')) # 字符串默认左对齐
print(format('3.14', ' <20')) # 左对齐
print(format('3.14', ' >20')) # 右对齐
print(format('3.14', ' ^20')) # 居中对齐
print(format(3.1415926, '.2f')) # 保留两位小数
print(format(20, 'b')) # 二进制
print(format(20, 'o')) # 八进制
print(format(20, 'x')) # 十六进制

# (2) len...

# (3) id...(查看内存地址)

# (4) type...

# (5) eval (字符串内数学运算)
print(eval('10+30'))
print(eval('10>30'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Qhumaing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值