程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(1)

python学习-31 内置函数

发布于2019-08-07 15:02     阅读(577)     评论(0)     点赞(4)     收藏(1)


内置函数

 

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



所属网站分类: 技术文章 > 博客

作者:我就是喜欢你

链接:https://www.pythonheidong.com/blog/article/11442/464fe78ef2aeefe4efd6/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

4 0
收藏该文
已收藏

评论内容:(最多支持255个字符)