Python3内置函数实例解析

bin(x)

将一个整数转换成二进制string。
eg:

>>> bin(2)
'0b10'

chr(i)

返回Unicode 码点是整数i的string。ord()与chr()相反。
eg:

>>> chr(100)
'd'
>>> ord('d')
100
>>>

format(value[, format_spec])

将value转化为format_spec指定的格式。

eg:


>>> 'name,age,sex'
'name,age,sex'
>>> '{},{},{}'.format('name','age','sex')
'name,age,sex'
>>> '{2},{1},{0}'.format('name','age','sex')
'sex,age,name'
>>> 
>>> '{2},{1},{1}'.format('name','age','sex')
'sex,age,age'
>>> 
#在Python3.5.1版本中,如果format_spec不是一个空字符串,object().__format__(format_spec) 将抛出一个TypeError异常。

要了解更多format的形式,请参考format的语法

all

all(iterable):如果iterable对象中所有内容都为True或内容为空,则返回True,否则返回False。


>>> m = []
>>> n = [1,2,3]
>>> l = [1,'']
>>> all(m)
True
>>> all(n)
True
>>> all(l)
False
>>>

any(iterable):如果iterable对象中有一个内容True或内容为空,则返回True,否则返回False。

dir

不带参数时,返回上下文中范围内所有的属性。
eg:


>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>> m=[]
>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'm']
>>>

带参数,dir(object)返回的是object的所有属性。
eg:


>>> m=[]
>>> dir(m)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>
    原文作者:Gavinsun
    原文地址: https://blog.csdn.net/gavinsun/article/details/51766553
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞