常用内置函数

常用内置函数

abs()

绝对值
print(abs(2222))
print(abs(-2222))

无负数 将负数变成整数绝对值

all() any()

l = [11, 22, 33, 0]
print(all(l))  # 所有元素都为True结果才是True
print(any(l))  # 所有元素只有一个为True结果就为True
bin( ) oct( ) hex( )

print(bin(222))   # 将222转为二进制数 打印出来
print(oct(222))   # 八进制
print(hex(222))   # 十六进制
bytes( ) str( )

res = '技术的  很牛逼'
res1 = bytes(res, 'utf8')   # 转为二进制的形式 
print(res1)
res2 = str(res1, 'utf8')   # 将二进制形式转为字符串
print(res2)

callable( )

s1 = 'jason'   #  判断是否可以调用(即:能不能加括号运行)
def index():   
    pass
print(callable(s1),callable(index))  # False True

chr( ) ord( )

print(chr(90))   # 根据ASCII码转数字找字符   输出为  z

print(ord('A'))   #  根据ASCII码转字符找数字  输出为 65
complex( )

print(complex(222))   #(222+0j)
转为 复数

dir( )

#查看当前对象可以调用的名字
#def index():
     pass
 print(dir(index))
 print(index.__name__)

divmod( )

divmod(a,b)

 如果参数 a 与 参数 b 都是整数,函数返回的结果相当于 (a // b, a % b)。

"""总数据100 每页10条  10页"""
"""总数据99  每页10条  10页"""
"""总数据101 每页10条  11页"""
 num,more = divmod(233,10)
 if more:
     num += 1
 print('总共需要%s页'%num)

eval( )

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

eg:
res = """
for i in range(10):
    print(i)
"""
res1 = """
print('hello world')
"""
exec(res)
exec(res1)

isinstance( )

 判断是否属于某个数据类型

print(isinstance(122,float))  # False
print(isinstance(2.2,int))  # True


pow( )

  pow(a,b)
  a的b次方

print(pow(4,3))

 输出为64


round( )

 相当与 5舍6入

print(round(4.5))  # 4
print(round(4.6))  # 5


sum( )

 求和

l = [11, 22, 33, 44, 66]
print(sum(l))

 输出为:  176

posted @ 2021-11-22 22:55  AlexEvans  阅读(37)  评论(0编辑  收藏  举报