100+Python挑战性编程练习系列 -- day 12
Question 44
编写一个程序,它可以使用map()来创建一个列表,列表中的元素是1到20之间的数字的平方(包括两者)。
def sqr(x): return x*x squaredNumbers = list(map(sqr, range(1,21))) print(squaredNumbers) Question 45
定义一个名为American的类,它有一个名为printNationality的静态方法。
class American(): @staticmethod def printNationality(): print("I am American") american = American() american.printNationality() # this will not run if @staticmethod does not decorates the function. # Because the class has no instance. American.printNationality() # this will run even though the @staticmethod # does not decorate printNationality() Question 46
定义一个名为American的类及其子类NewYorker。
class American(): pass class NewYorker(American): pass american = American() newyorker = NewYorker() print(american) print(newyorker)