3 回答

TA贡献1966条经验 获得超4个赞
有两种方法可以做到这一点,
@paritosh-singh 说,把它写成一个函数
使用允许您进行符号计算的库,例如sympy
有了 sympy,你可以这样做
from sympy import *
x, y, z= symbols('x y z')
z = (x^2)+(y^2)
您现在可以为 x 和 y 分配值并获得输出为 z。
同情文档

TA贡献1829条经验 获得超6个赞
您应该使用sympy进行更清晰的计算
from sympy import *
x = Symbol('x') # define first symbol
y = Symbol('y') # define second symbol
output = x**2 + y**2 # form the equation
print(output) # print the equation on console
输出
x**2 + y**2
现在替换 x 和 y 的值,就像我们在任何数学方程中所做的那样
output.subs({x:1,y:1}) #substitue x::1 and y::1 to get the result
输出
2 # 1**2 ==1 and 1**2==1 and 1+1 =2
为了完整起见,您也可以在函数内部定义方程,但它对复杂方程的描述性较差
def func(x,y): return x**2 + y**2
现在您可以使用该函数来获取输出
func(1,1) #2
添加回答
举报