magic book

发布时间 2023-12-31 23:38:34作者: T7H
magic book.md

填空

class Dim: def __init__(self,x,y): self.x = x self.y = y def area(self): pass class Circle(Dim): def __init__(self, r): Dim.__init__(self, 0, r) def area(self): return 3.14 * self.y * self.y class Rectangle(Dim): def __init__(self, w, h): Dim.__init__(self, w, h) def area(self): return self.x * self.y c1 = Circle(4.0) r1 = Rectangle(3.0,5.0) print('圆面积',c1.area()) print('矩形面积',r1.area())
def FindWanshu(): ws = 0 lst = "" for num in range(1,2001): s = num for i in range(1,num): if num%i == 0: s = s-i if s == 0: lst = lst + str(num) + ',' ws = ws+1 return lst,ws f = open("wanshu.txt","w") lstw,lws=FindWanshu()

f.write(lstw)
f.close()
print("1~2000之间的完数共有%d个" % lws)
print("wanshu:"+lstw)

import math def FindSushu(lst): for num in range(1,1000): k = int(math.sqrt(num)) for i in range(2,k+2): if num%i == 0: break if i == k+1: lst.append(num) f = open("sushu.txt","w") lst = [] FindSushu(lst) txt = "" for item in lst: txt += str(item) txt += "," f.write(txt) f.close() print("1~1000之间的素数共有%d个" % len(lst))
class Per: def __init__(self,name,age): self.name=name self.age=age def say_hi(self): print("您好!我叫{0},{1}岁",format(self.name,self.age)) class Stud(Per): def __init__(self,name,age,stu_id): Per.__init__(self,name,age) self.stu_id=stu_id def say_hi(self): Per.say_hi(self) print('我是学生,我的学号为:',self.stu_id) per1 = Per("张三",43) per1.say_hi() stu1 = Stud("李四",20,20212345678) stu1.say_hi()

编程题

def max_n(a,b,*c): max_value = a if max_value < b: max_value = b for n in c: if max_value < n: max_value = n return max_value #测试代码 print("最大值为:",max_n(6,2)) print("最大值为",max_n(16,5,7,4,20))
import math class MyMath: def __init__(self,r): self.r = r global pi pi = 3.14 def Perimeter(self): return (2 * pi * self.r) def Area(self): return (pi * self.r * self.r) def Surface(self): return (4 * pi * self.r * self.r) def Volume(self): return (4 * pi * pow(self.r, 3) / 3) r = float(input("请输入半径:")) m = MyMath(r) print(str.format("圆的周长 = {0:2.2f}",m.Perimeter())) print(str.format("圆的面积 = {0:2.2f}",m.Area())) print(str.format("球的表面积 = {0:2.2f}",m.Surface())) print(str.format("球的体积 = {0:2.2f}",m.Volume()))
def getMaxMin(s): maxi = s[0] mini = s[0] count = 0 for i in s: if maxi < i: maxi = i if mini > i: mini = i count += 1 return maxi, mini, count #测试代码 list1 = [19,20,1,2,3] x1,y1,z1 = getMaxMin(list1) print("list=",list1) print("最大值=",x1,"最小值",y1,",元素个数",z1) list2 = list2 = ["marry","lili","ben","alicei"] x2,y2,z2 = getMaxMin(list2) print("list=",list2) print("最大值=",x2,"最小值",y2,",元素个数",z2)
class Temperature: def __init__(self, degree, degree_type): self.degree = degree self.degree_type = degree_type def ToFahrenheit(self): if self.degree_type == 'C': self.degree_type = 'F' self.degree = (self.degree * 1.8) + 32 return self.degree else: return self.degree def ToCelsius(self): if self.degree_type == 'F': self.degree_type = 'C' self.degree = (self.degree - 32) /1.8 return self.degree else: return self.degree #测试代码 d1 = Temperature(float(input("请输入摄氏温度:")),'C') print("华氏温度 = {0:2.2f},摄氏温度 = {1:2.2f}".format(d1.ToFahrenheit(),d1.ToCelsius())) d2 = Temperature(float(input("请输入华氏温度:")),'F') print("华氏温度 = {0:2.2f},摄氏温度 = {1:2.2f}".format(d2.ToFahrenheit(),d2.ToCelsius()))

必考

import numpy as np import matplotlib.pyplot as plt def f(t): return np.exp(-0.5*t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure('曲线') plt.subplot(212) plt.plot(t1, f(t1), 'ko', t2, f(t2), 'b') plt.subplot(211) plt.plot(t2,np.sin(2*np.pi*t2),'r',t1,np.cos(2*np.pi*t1+np.pi),'g-.o') plt.show()