Python内置函数

abs

参数  整数或复数
返回一个数的绝对值。实参可以是整数或浮点数。如果实参是一个复数,返回它的模。
a = -1
print(abs(a)) # 1
a = 0.23-8.33j
print(abs(a)) # 8.33317466515613

 

all

参数  可迭代对象  list dict 
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
如果 iterable 的所有元素为真(或迭代器为空),返回 True 。


  all([]) # True
  all({}) # True
  all(()) # True
list,tuple,set
  all([1,2,3,4]) # True
  all([0,None,False,'',(),{},[]] # False
dict
  all({'das':0,'asd':None,'adsa':False,'asda':'','adsasdad':{}}) # True
  all({'':'dasd',False:'dsad',None:'asd',0:'dasd'}) # False

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

# 循环一个空的迭代其对象不会报错

any

Return True if bool(x) is True for any x in the iterable.   
If the iterable is empty, return False.

如果 iterable 的任一元素为真则返回 True。如果迭代器为空,返回 False。

  all([]) # False
  all({}) # False
  all(()) # False
list,tuple,set
  all([1,2,3,4]) # True
  all([0,None,False,'',(),{},[]]) # False
dict
  all({'das':0,'asd':None,'adsa':False,'asda':'','adsasdad':{}}) # True
  all({'':'dasd',False:'dsad',None:'asd',0:'dasd'}) # False


def any(iterable):
    for element in iterable:
        if element:
            return False
    return True
 

 

ascii

Return an ASCII-only representation of an object.
As repr(), return a string containing a printable representation of an object, but escape the non-ASCII characters in the string 
returned by repr()
using \\x, \\u or \\U escapes. This generates a string similar to that returned by repr() in Python 2.

就像函数 repr(),返回一个对象可打印的字符串,但是 repr() 返回的字符串中非 ASCII 编码的字符,会使用 \x\u 和 \U 来转义。生成的字符串和 Python 2 的 repr() 返回的结果相似。

ASCII编码
  ascii('-+dasds5646514') # '-+dasds5646514'
非ASCII编码
  ascii('阿发发') # '\u963f\u53d1\u53d1'

 

bin

Return the binary representation of an integer.
       >>> bin(2796202)
       '0b1010101010101010101010'

将一个整数转变为一个前缀为“0b”的二进制字符串。结果是一个合法的 Python 表达式。如果 x 不是 Python 的 int 对象,那它需要定义 __index__() 方法返回一个整数。
bin(123)  # 0b1111011
bin(-10) # -0b1010

 

bool

Returns True when the argument x is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
he class bool is a subclass of the class int, and cannot be subclassed.

返回一个布尔值,True 或者 False。 x 使用标准的 真值测试过程 来转换。如果 x 是假的或者被省略,返回 False;其他情况返回 True
bool 类是 int 的子类(参见 数字类型 --- int, float, complex)。其他类不能继承自它。它只有 False 和 True 两个实例(参见 布尔值)。

bool('') bool() bool(False) bool([]) bool(()) bool({}) # False

 

未完待续。。。。。

posted @ 2019-06-02 23:05  yyfgrd  阅读(196)  评论(0)    收藏  举报