首页 > 其他 > 详细

内置函数

时间:2017-08-15 19:35:01      阅读:301      评论:0      收藏:0      [点我收藏+]
 

1. all: 所有条件为真则为True,否则是False

1 >>> all([0,3])
2 False
3 >>> all([-1,3])
4 True

 2. any:任一条件为真则为True

1 >>> any([0,3])
2 True
3 >>> any([-1,3])
4 True
5 >>> any([0])
6 False
7 >>> any([0,‘‘])
8 False
9 >>>

 3. bin:将数字转换为二进制

 1 >>> bin(1)
 2 ‘0b1‘
 3 >>> bin(2)
 4 ‘0b10‘
 5 >>> bin(4)
 6 ‘0b100‘
 7 >>> bin(8)
 8 ‘0b1000‘
 9 >>> bin(255)
10 ‘0b11111111

 4. bool:判断真假

1 >>> bool([])
2 False
3 >>> bool([1])
4 True
5 >>> bool(‘abc‘)
6 True
7 >>> bool(‘‘)
8 False

5. ord和chr

1 >>> chr(29579)
2 ‘王‘
3 >>> chr(29239)
4 ‘爷‘
5 >>> ord("王")
6 29579
7 >>> ord("爷")
8 29239

6. compile:将字符串编译成可执行代码

 1 >>> code = "for i in range(10):print(i)"
 2 >>> code
 3 ‘for i in range(10):print(i)‘
 4 >>> compile(code, ‘‘, ‘exec‘)
 5 <code object <module> at 0x0000018C686E3ED0, file "", line 1>
 6 >>> exec(code)
 7 0
 8 1
 9 2
10 3
11 4
12 5
13 6
14 7
15 8
16 9

 7. dir:查询对象有哪些使用方法

1 >>> a = {}
2 >>> dir(a)
3 [‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘clear‘, ‘copy‘, ‘fromkeys‘, ‘get‘, ‘items‘, ‘keys‘, ‘pop‘, ‘popitem‘, ‘setdefault‘, ‘update‘, ‘values‘]

8. divmod:地板除

1 >>> divmod(5,2)
2 (2, 1)
3 >>> divmod(5,1)
4 (5, 0)
5 >>> divmod(5,3)
6 (1, 2)

 9. filter:过滤,示例结合匿名函数

1 >>> res = filter(lambda n:n>5,range(10))
2 >>> for i in res:
3 ...   print(i)
4 ...
5 6
6 7
7 8
8 9

 

内置函数

原文:http://www.cnblogs.com/cnwangshijun/p/7366783.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!