内置方法补充

abs

# print(abs(-11.11))  # 求绝对值

all

# 只要有一个为False就返回False

any

# 只要有一个位True就返回True

globals    locals

def index():
    username = '我是局部名称空间里面的username'
    # print(locals())  # 当前语句在哪个位置 就会返回哪个位置所存储的所有的名字
    print(globals())  # 无论在哪 查看的都是全局名称空间

bin  oct  hex

# print(bin(10))  # 0b1010
# print(oct(10))  # 0o12
# print(hex(10))  # 0xa

int

# print(int('0b1010',2))  # 10

bool

# print(bool(1))  True
# print(bool(0))  False

encode    bytes

s = 'hello'
print(s.encode('utf-8'))  # 用utf-8方式编码
print(bytes(s,encoding='utf-8'))  # 把s字符串通过utf-8方式转化为字节

callable

# 可调用的(可以加括号执行相应功能的)
l = [1,2,3]
def index():
    pass
print(callable(l))
print(callable(index))

dir

# l = [1,2,3]
# print(dir(l))

divmod

# print(divmod(101,10))
# total_num,more = divmod(900,11)
# if more:
#     total_num += 1
# print('总页数:',total_num)

enumerate

# enumerate 枚举
# l = ['a','b']
# for i,j in enumerate(l,1):
#     print(i,j)

eval    exex

# eval不支持逻辑代码,只支持一些简单的python代码
# eval(s)
# exec可以把字符串里的字符转换为可执行代码,可以支持多行字符。但是拿不到返回结果。
# exec(s)
s1 = """
print(1 + 2)
for i in range(10):
    print(i)
"""
# eval(s1)
# exec(s1)

format

# format 三种玩法
# {}占位
# {index} 索引
# {name} 指名道姓

isinstance

# isinstance 后面统一改方法判断对象是否属于某个数据类型

type

# print(type(n))

pow

# print(pow(2,3))

round

# print(round(3.4))  四舍五入
原文地址:https://www.cnblogs.com/jinpan/p/11191918.html