当前位置: 移动技术网 > IT编程>脚本编程>Python > python学习-31 内置函数

python学习-31 内置函数

2019年07月10日  | 移动技术网IT编程  | 我要评论

锦绣良缘之驿寄梅花,鱼饵秘方,残酷猎奇地带

内置函数

 

1.abs()  绝对值

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

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

运行结果:

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("113123123465"))
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()取最小值

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网