你們好,我又回來了,昨天和你們分享了31-40題,今天繼續來看41~50題python
Write a program which can map() to make a list whose elements are square of elements in [1,2,3,4,5,6,7,8,9,10].
lst=[i for i in range(1,11)] lst_square = list(map(lambda x:x*x,lst)) print(lst_square)
li = [1,2,3,4,5,6,7,8,9,10] squaredNumbers = map(lambda x: x**2, li) print(list(squaredNumbers))
Write a program which can map() and filter() to make a list whose elements are square of even number in [1,2,3,4,5,6,7,8,9,10].
lst=[i for i in range(1,11)] even_numbers = list(map(lambda x: x**2, filter(lambda x: x%2==0, lst))) print(even_numbers)
def even(x): return x%2==0 def squer(x): return x*x li = [1,2,3,4,5,6,7,8,9,10] li = map(squer,filter(even,li)) print(list(li))
Write a program which can filter() to make a list whose elements are even number between 1 and 20 (both included).
even_numbers = list(filter(lambda x: x%2==0, range(1,21))) print(even_numbers)
def even(x): return x%2==0 evenNumbers = filter(even, range(1,21)) print(list(evenNumbers))
Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included).
def sqr(x): return x*x squaredNumbers = list(map(sqr, range(1,21))) print (squaredNumbers)
squaredNumbers = list(map(lambda x: x**2, range(1,21))) print(squaredNumbers)
Define a class named American which has a static method called 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()
Define a class named American and its subclass NewYorker.
class American(): pass class NewYorker(American): pass american = American() newyorker = NewYorker() print(american) print(newyorker)
Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area.
class Circle: def __init__(self,radius): self.radius = radius def area(self): return (self.radius**2*3.14) # Test circle = Circle(5) print(circle.area())
Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area.
class Rectangle(): def __init__(self,l,w): self.length = l self.width = w def area(self): return self.length*self.width rect = Rectangle(2,4) print(rect.area())
Define a class named Shape and its subclass Square. The Square class has an init function which takes a length as argument. Both classes have a area function which can print the area of the shape where Shape's area is 0 by default.
class Shape(): def __init__(self): pass def area(self): return 0 class Square(Shape): def __init__(self,length = 0): Shape.__init__(self) self.length = length def area(self): return self.length*self.length Asqr = Square(5) print(Asqr.area()) # prints 25 print(Square().area()) # prints à
Please raise a RuntimeError exception.
raise RuntimeError('something wrong')
這十道題的代碼在個人github上,若是你們想看一下每道題的輸出結果,能夠點擊如下連接下載:git
個人運行環境Python 3.6+,若是你用的是Python 2.7版本,絕大多數不一樣就體如今如下3點:github
謝謝你們,咱們下期見!但願各位朋友不要吝嗇,把每道題的更高效的解法寫在評論裏,咱們一塊兒進步!!!this