国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Python基礎練習100題 ( 41~ 50)

mochixuan / 1893人閱讀

摘要:刷題繼續(xù)大家好,我又回來了,昨天和大家分享了題,今天繼續(xù)來看題解法一解法二解法一解法二解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載題

刷題繼續(xù)

大家好,我又回來了,昨天和大家分享了31-40題,今天繼續(xù)來看41~50題

Question 41:
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))
Question 42:
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))
Question 43:
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))
Question 44:
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)
Question 45:
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()                            
Question 46:
Define a class named American and its subclass NewYorker.

解法一
class American():
    pass

class NewYorker(American):
    pass

american = American()
newyorker = NewYorker()

print(american)
print(newyorker)
Question 47:
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())
Question 48:
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())
Question 49:
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 à
Question 50:
Please raise a RuntimeError exception.

解法一
raise RuntimeError("something wrong")
源代碼下載

這十道題的代碼在我的github上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載:

Python 41-50題

我的運行環(huán)境Python 3.6+,如果你用的是Python 2.7版本,絕大多數(shù)不同就體現(xiàn)在以下3點:

raw_input()在Python3中是input()

print需要加括號

fstring可以換成.format(),或者%s,%d

謝謝大家,我們下期見!希望各位朋友不要吝嗇,把每道題的更高效的解法寫在評論里,我們一起進步!!!

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/43970.html

相關文章

  • Python基礎練習100 ( 51~ 60)

    摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載 刷題繼續(xù) 昨天和大家分享了41-50題,今天繼續(xù)來刷51~60題 Question 51: Write a function to compute 5/0 and use ...

    岳光 評論0 收藏0
  • 測試開發(fā)必看:《笨辦法學Python3》PDF中文高清版,豆瓣高分8.0

    摘要:笨辦法學第版結(jié)構(gòu)非常簡單,共包括個習題,其中個覆蓋了輸入輸出變量和函數(shù)三個主題,另外個覆蓋了一些比較高級的話題,如條件判斷循環(huán)類和對象代碼測試及項目的實現(xiàn)等。最后只想說,學習不會辜負任何人,笨辦法學 內(nèi)容簡介   《笨辦法學Python(第3版)》是一本Python入門書籍,適合對計...

    不知名網(wǎng)友 評論0 收藏0
  • Python基礎練習100 ( 1~ 10)

    摘要:一套全面的練習,大家智慧的結(jié)晶大家好,好久不見,我最近在上發(fā)現(xiàn)了一個好東西,是關于夯實基礎的道題,原作者是在的時候創(chuàng)建的,閑來無事,非常適合像我一樣的小白來練習對于每一道題,解法都不唯一,我在這里僅僅是拋磚引玉,希望可以集合大家的智慧,如果 一套全面的練習,大家智慧的結(jié)晶 大家好,好久不見,我最近在Github上發(fā)現(xiàn)了一個好東西,是關于夯實Python基礎的100道題,原作者是在Pyt...

    Java3y 評論0 收藏0
  • Python基礎練習100 ( 81~ 90)

    摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法一解法二解法一解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載題我的運 刷題繼續(xù) 昨天和大家分享了71-80題,今天繼續(xù)來刷81~90題 Question 81: By using list comprehension, p...

    劉德剛 評論0 收藏0
  • 第7期 Datawhale 組隊學習計劃

    馬上就要開始啦這次共組織15個組隊學習 涵蓋了AI領域從理論知識到動手實踐的內(nèi)容 按照下面給出的最完備學習路線分類 難度系數(shù)分為低、中、高三檔 可以按照需要參加 - 學習路線 - showImg(https://segmentfault.com/img/remote/1460000019082128); showImg(https://segmentfault.com/img/remote/...

    dinfer 評論0 收藏0

發(fā)表評論

0條評論

mochixuan

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<