# How to create symbolic variables (in Python, using SymPy)

See all solutions.

The word “variable” does not mean the same thing in mathematics as it does in computer programming. In mathematics, we often use it to mean an unknown for which we might solve; but in programming, variables typically have known values.

If we want to do symbolic mathematics in a software package, how can we tell the computer that we want to use variables in the mathematical sense, as symbols whose value may be unknown?

## Solution

This answer assumes you have imported SymPy as follows.

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


You can define any number of variables as follows. Here we define $x$, $y$, and $z$.

1
var( 'x y z' )


$\displaystyle \left( x, \ y, \ z\right)$

You can tell that they are variables, because when you ask Python to print them out, it does not print a value (such as a number) but rather just the symbol itself.

1
x


$\displaystyle x$

And when you use a symbol inside a larger formula, it doesn’t attempt to compute a result, but stores the entire formula symbolically.

1
2
formula = sqrt(x) + 5
formula


$\displaystyle \sqrt{x} + 5$