内置函数

 

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

 10. frozenset:不可变集合

1 >>> a = set([1,3,4,55,6,7,3,55,3,2,7])
2 >>> a
3 {1, 2, 3, 4, 6, 7, 55}
4 >>> a = frozenset([1,3,4,55,6,7,3,55,3,2,7])
5 >>> a
6 frozenset({1, 2, 3, 4, 6, 7, 55})
7 >>> dir(a)
8 ['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'copy', 'difference', 'intersection', 'isdisjoint', 'issubset', 'issuperset', 'symmetric_difference', 'union']
9 >>>

11. globals:显示当前文件所有全局的变量,对应的有locals,只显示局部的变量

1 >>> print(globals())
2 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': frozenset({1, 2, 3, 4, 6, 7, 55}), 'code': 'for i in range(10):print(i)', 'i': <function <listcomp>.<lambda> at 0x0000018C687A3158>, 'calc': <function <lambda> at 0x0000018C6877CAE8>, 'res': [<function <listcomp>.<lambda> at 0x0000018C6877CC80>, <function <listcomp>.<lambda> at 0x0000018C6877CD08>, <function <listcomp>.<lambda> at 0x0000018C6877CD90>, <function <listcomp>.<lambda> at 0x0000018C6877CE18>, <function <listcomp>.<lambda> at 0x0000018C6877CEA0>, <function <listcomp>.<lambda> at 0x0000018C6877CF28>, <function <listcomp>.<lambda> at 0x0000018C6877CB70>, <function <listcomp>.<lambda> at 0x0000018C687A3048>, <function <listcomp>.<lambda> at 0x0000018C687A30D0>, <function <listcomp>.<lambda> at 0x0000018C687A3158>], 'functools': <module 'functools' from 'D:\\Programs\\Python\\Python36\\lib\\functools.py'>, 'res2': 362880}

 12. hash:将字符串对象转换称hash值

>>> hash("shenzhen")
5509959518264855463
>>> hash("shanghai")
-888420846842475357
>>> hash("changsha")
-1606297724449516823
>>> hash("guangzhou")
854541565800798490

13. hex:将数字转换为16进制

>>> hex(15)
'0xf'
>>> hex(9)
'0x9'
>>> hex(255)
'0xff'
>>> hex(200)
'0xc8'
>>>

14. max和min:返回最大或最小值

>>> max(2,5,6,7,9)
9
>>> min(2,5,6,7,9)
2

15. pow,x的y次方

>>> pow(3,5)
243
>>> pow(2,8)
256

16. round

>>> round(1.3352324)
1
>>> round(1.3352324,2)
1.34
>>> round(1.3352324,3)
1.335

17. sorted:对字典进行排序,排序完之后结果为一列表

>>> a = {6:2,8:0,1:4,-6:55,88:12,7:15}
>>> a
{1: 4, 6: 2, 7: 15, 8: 0, 88: 12, -6: 55}
>>> sorted(a)
[-6, 1, 6, 7, 8, 88]
>>> sorted(a.items())
[(-6, 55), (1, 4), (6, 2), (7, 15), (8, 0), (88, 12)]
>>>
>>> sorted(a.items(), key=lambda x:x[1])
[(8, 0), (6, 2), (1, 4), (88, 12), (7, 15), (-6, 55)]

18. zip:列表拼接,如果数据不对等,则以最少的为准

>>> a = [1,2,3,4]
>>> b = ["a",'b','c','d']
>>> zip(a,b)
<zip object at 0x0000026D0BDB97C8>
>>>
>>> for i in zip(a,b):
...   print(i)
...
(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')

  

  

posted @ 2017-08-15 18:28  炉山假面目  阅读(113)  评论(0编辑  收藏  举报