提示:会有语法格式上的错误
#----------------------------------------#
问题:43
使用给定的元组 (1,2,3,4,5,6,7,8,9,10),编写一个程序,在一行中打印前半个值,在一行中打印后半个值。
提示:
使用 [n1:n2] 表示法从元组中获取切片。
解决方案:
tp=(1,2,3,4,5,6,7,8,9,10)
tp1=tp[:5]
tp2=tp[5:]
print tp1
print tp2
#----------------------------------------#
问题:44
编写一个程序来生成并打印另一个元组,其值为给定元组 (1,2,3,4,5,6,7,8,9,10) 中的偶数。
提示:
使用“for”迭代元组
使用 tuple() 从列表生成元组。
解决方案:
tp=(1,2,3,4,5,6,7,8,9,10)
li=list()
for i in tp:
if tp[i]%2==0:
li.append(tp[i])
tp2=tuple(li)
print tp2
#----------------------------------------#
问题:46
编写一个接受字符串作为输入的程序,如果字符串为“是”或“是”或“是”,则打印“是”,否则打印“否”。
提示:
使用 if 语句判断条件。
解决方案:
s= raw_input()
if s=="yes" or s=="YES" or s=="Yes":
print "Yes"
else:
print "No"
#----------------------------------------#
问题:47
编写一个程序,可以使用过滤函数过滤列表中的偶数。 列表是:[1,2,3,4,5,6,7,8,9,10]。
提示:
使用 filter() 过滤列表中的某些元素。
使用 lambda 定义匿名函数。
解决方案:
li = [1,2,3,4,5,6,7,8,9,10]
evenNumbers = filter(lambda x: x%2==0, li)
print evenNumbers
#----------------------------------------#
问题:48
编写一个程序,可以 map() 生成一个列表,其元素是 [1,2,3,4,5,6,7,8,9,10] 中元素的平方。
提示:
使用 map() 生成列表。
使用 lambda 定义匿名函数。
解决方案:
li = [1,2,3,4,5,6,7,8,9,10]
squaredNumbers = map(lambda x: x**2, li)
print squaredNumbers
问题:49
编写一个程序,可以通过 map() 和 filter() 生成一个列表,该列表的元素为 [1,2,3,4,5,6,7,8,9,10] 中的偶数平方。
提示:
使用 map() 生成列表。
使用 filter() 过滤列表的元素。
使用 lambda 定义匿名函数。
解决方案:
li = [1,2,3,4,5,6,7,8,9,10]
evenNumbers = map(lambda x: x**2, filter(lambda x: x%2==0, li))
print evenNumbers
#----------------------------------------#
问题:50
编写一个程序,可以使用 filter() 来创建一个列表,该列表的元素是 1 到 20 之间的偶数(都包括在内)。
提示:
使用 filter() 过滤列表的元素。
使用 lambda 定义匿名函数。
解决方案:
evenNumbers = filter(lambda x: x%2==0, range(1,21))
print evenNumbers
#----------------------------------------#
问题:51
编写一个可以 map() 生成一个列表的程序,该列表的元素是 1 到 20 之间的数字的平方(都包括在内)。
提示:
使用 map() 生成列表。
使用 lambda 定义匿名函数。
解决方案:
squaredNumbers = map(lambda x: x**2, range(1,21))
print squaredNumbers
#----------------------------------------#
问题:52
定义一个名为 American 的类,它有一个名为 printNationality 的静态方法。
提示:
使用@staticmethod 装饰器定义类静态方法。
解决方案:
class American(object):
@staticmethod
def printNationality():
print "America"
anAmerican = American()
anAmerican.printNationality()
American.printNationality()
#----------------------------------------#
问题:53
定义一个名为 American 的类及其子类 NewYorker。
提示:
使用 class Subclass(ParentClass) 定义子类。
解决方案:
class American(object):
pass
class NewYorker(American):
pass
anAmerican = American()
aNewYorker = NewYorker()
print anAmerican
print aNewYorker
#----------------------------------------#
问题:54
定义一个名为 Circle 的类,它可以由半径构造。 Circle 类有一个可以计算面积的方法。
提示:
使用 def methodName(self) 来定义一个方法。
解决方案:
class Circle(object):
def __init__(self, r):
self.radius = r
def area(self):
return self.radius**2*3.14
aCircle = Circle(2)
print aCircle.area()
#----------------------------------------#
问题:55
定义一个名为 Rectangle 的类,它可以由长度和宽度构造。 Rectangle 类有一个可以计算面积的方法。
提示:
使用 def methodName(self) 来定义一个方法。
解决方案:
class Rectangle(object):
def __init__(self, l, w):
self.length = l
self.width = w
def area(self):
return self.length*self.width
aRectangle = Rectangle(2,10)
print aRectangle.area()
#----------------------------------------#
问题:56
定义一个名为 Shape 的类及其子类 Square。 Square 类有一个 init 函数,它接受一个长度作为参数。 这两个类都有一个 area 函数,它可以打印形状的面积,默认情况下,Shape 的面积为 0。
提示:
要覆盖超类中的方法,我们可以在超类中定义一个同名的方法。
解决方案:
class Shape(object):
def __init__(self):
pass
def area(self):
return 0
class Square(Shape):
def __init__(self, l):
Shape.__init__(self)
self.length = l
def area(self):
return self.length*self.length
aSquare= Square(3)
print aSquare.area()