import math
def add(a,b):
return(a+b)
def subtract(a,b):
return(a-b)
def mutiple(a,b):
return(a*b)
def devide(a,b):
if b==0:
return('除数为0,无法计算')
else:
return(a/b)
代码A定义模块
代码B调用
import A
import inspect
# 获取 A.py 文件中的所有对象
members = inspect.getmembers(A)
# 遍历并筛选函数对象
for name, obj in members:
if inspect.isfunction(obj):
print(f"函数名称:{name}")
result=A.add(3,4)
print('相加的结果是',result)
result=A.subtract(3,4)
print('相减的结果是',result)
result=A.mutiple(3,4)
print('相乘的结果是',result)
result=A.devide(3,4)
print('相除的结果是',result)
from A import add
from A import subtract
from A import mutiple
from A import devide
re=add(2,8)
print('相加的结果是',re)
re=subtract(2,8)
print('相减的结果是',re)
re=mutiple(2,8)
print('相乘的结果是',re)
re=devide(2,8)
print('相除的结果是',re)
5个函数画在一张图里
import matplotlib.pyplot as plt
import numpy as np
# 创建 x 值的范围
x = np.linspace(0.1, 10, 100)
# 计算对应的 y 值
y1 = x
y2 = x**2
y3 = np.sqrt(x)
y4 = np.log(x)
y5 = 1 / x
y6 = np.sin(x)
# 创建并配置图形和子图
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(16, 8))
fig.suptitle('Graphs of Functions')
# 在第一个子图上绘制 y = x, y = x^2, y = sqrt(x), y = log(x), y = 1/x
ax1.plot(x, y1, label='y = x', color='red')
ax1.plot(x, y2, label='y = x^2', color='blue')
ax1.plot(x, y3, label='y = sqrt(x)', color='green')
ax1.plot(x, y4, label='y = log(x)', color='purple')
ax1.plot(x, y5, label='y = 1/x', color='orange')
# 添加标题和标签到第一个子图
ax1.set_title('Group of five function')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.legend()
# 在第二个子图上绘制 y = sin(x)
ax2.plot(x, y6, label='y=sin(x)', color='red')
# 添加标题和标签到第二个子图
ax2.set_title('y = sin(x)')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
# 调整子图布局
plt.tight_layout()
# 显示图形
plt.show()