python作业04

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20

1

Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area.

分析:考察构造函数和类方法

1
2
3
4
5
6
7
class Circle(object):
def __init__(self,r):
self.radius=r
def area(self):
return self.radius*self.radius
c=Circle(4)
print(c.area())

image-20231008184906370

2

Define a class named American and its subclass NewYorker.

1
2
3
4
5
6
class American(object):
def __init__(self):
pass
class NewYorker (American):
def __init__(self):
American.__init__(self)

image-20231008185007959

3

Define a class named American which has a static method called printNationality.

1
2
3
4
5
6
7
class American(object):
def __init__(self):
pass
@staticmethod
def printNationality(Nationality):
print(Nationality)

4

Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included).

分析:lambda函数的用法

1
2
s = map(lambda x: x**2, range(1,21))
print (list(s))

image-20231008185146207

5

Write a program which can filter() to make a list whose elements are even number between 1 and 20 (both included).

1
2
l = filter(lambda x: x%2==0,range(1,21))
print(list(l))

image-20231008185242512

6

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].

1
2
3
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 (list(evenNumbers))

image-20231008185329371

7

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].

1
2
3
li = [1,2,3,4,5,6,7,8,9,10]
l =map(lambda x:x**2,li)
print(list(l))

image-20231008185420941

8

Write a program which can filter even numbers in a list by using filter function. The list is: [1,2,3,4,5,6,7,8,9,10].

1
2
3
li = [1,2,3,4,5,6,7,8,9,10]
l = filter(lambda x: x%2==0,li)
print(list(l))

image-20231008190431567

9

Write a program which accepts a string as input to print “Yes” if the string is “yes” or “YES” or “Yes”, otherwise print “No”.

1
2
3
4
5
s =input("Please input:")
if (s=="yes" or s=="YES"or s=="Yes"):
print("Yes")
else:
print("No")

image-20231008190554236

10

Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10).

分析:考察元组

1
2
3
4
5
6
7
t =(1,2,3,4,5,6,7,8,9,10)
l = []
for i in t:
if(i%2==0):
l.append(i)
t1=tuple(l)
print(t1)

image-20231008190905188

11

With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line.

分析:[a : b : c]表示从a到b步长为c取元素

1
2
3
4
5
tp=(1,2,3,4,5,6,7,8,9,10)
tp1=tp[:5]
tp2=tp[5:]
print (tp1)
print (tp2)

image-20231008191313661

12

Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included).

分析:考察元组

1
2
3
4
5
6
7
def printTuple():
li=list()
for i in range(1,21):
li.append(i**2)
print (tuple(li))
printTuple()

image-20231008191703271

13

Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list.

1
2
3
4
5
6
7
def printList():
li=list()
for i in range(1,21):
li.append(i**2)
print (li[:5])
printList()

image-20231008192106761

14

Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list.

分析:考察列表

1
2
3
4
5
6
7
def printList():
li=list()
for i in range(1,21):
li.append(i**2)
print (li[-5:])
printList()

image-20231008192357065

15

Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.

1
2
3
4
5
6
def printList():
li=list()
for i in range(1,21):
li.append(i**2)
print (li[:5])
printList()

image-20231008205658001

16

Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).

1
2
3
4
5
6
7
def printList():
li=list()
for i in range(1,21):
li.append(i**2)
print (li)
printList()

image-20231008205748601

17

Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only.

分析:考察字典

1
2
3
4
5
6
7
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
for k in d.keys():
print (k,end=" ")
printDict()

image-20231008205846615

18

Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the values only.

1
2
3
4
5
6
7
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
for v in d.values():
print (v,end=" ")
printDict()

image-20231008205944478

19

Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys.

1
2
3
4
5
6
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
print (d)
printDict()

image-20231008210023070

20

Define a function which can print a dictionary where the keys are numbers between 1 and 3 (both included) and the values are square of keys.

1
2
3
4
5
6
def printDict():
d=dict()
for i in range(1,4):
d[i]=i**2
print (d)
printDict()

image-20231008210112353


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论