内置函数

'''
abs()	dict()	help()	min()	setattr()
all()	dir()	hex()	next()	slice()
any()	divmod()	id()	object()	sorted()
ascii()	enumerate()	input()	oct()	staticmethod()
bin()	eval()	int()	open()	str()
bool()	exec()	isinstance()	ord()	sum()
bytearray()	filter()	issubclass()	pow()	super()
bytes()	float()	iter()	print()	tuple()
callable()	format()	len()	property()	type()
chr()	frozenset()	list()	range()	vars()
classmethod()	getattr()	locals()	repr()	zip()
compile()	globals()	map()	reversed()	__import__()
complex()	hasattr()	max()	round()	 
delattr()	hash()	memoryview()	set()	 
'''

# 绝对值
print(abs(-199.23), 'abs')

# 传入的序列所有元素都为真(序列里的元素为0,'',None,都是假.字典的话,判断key)返回真,否则假
print(all((1, 2, 3, 4, 5)), 'all')

# 传入的序列里所有的元素都为假,才返回真,否则假
print(any([0, '', None]), any((1, 0, '', None)), 'any')

# 返回一个可打印对象的字符串
a = ascii([1, 2, 3, 4, 5])
print(type(ascii(a)), 'ascii')

# 返回一个可打印对象的字符串
print(repr('123'), 'repr')

# 判断对象的逻辑值
print(bool(1), bool([]), 'bool')

# 将序列,转换成字节数组,字符串要加编码(UTF8一个汉字=三个字节,gkb=两个字节)
print(bytearray([1, 2, 3, 4, 5]), bytearray(
    '张三', encoding='gb2312'), 'bytearray')

# 将序列,转换成字节字符串,字符串要加编码(UTF8一个汉字=三个字节,gkb=两个字节)
print(bytes([1, 2, 3, 4, 5]), bytes(
    '张三', encoding='gb2312'), 'bytes')

# 对象可执行返回真,否则假
fun1 = lambda x: x + 10
print(callable(fun1), callable("1"), 'callable')

# 将ascii码转成字符(小于0x110000 或1114112)
print(chr(25106), 'chr')

# 将单字符转成ascii码
print(ord('我'), 'ord')

# 类方法用
# classmethod()

# 类方法用
# staticmethod()

# 类方法用
# super()

# 类方法用
# isinstance()

# 类方法用
# issubclass()

# 类方法用
# iter()

# 类方法用
# property()

# 类方法用
# slice()

# 编译代码
# compile('print(1)')

# 复数形式
print(complex(3.141592654), 'complex')

# 对象操作
# delattr()

# 对象操作
# getattr()

# 对象操作
# hasattr()

# 对象操作
# setattr()

# 生成字典类
print(dict(k1='1'), 'dict')

# 查看类所有方法,为空则查看当前所有变量名
print(dir(), 'dir')

# 查看类所有方法,为空则查看当前所有变量名和值
print(vars(list), 'vars')

# 取商和余数
print(divmod(10, 3), 'divmod')

# 枚举序列,返回迭代数和序列元素,参数一为序列,参数二为迭代开始值(默认为0)
l1 = {'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': 'd1'}
for i, m in enumerate(l1, 10):
    print(i, m, 'enumerate')

# 运算字符串表达式,并返回结果
print(eval('1+2*5/10'), 'eval')

# 运算字符串表达式,返回None
print(exec('1+2*5/10'), 'exec')


# 循环将序列的每个元素取出来,传递进函数内(函数每次接收一个元素),行进操作后,返回一个新序列,将操作后的每一个元素添加进去
def fun1(x):
    if x > 4:
        x += 5
        x - +100
        x *= 55
    return x


# lambda x: x + 5,也可以使用lambda函数
print(list(map(fun1, [1, 2, 3, 4, 5, 6, 7])), 'map')


# 过滤器,元素传递进函数,根据函数体内返回True,在新序列来添加,False的不添加
def fun2(x):
    if x > 4:
        return True
    else:
        return False


print(list(filter(fun2, [1, 2, 3, 4, 5, 6, 7])), 'filter')

# 将数字或数字字符串转成浮点数
print(float('1.15164'), 'float')

# 格式化字符串
print(format('3.14%d %f %s' % (5, 1.15, '6')))

# 转换到set集合
a = set([1, 2, 3])
print(a, 'set')

# 冻结set集合
b = frozenset(a)
# b.add(6)#无法使用add和discard
print(b, 'frozenset')

# 显示所有全局变量
# print(globals(), 'globals')


# hash值
print(hash('123456'), 'hash')

# 查看帮助文档
# print(help(list), 'help')


# 10转16进制
print(hex(10), 'hex')

# 10转8进制
print(oct(10), 'oct')

# 10转2进制
print(bin(10), 'bin')

# 查看对象内存地址
print(id(fun1), 'id')

# 输入内容
# print(input("请输入内容:"),'input')

# 转换到整数
print(int('1234'), int(1.345), 'int')

# 获取对象长度
print(len('sdfsdfdiruri'), 'len')

# 转换到列表
l1 = (1, 2, 4, 5, 6)
print(list(l1), 'list')

# 显示局部变量
# print(locals(),'locals')


# 取最大值
print(max(1, 2, 3, 4, ), 'max')

# 取最小值
print(min(1, 2, 3, 4, ), 'min')

# 返回一个 memor vies 对象
# print(memoryview())


# 打开文件,返回一个对象
f = open('E:下载Pythoncoding老男孩内置函数.py', 'r')
print(f, 'open')
f.close()

# 幂
print(pow(3, 3), 'pow')

# 打印
print('print')

# 生成指定范围迭代器,参数1=起始位置,参数2=结束位置,参数3=间隔(负数为递减)
for i in range(15, 10, -2):
    print(i, 'range')

# 反转序列
print(list(reversed([1, 2, 3])), 'reversed')

# 四舍五入,参数2=保留的小数位,默认为0
print(round(1.333, 2), 'round')

# 排序,可使用reverse=True 进行反转
print(sorted([1, 5, 6, 2, 3, 4], reverse=True), 'sorted')

# 转换到字符串
print(str(1), 'str')

# 求整形序列的和
print(sum([10, 2, 3, 4, 5, 76]), 'sum')

# 转换到元祖
print(tuple([1, 2, 3]), 'tuple')

# 查看对象类型
print(type('234'), 'type')

# 将多个序列按顺序,合并成一个新序列
a = (1, 2, 3,)
b = ['a', 'b', 'c', ]
c = {'k1': 'k1', 'k2': 'k2', 'k3': 'k3'}
nzip = list(zip(a, b, c))
# [(1, 'a'), (2, 'b'), (3, 'c')]
print(nzip, 'zip')

  

原文地址:https://www.cnblogs.com/xh4528/p/6538642.html