首页 > 编程语言 > 详细

Python内置函数

时间:2019-06-02 23:31:16      阅读:114      评论:0      收藏:0      [点我收藏+]

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
 

 

未完待续。。。。。

Python内置函数

原文:https://www.cnblogs.com/yuyafeng/p/10964993.html

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