专栏首页python3Python基础练习100题 ( 41

Python基础练习100题 ( 41

刷题继续

大家好,我又回来了,昨天和大家分享了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 3.6+,如果你用的是Python 2.7版本,绝大多数不同就体现在以下3点:

  • raw_input()在Python3中是input()
  • print需要加括号
  • fstring可以换成.format(),或者%s,%d

谢谢大家,我们下期见!希望各位朋友不要吝啬,把每道题的更高效的解法写在评论里,我们一起进步!!!

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 当Excel不够用的时候如何用Python救场?

    Excel是很多公司非常流行的工具,数据分析师和数据科学家经常发现他们把它作为数据分析和可视化工具的一部分,但这并不总是最好的选择。

    HuangWeiAI
  • 如何在Windows上使用Python,看看微软的官方教程

    随着Python持续火热状态,微软开始重视Python在Windows系统上的运行,于是它出品了一个官方教程。这个教程包含了入门,web开发,自动化和脚本,数据...

    HuangWeiAI
  • Vue脚手架搭建项目中的坑

    Vue 框架如同 Python 中的 Django 框架一样,让一切操作变得超级简单,只需在规定的目录下书写规定的代码即可,至于如何运作,框架自己搞定。此篇文章...

    小闫同学啊
  • Python之前准备工作,轻松安装好这些软件就可以开始入门了

    Python作为一个目前最火的编程语言之一,已经渗透到了各行各业,笔者所在的人工智能科研领域就以Python为主要编程语言,尤其是神经网络几乎已经离不开Pyth...

    HuangWeiAI
  • 使用Python『秒开』100GB+数据!

    第二种使用分布式计算:虽然在某些情况下这是一种有效的方法,但是它带来了管理和维护集群的巨大开销。想象一下,必须为一个刚好超出RAM范围的数据集设置一个集群,比如...

    量化投资与机器学习微信公众号
  • Python SDK入门(1)——让NAO说话

    在python for naoqi的环境安装完成后,我们先来尝试让NAO说出“Hello world”:

    李志伟
  • Python进阶之Matplotlib入门(九)

    Matplotlib是Python的画图领域使用最广泛的绘图库,它能让使用者很轻松地将数据图形化以及利用它可以画出许多高质量的图像,是用Python画图的必备技...

    HuangWeiAI
  • PHP 到底是不是宇宙第一?TIOBE 排行榜来证明!

    做为一名程序员,都比较关注其使用编程语言的热度,一方面编程语言的热度决定了它拥有多大的市场,另一方面也关系到行业内程序员选择机会有多大。

    纯洁的微笑
  • [必备工具]Python可视化绘图库Altair

    Altair is a declarative statistical visualization library for Python, based on V...

    zhangqibot
  • 许嵩粉丝地域分布热力图,看看你的爱豆受到哪些地方的人喜欢

    这次我们想绘制一个歌手许嵩的粉丝地域分布的热力图。我们通过对网易云音乐网站爬虫,获取了所有粉丝的信息,包括所在地区。然后将这些地区按照关注人数通过热力图的形式,...

    HuangWeiAI

扫码关注云+社区

领取腾讯云代金券