常见内置函数

内置函数表

 目前重点掌握

  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

  3、bin()、oct()、hex() 进制数

print(bin(123))  # 0b1111011
print(oct(123))  # 0o173
print(hex(123))  # 0x7b

  4、bytes()、str()

复制代码
res = '美美的'
res1 = bytes(res,'utf8')
print(res1)  # b'\xe7\xbe\x8e\xe7\xbe\x8e\xe7\x9a\x84'
res2 = str(res1,'utf8')
print(res2)  # 美美的
res1 = res.encode('utf8')
print(res1)  # b'\xe7\xbe\x8e\xe7\xbe\x8e\xe7\x9a\x84'
res2 = res1.decode('utf8')
print(res2)  # 美美的
复制代码

  5、callable() 是否可调用(能不能加括号运行)

s1 = 'tony'
def index():
    pass
print(callable(s1),callable(index))  # False True

  6、chr()、ord()

print(chr(65))  # 根据ASCII码转数字找字符
print(ord('A'))  # 65

  7、complex() 复数

print(complex(123))  # (123+0j)

  8、dir() 查看当前对象可调用的名字

def index():
    pass
print(dir(index))
print(index.__name__)

  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、enal() 只能识别简单的语法、exec()可以识别复杂语法这两个都是将字符串中的数据内容加载并执行

复制代码
res = """
你好啊
for i in range(10):
    print(i)
"""
res = """
print('hello world')
"""
eval(res)
exec(res)
复制代码

  11、isinstance() 判断是否属于某个数据类型

print(isinstance(123,float))  # False
print(isinstance(123,int))  # True

  12、pow()

print(pow(4,3))  # 64

  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

 

posted @   那就凑个整吧  阅读(52)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示