#1
1 #1 2 class Account: 3 def __init__(self,name,account_number,initial_amount=10): 4 self._name=name 5 self._card_no=account_number 6 self._balance=initial_amount 7 8 def deposit(self,amount): 9 '''存款''' 10 self._balance+=amount 11 12 def withdraw(self,amount): 13 '''取款''' 14 if self._balance<amount: 15 print('余额不足') 16 return 17 self._balance-=amount 18 19 def info(self): 20 '''打印账户信息''' 21 print('持卡人姓名:',self._name) 22 print('持卡人账号:',self._card_no) 23 print('持卡人账户余额:',self._balance) 24 25 def get_balance(self): 26 '''返回账户余额''' 27 return self._balance 28 29 def main(): 30 print('测试账户1:'.center(30,'*')) 31 a1=Account('Bob','5002311',20000) 32 a1.deposit(5000) 33 a1.withdraw(4000) 34 a1.info() 35 36 print() 37 38 print('测试账户2:'.center(30,'*')) 39 a1=Account('Joe','5006692',20000) 40 a1.deposit(10000) 41 a1.withdraw(5000) 42 a1.info() 43 44 if __name__=='__main__': 45 main()View Code
#2
1 #2 2 class Shape: 3 def info(self): 4 pass 5 6 def area(self): 7 pass 8 9 def perimeter(self): 10 pass 11 12 class Rect(Shape): 13 def __init__(self,x=0,y=0,length=2,width=1): 14 self._x=x 15 self._y=y 16 self._width=width 17 self._length=length 18 19 def info(self): 20 print(f'矩形左上角顶点坐标:({self._x},{self._y})') 21 print(f'矩形长:{self._length}') 22 print(f'矩形宽:{self._width}') 23 24 def area(self): 25 return self._length * self._width 26 27 def perimeter(self): 28 return(self._length+self._width)*2 29 30 class Circle(Shape): 31 def __init__(self,x=0,y=0,radius=1): 32 self._x=x 33 self._y=y 34 self._r=radius 35 36 def info(self): 37 print(f'圆心:({self._x},{self._y})') 38 print(f'半径:{self._r}') 39 40 def area(self): 41 return 3.14*self._r*self._r 42 43 def perimeter(self): 44 return 3.14*2*self._r 45 46 class Triangle(Shape): 47 def __init__(self,a=1,b=1,c=1): 48 self._a,self._b,self._c=a,b,c 49 50 def info(self): 51 print(f'三角形三边长:({self._a},{self._b},{self._c})') 52 53 def area(self): 54 s=(self._a+self._b+self._c)/2 55 ans=(s*(s-self._a)*(s-self._b)*(s-self._c))**0.5 56 57 return ans 58 59 def perimeter(self): 60 return(self._a+self._b+self._c) 61 62 def main(): 63 print('测试1:'.center(40,'*')) 64 shapes_lst1=[Circle(),Rect(),Triangle()] 65 for t in shapes_lst1: 66 t.info() 67 print(f'面积:{t.area():.2f}') 68 print(f'周长:{t.perimeter():.2f}') 69 print() 70 71 print('测试2:'.center(40,'*')) 72 shapes_lst2=[Circle(x=2,y=2,radius=10), 73 Rect(x=50,y=50,length=10,width=5), 74 Triangle(a=3,b=4,c=5)] 75 for t in shapes_lst2: 76 t.info() 77 print(f'面积:{t.area():.2f}') 78 print(f'周长:{t.perimeter():.2f}') 79 print() 80 81 if __name__=='__main__': 82 main()View Code
2.2
1 #2.2 2 from shape import Rect,Circle 3 4 shape_lst=[Rect(5,5,10,5),Circle(),Circle(1,1,10)] 5 6 for i in shape_lst: 7 i.info() 8 print(f'面积:{i.area():.2f}') 9 print(f'周长:{i.perimeter():.2f}') 10 print()View Code
#3
1 #3 2 from math import * 3 def func(x): 4 m=0 5 s=2 6 return (1/(2*pi)**0.5/s)*exp(-0.5*((x-m)/s)**2) 7 8 for i in range(1,10,2): 9 print(f'x={i},y={func(i):.8f}')View Code
#4
1 #4.1 2 from random import choice 3 class RandomWalk(): 4 def __init__(self,num_points=5000): 5 self.num_points=num_points 6 self.x_values=[0] 7 self.y_values=[0] 8 9 def fill_walk(self): 10 while len(self.x_values)<self.num_points: 11 x_direction=choice([1,-1]) 12 x_distance=choice([0,1,2,3,4]) 13 x_step=x_direction*x_distance 14 15 y_direction=choice([1,-1]) 16 y_distance=choice([0,1,2,3,4]) 17 y_step=y_direction*y_distance 18 19 if x_step==0 and y_step==0: 20 continue 21 22 next_x=self.x_values[-1]+x_step 23 next_y=self.y_values[-1]+y_step 24 25 self.x_values.append(next_x) 26 self.y_values.append(next_y) 27 28 def main(): 29 rw=RandomWalk(5) 30 rw.fill_walk() 31 print(rw.x_values) 32 print(rw.y_values) 33 34 if __name__=='__main__': 35 main()View Code
4.2
1 #4.2 2 from matplotlib import pyplot as plt 3 from random_walk import RandomWalk 4 from time import sleep 5 6 n=0 7 while n<2: 8 n+=1 9 rw=RandomWalk(50000) 10 rw.fill_walk() 11 12 plt.figure(figsize=(10,6),dpi=128) 13 point_numbers=list(range(rw.num_points)) 14 plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolors='none',s=1) 15 16 plt.scatter(0,0,c='grey',edgecolors='none',s=100) 17 plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100) 18 19 plt.axis('off') 20 plt.show()View Code
标签:__,10,self,实验,._,print,def From: https://www.cnblogs.com/047lishiqi/p/17462922.html