老男孩python学习自修第十天【内置函数】

1.基本内置函数

help()  帮助文档

dir()  列出当前文件的所有变量和方法

vars()  列出当前文件的所有变量及其值

type()  返回变量的类型

id()  返回变量的内存地址

len()  返回变量的长度

from package import module  导入模块

reload(package.module)  重新加载模块

2.基本运算内置函数

bool()  转化为bool值

abs()  获取绝对值

divmod()  返回商和余数的元组

max()  返回最大值

min()  返回最小值

sum()  返回和

pow()  返回指数

3.列表判断内置函数

all()  传入列表,如果列表中所有的值都为True则返回True,否则返回False

any()  传入列表,如果列表中任何值为True则返回True,否则返回False

4.进制与ASCII码

chr()  返回ASCII对应的字符

ord()  返回字符对应的ASCII码

hex()  转化为16进制

oct()  转化为8进制

bin()  转化为2进制

5.迭代生成序列号与占位符

li = ['汽车', '房子', '手表']
for item in li:
    print li

for item in enumerare(li, 1):
    print item

占位符:

s = "I am {0}"
print s.format("Alex")

6.map,filter,reduce

map的例子:

def foo(arg):
    return arg + 100

li = [11, 22, 33]
temp = []
for item in li:
    temp.append(foo(item))
print temp
temp = map(foo, li)
print temp
temp = map(lambda arg: arg+100, li)
print temp

filter与reduce

print filter(lambda x: x == 1, [1, 23, 4])    True序列
print reduce(lambda x,y: x+y, [1, 2, 3])    累加

猜你喜欢

转载自www.cnblogs.com/liuzhiqaingxyz/p/9321359.html