常见内置函数简介


常见内置函数

  • 1、abs() 绝对值
    print(abs(123))
    print(abs(-123))
    二者的输出结果一致

  • 2、all() any()
    print(all(l)) # 所有的元素都为True结果才是True
    print(any(l)) # 所有的元素只要有一个为True结果就为True

  • 3、bin() oct() hex() 转换进制数

  • 4、bytes() str()
    res = '金丹强者牛逼'
    res1 = bytes(res,'utf8')
    print(res1)
    res2 = str(res1,'utf8')
    print(res2)

  • 5、callable() 是否可调用(能不能加括号运行)
    s1 = '金丹强者'
    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()

  • 10、eval()和exec()
    eval()只能识别简单的语法
    exec()可以识别复杂语法 都是将字符串中的数据内容加载并执行

  • 11.isinstance() 判断是否属于某个数据类型
    print(isinstance(123,float)) # False
    print(isinstance(123,int)) # True

  • 12.pow()

  • 13.round()
    round() 方法返回浮点数x的四舍五入值
    print(round(4.8))

  • 14.sum()
    sum() 方法对序列进行求和计算

py