How to substitute multiple symbols in an expression in sympy?

Wesley picture Wesley · Oct 4, 2015 · Viewed 31.9k times · Source

Assigning a variable directly does not modify expressions that used the variable retroactively.

>>> from sympy import Symbol
>>> x = Symbol('x')
>>> y = Symbol('y')
>>> f = x + y
>>> x = 0

>>> f
x + y

Answer

Wesley picture Wesley · Oct 4, 2015

To substitute several values:

>>> from sympy import Symbol
>>> x, y = Symbol('x y')
>>> f = x + y
>>> f.subs({x:10, y: 20})
>>> f
30