Python(16)-Python内置函数

 

Python(16)-Python内置函数

 

python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:https://docs.python.org/3/library/functions.html

  Built-in Functions  
abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
bin() eval() int() open() str()
bool() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()  
delattr() hash() memoryview() set()  

 

1. abs()  获取绝对值

abs(-10)
--->10
 
abs(10)
--->10
 
abs(0)
--->0
 
a = -10
a.__abs__()
--->10

  

2. all()  参数为可迭代对象,迭代对象为空时,返回True.如果迭代对象的所有元素都为真,那么返回True,否则返回False.

all(['python',123])
--->True
 
all([])
--->True
 
all([0])
--->False
 
all(" ")
--->True
 
all(1,' ',2,None)
--->False

  

 3.any()  参数为可迭代对象,参数为空时返回True

print(any([None,0,' ',{},1]))
--->True
 
print(any(' '))
--->True

  

4.sum()  求和

res=sum(i for i in range(5))  #(i for i in range(5))为生成器表达式
print(res)
--->12

 

 
 
 
 
posted @ 2018-07-08 16:27  yimi+fly  阅读(128)  评论(0编辑  收藏  举报