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

資訊專欄INFORMATION COLUMN

Python基礎(chǔ)練習(xí)100題 ( 41~ 50)

mochixuan / 1884人閱讀

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

刷題繼續(xù)

大家好,我又回來(lái)了,昨天和大家分享了31-40題,今天繼續(xù)來(lái)看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é)果,可以點(diǎn)擊以下鏈接下載:

Python 41-50題

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

raw_input()在Python3中是input()

print需要加括號(hào)

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

謝謝大家,我們下期見(jiàn)!希望各位朋友不要吝嗇,把每道題的更高效的解法寫在評(píng)論里,我們一起進(jìn)步!!!

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

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

相關(guān)文章

  • Python基礎(chǔ)練習(xí)100 ( 51~ 60)

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

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

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

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

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

    Java3y 評(píng)論0 收藏0
  • Python基礎(chǔ)練習(xí)100 ( 81~ 90)

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

    劉德剛 評(píng)論0 收藏0
  • 第7期 Datawhale 組隊(duì)學(xué)習(xí)計(jì)劃

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

    dinfer 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

mochixuan

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<