---
author: Nathan Carter (ncarter@bentley.edu)
---

This answer assumes you have imported SymPy as follows.


```python
from sympy import *                   # load all math functions
init_printing( use_latex='mathjax' )  # use pretty math output
```

Let's assume we've defined a variable and created a formula, as covered
in how to create symbolic variables.


```python
var( 'x' )
formula = x**2 + x
formula
```




$\displaystyle x^{2} + x$



We can substitute a value for $x$ using the `subs` function.
You provide the variable and the value to substitute.


```python
formula.subs( x, 8 )  # computes 8**2 + 8
```




$\displaystyle 72$



If you had to substitute values for multiple variables, you can use
multiple `subs` calls or you can pass a dictionary to `subs`.


```python
var( 'y' )
formula = x/2 + y/3
formula
```




$\displaystyle \frac{x}{2} + \frac{y}{3}$




```python
formula.subs( x, 10 ).subs( y, 6 )
```




$\displaystyle 7$




```python
formula.subs( { x: 10, y: 6 } )
```




$\displaystyle 7$



140d951638e08ebbdca3a83391b792743e81e51b
ipynb
Jupyter Notebook
database/tasks/How to substitute a value for a symbolic variable/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
database/tasks/How to substitute a value for a symbolic variable/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
database/tasks/How to substitute a value for a symbolic variable/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
2021-07-18T19:01:29.000Z
2022-03-29T06:47:11.000Z
Qwen/Qwen-72B
1. YES
2. YES