内置函数简单来说就是python3本身就自带的函数。
abs()返回一个数字的绝对值。如果给出复数,返回值就是该复数的模 print(abs(-1100)) 输出:1100
如果iterable的所有元素不为0、''、False或者iterable为空,all(iterable)返回True,否则返回False; print(all(['a', 'b', 'c', 'd'])) #列表list,元素都不为空或0 注意:空元组、空列表返回值为True,这里要特别注意
如果所有元素中有一个值非0、''或False,那么结果就为True,当iterable所有的值都是0、''或False时,那么结果为False, print(any(['a', 'b', 'c', 'd'])) #列表list,元素都不为空或0 print(any(['a', 'b', '', 'd'])) #列表list,存在一个为空的元素 print(any([0,1,2,3])) #列表list,存在一个为0的元素 print(any(('a', 'b', 'c', 'd'))) #元组tuple,元素都不为空或0 print(any(('a', 'b', '', 'd'))) #元组tuple,存在一个为空的元素 print(any((0,1,2,3))) #元组tuple,存在一个为0的元素 print(any([])) # 空列表 print(any(())) # 空元组
调用对象的__repr__()方法,获得该方法的返回值. print(ascii([1,2,3,1,22,123])) #[1, 2, 3, 1, 22, 123]
三个函数功能为:将十进制数分别转换为2进制。 print(bin(10)) #0b1010
测试一个对象是True还是False. print(bool([])) #False
将一个字符串转换成字节类型 s='apple'
v=bytes(s,encoding='utf-8')
print(v) #b'apple'
callable()函数用于测试对象是否可调用,如果可以则返回1(真);否则返回0(假)。可调用对象包括函数、方法、代码对象、类和已经定义了 调用 方法的类实例。 a = '123' print(callable(a)) #False
chr()函数返回ASCII码对应的字符串。 print(chr(65)) #A
complex()函数可把字符串或数字转换为复数。 print(complex(2,1)) #(2+1j)
删除对象的属性
创建数据字典 print(dict()) #{}
不带参数时返回当前范围内的变量,方法和定义的类型列表,带参数时返回参数的属性,方法列表 print(dir()) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'time']
divmod(x,y)函数完成除法运算,返回商和余数。 print(divmod(10,3)) #(3, 1)
返回一个可以枚举的对象,该对象的next()方法将返回一个元组 s = ['a','b','c']
for i ,v in enumerate(s,1):
print(i,v)
1 a
2 b
3 c
将字符串str当成有效的表达式来求值并返回计算结果 s = '1 + 3 +5' print(eval(s)) #9
执行字符串或complie方法编译过的字符串,没有返回值
float()函数把一个数字或字符串转换成浮点数。 print(float('12')) #12.0
格式化输出字符串 print('i am {0},age{1}'.format('tom',18)) i am tom,age18
创建一个不可修改的集合 参数iterable:可迭代对象。
返回一个描述当前全局变量的字典 a = 'apple' print(globals()) {'__package__': None, '__file__': '/Users/hexin/PycharmProjects/py3/day4/2.py', '__name__': '__main__', 'a': 'apple', 'time': <module 'time' (built-in)>, '__cached__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10bd73c88>, '__builtins__': <module 'builtins' (built-in)>, '__spec__': None, '__doc__': None}
哈希值
两个数值如果相等,则哈希值也相等。
返回对象的帮助文档 调用内建的帮助系统,如果不包含参数,交互式帮助系统将在控制台启动。如果参数为字串,则可以是模块,类,方法等名称,并且帮助页面将会在控制台打印。参数也可以为任意对象
hex()函数可把整数转换成十六进制数。 print(hex(12)) #0xc
返回对象的内存地址 a = 'apple' print(id(a)) #4562197840
获取用户输入内容
int()函数把数字和字符串转换成一个整数,base为可选的基数。
返回一个iterator对象。
print(len('aa')) #2
list()函数可将序列对象转换成列表。 print(list('hello world')) ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
打印当前可用的局部变量的字典
max()函数返回给定参数的最大值,参数可以为序列。 print(max(1,2,3,4)) #4
min()函数返回给定参数的最小值,参数可以为序列。 print(min(1,2,3,4)) #1
返回一个可迭代数据结构(如列表)中的下一项
oct()函数可把给出的整数转换成八进制数。 print(oct(12)) #0o14
ord()函数返回一个字符串参数的ASCII码或Unicode值。 print(ord('a')) #97
打开文件
pow()函数返回以x为底,y为指数的幂。如果给出z值,该函数就计算x的y次幂值被z取模的值。 print(pow(2,5)) #32
print(pow(2,5,3)) #2
range()函数可按参数生成连续的有序整数列表。 print(range(1,10,2)) #range(1, 10, 2)
将任意值转换为字符串,供计时器读取的形式
反转,逆序对象
round()函数返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。 print(round(5.9)) #6
将对象转换成集合
切片功能 s = ['a','b''c','d']
print(slice(1,3,s))
slice(1, 3, ['a', 'bc', 'd'])
排序 列表排序,按数轴方向排,高阶函数,以绝对值大小排序,字符串排序,按照ASCII的大小排序,如果需要排序的是一个元组,则需要使用参数key,也就是关键字。反向排序,reserve=True
str()函数把对象转换成可打印字符串。 print(str(4)) #4
求和
tuple()函数把序列对象转换成tuple。 print(tuple('hello world')) ('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
type()函数可返回对象的数据类型。 print(type('123')) print(type(1)) <class 'str'> <class 'int'>
本函数是实现返回对象object的属性和属性值的字典对象。如果默认不输入参数,就打印当前调用位置的属性和属性值,相当于locals()的功能。如果有参数输入,就只打印这个参数相应的属性和属性值。 print(vars())
#{'__name__': '__main__', '__spec__': None, '__package__': None, '__builtins__': <module 'builtins' (built-in)>, 'time': <module 'time' (built-in)>, '__cached__': None, '__doc__': None, '__file__': '/Users/hexin/PycharmProjects/py3/day4/2.py', '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10e5f3c88>}
print(vars(time))
将对象逐一配对 <zip object at 0x1051d1608>
('h', 1)
('e', 2)
('l', 3)
('l', 4)
('o', 5)
|
|