摘要:刷题继续大家好,我又回来了,昨天和大家分享了题,今天继续来看题解法一解法二解法一解法二解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载题
刷题继续
大家好,我又回来了,昨天和大家分享了31-40题,今天继续来看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上,如果大家想看一下每道题的输出结果,可以点击以下链接下载:
Python 41-50题
我的运行环境Python 3.6+,如果你用的是Python 2.7版本,绝大多数不同就体现在以下3点:
raw_input()在Python3中是input()
print需要加括号
fstring可以换成.format(),或者%s,%d
谢谢大家,我们下期见!希望各位朋友不要吝啬,把每道题的更高效的解法写在评论里,我们一起进步!!!
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/43970.html
摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法一解法一解法一源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载 刷题继续 昨天和大家分享了41-50题,今天继续来刷51~60题 Question 51: Write a function to compute 5/0 and use ...
摘要:笨办法学第版结构非常简单,共包括个习题,其中个覆盖了输入输出变量和函数三个主题,另外个覆盖了一些比较高级的话题,如条件判断循环类和对象代码测试及项目的实现等。最后只想说,学习不会辜负任何人,笨办法学 内容简介 《笨办法学Python(第3版)》是一本Python入门书籍,适合对计...
摘要:一套全面的练习,大家智慧的结晶大家好,好久不见,我最近在上发现了一个好东西,是关于夯实基础的道题,原作者是在的时候创建的,闲来无事,非常适合像我一样的小白来练习对于每一道题,解法都不唯一,我在这里仅仅是抛砖引玉,希望可以集合大家的智慧,如果 一套全面的练习,大家智慧的结晶 大家好,好久不见,我最近在Github上发现了一个好东西,是关于夯实Python基础的100道题,原作者是在Pyt...
摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法一解法二解法一解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法二源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载题我的运 刷题继续 昨天和大家分享了71-80题,今天继续来刷81~90题 Question 81: By using list comprehension, p...
马上就要开始啦这次共组织15个组队学习 涵盖了AI领域从理论知识到动手实践的内容 按照下面给出的最完备学习路线分类 难度系数分为低、中、高三档 可以按照需要参加 - 学习路线 - showImg(https://segmentfault.com/img/remote/1460000019082128); showImg(https://segmentfault.com/img/remote/...
阅读 1533·2023-04-26 02:50
阅读 3534·2023-04-26 00:28
阅读 1931·2023-04-25 15:18
阅读 3208·2021-11-24 10:31
阅读 985·2019-08-30 13:00
阅读 999·2019-08-29 15:19
阅读 1765·2019-08-29 13:09
阅读 2974·2019-08-29 13:06