Python入门到精通

Python基础006--标识符、关键字、內建函数

2018-02-25  本文已影响1人  不一样的丶我们

了解并使用常用python标识符、关键字、內建函数

# apply reduce
def sum(a,b):
    return a+b
print reduce(sum,range(0,10))
print apply(sum,(1,2))

# map
def pow(x,y):
    return x**y
print map(pow, range(1,5), range(5,1,-1))

# issubclass
class A():
    pass
class B(A):
    pass
print issubclass(B,A)--->True

# format
In [45]: "{1},{0}.{1}".format("hello","world")
Out[45]: 'world,hello.world'

In [46]: print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com"))
Out[46]: 网站名:菜鸟教程, 地址 www.runoob.com

# getattr setattr
In [49]: class A(object):
    ...:     bar = 1
In [50]: a = A()
In [51]: getattr(a,"bar")
Out[51]: 1
In [52]: setattr(a,"bar",5)
In [53]: a.bar
Out[53]: 5

上一篇 下一篇

猜你喜欢

热点阅读