python学习-31 内置函数

时间:2023-03-09 14:39:45
python学习-31 内置函数

内置函数

1.abs()  绝对值

2.all()    判断列表里的所有值的布尔值(如果迭代列表里的每个值后都是True 则返回True)

print(all([1,2,'']))

运行结果:

True

Process finished with exit code 0

3.any() 与all相反 ,如果有一个为真 都为真

4.bin() 十进制转换为二进制

5.bool() 判断布尔值  。为False的布尔值(空,None,0)

6.bytes()  将字符串转换成字节

name = '小明'
print(bytes(name,encoding='utf-8'))

运行结果:

b'\xe5\xb0\x8f\xe6\x98\x8e'           # 6个字节

Process finished with exit code 0

7.decode 解码   (ASCII 不能编码中文)

name = '小明'
print(bytes(name,encoding='utf-8').decode('utf-8'))
print(bytes(name,encoding='gbk').decode('gbk'))

运行结果:

小明
小明 Process finished with exit code 0

8.chr()   按照ASCII表 打印

9.dir()  目录

print(dir(all))

运行结果:

['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__text_signature__']

Process finished with exit code 0

10.divmod()   取商和余

print(divmod(4,2))    # 可以用来做分页功能,例如:一共4页,每页分了2行字,一共分了2页。

运行结果:

(2, 0)

Process finished with exit code 0

11.eval() 提取字符串中的数据结构,还可以把字符串中的表达式进行运算

12.hash()   可hash的数据类型,为不可变数据类型

print(hash(""))
print(hash("456asdfa4561sdf1a456sad4f"))

运行结果:

714133531
-1932136269 Process finished with exit code 0

13.help() 查看帮助

14.hex()  十进制转十六进制

15.oct()  十进制转八进制

16.isinstance()   判断类型

print(isinstance(1,int))

运行结果:

True

Process finished with exit code 0

17.locals()  打印当前路径  (局部)

18.globals()  打印路径(当前文件的路径)

19.max() 取最大值

20.min()取最小值