![]() |
VOOZH | about |
sympy.subs() method in Python is used to substitute a variable or expression with a specified value or another expression in a symbolic mathematical expression. It is part of the SymPy library, which provides functionality for symbolic mathematics in Python, allowing you to work with equations, polynomials, integrals, and more.
This example demonstrates how to substitute one variable with another symbolic variable in an expression.
Output
Before Substitution : x**2 + 1
After Substitution : y**2 + 1
Explanation:
The code uses SymPy to create symbolic variables x and y. The expression x**2 + 1 is defined. The subs() method is used to replace x with y in the expression.
sympy.subs(old, new)
The method returns a new expression with the substitution made. It does not modify the original expression but returns a new expression where the substitution is applied.
This example shows how to substitute a variable with a numeric value (0 in this case) in a mathematical expression.
Output
Before Substitution : cos(x) + 7
After Substitution : 8
Explanation:
This example illustrates how to perform multiple variable substitutions simultaneously using a list of tuples in the subs() method.
Output
Before Substitution : x**2 + 7*y + z
After Substitution : 33
Explanation: The code defines the symbolic variables x, y, and z using SymPy. It then creates an expression x**2 + 7 * y + z, and substitutes values for x, y, and z using the subs() method.