0
点赞
收藏
分享

微信扫一扫

Python学习---内置函数的学习

Python学习---内置函数的学习


内置函数

【Py3.5官方文档】https://docs.python.org/3.5/library/functions.html#abs










Built-in Functions









abs()



dict()



​​help()​​



​​min()​​



​​setattr()​​



​​all()​​



​​dir()​​



​​hex()​​



​​next()​​



​​slice()​​



​​any()​​



​​divmod()​​



​​id()​​



​​object()​​



​​sorted()​​



​​ascii()​​



​​enumerate()​​



​​input()​​



​​oct()​​



​​staticmethod()​​



​​bin()​​



​​ev​​al()



​​int()​​



​​open()​​



​​str()​​



​​bool()​​



​​exec()​​



​​isinstance()​​



​​ord()​​



​​sum()​​



​​bytearray()​​



​​filter()​​



​​issubclass()​​



​​pow()​​



​​super()​​



​​bytes()​​



​​float()​​



​​iter()​​



​​print()​​



​​tuple()​​



​​callable()​​



​​for​​mat()



​​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()​​


重要函数

filter(function, sequence): 对sequence中的item依次执行function(item),将执行结果为True的item做成一个filter object的迭代器返回。可以看作是过滤函数。【不更改原来的值,只有一个过滤的效果】

def fun1(s):
if s != 'a':
return s
ret = filter(fun1, str)
print(ret) # ret是一个迭代器对象 <filter object at 0x0000000000A4C518>
print(list(ret)) # ['b', 'c', 'd']

eval:  计算器功能【在引号内使用】和直接返回数据原本的类型

print(eval('1+2+3+4+5+6+7+8+9+10'))    # 55

map(function, sequence) : 对sequence中的item依次执行function(item),将执行结果组成一个map迭代器返回【更改元素值】

str = ['m', 'g', 'f', 'w']
def fun2(s):
return s + "ood"
ret = map(fun2, str)
print(ret) # map object的迭代器
print(list(ret)) # ['mood', 'good', 'food', 'wood']

注意: ​map也支持多个sequence,这就要求function也支持相应数量的参数输入:

def add(x, y):
return x+y
print (list(map(add, range(10), range(10))))##[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

map和filter的区别:

def f(s):
return s + 'ood'
li = ['f', 'm', 'g', 'h']
print(list(map(f, li))) # ['food', 'mood', 'good', 'hood'],拼凑后重新输出
print(list(filter(f, li))) # ['f', 'm', 'g', 'h'], 输出原来的值

reduce(function, sequence, starting_value): 对sequence中的item顺序迭代调用function,如果有starting_value,还可以作为初始值调用.   【直接返回结果,区别map和filter, 阶乘很方便】

from functools import reduce
def fac(x,y):
return x * y
print(reduce(fac, range(1, 5))) # 24
print(reduce(fac, range(1, 5), 10)) # 240
getattr(obj, name, default)函数:从obj对象中取出来某个属性或者方法,没有则打印default值
class Foo:
def __init__(self, name, age):
self.name = name
self.age = age

obj = Foo('ftl', 23)
print(obj.name)
b = 'name'
print('obj.__dict__[b]:',obj.__dict__[b]) # 通过字典取值
print("getattr(obj, 'name'):",getattr(obj, 'name')) # 通过内置函数getattr取出值
setattr(obj, 'school', 'xupt')
print(hasattr(obj, 'school'))
print(delattr(obj, 'school')) # 其他的内置方法,通过字符串的形式操作对象的成员


作者:​​小a玖拾柒​​​ ​

-------------------------------------------

个性签名: 所有的事情到最後都是好的,如果不好,那說明事情還沒有到最後~

本文版权归作者【​​小a玖拾柒​​​】,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利!



举报

相关推荐

0 条评论