import sympy as sp
x = sp.symbols("x")
expr = (x + 1)**2
expr\(\displaystyle \left(x + 1\right)^{2}\)
After this lesson you should be able to create SymPy symbols, distinguish expressions from equations, use common symbolic commands, solve and check equations, inspect restrictions, make tables and plots, and compare SymPy with browser symbolic tools.
A numeric calculator gives decimal values. A symbolic system keeps exact expressions and manipulates formulas. SymPy is a Python computer algebra system. It helps you check and explore, but you still decide the domain and meaning.
\(\displaystyle \left(x + 1\right)^{2}\)
Use * for multiplication and ** for powers. Write 2*x, not 2x; write x**2, not x^2.
In Python, = assigns a name. A mathematical equation uses sp.Eq(left, right).
| Task | SymPy command | Meaning |
|---|---|---|
| Expand | sp.expand(expr) |
multiply out |
| Factor | sp.factor(expr) |
write as product |
| Simplify | sp.simplify(expr) |
try a simpler equivalent form |
| Cancel | sp.cancel(expr) |
simplify rational expression |
| Together | sp.together(expr) |
combine rational terms |
The result x + 1 needs a warning: the original expression excludes x = 1.
For radical equations, checking prevents extraneous answers.
Use sp.denom to inspect rational restrictions:
((x - 3)*(x + 2), [-2, 3])
For logs, require positive inputs. For even roots, require nonnegative radicands. Assumptions can help, but beginners should first write restrictions in words.
Exact values are usually better during algebra. Decimals are useful for final interpretation.
((-1.0, 6.0), (0.0, 2.0), (1.0, 0.0), (2.0, 0.0), (3.0, 2.0), (4.0, 6.0))
A PyScript sandbox could load SymPy using a package configuration. Verify the exact current PyScript syntax before publishing a live sandbox.
t.(x + 3)^2.(x + 3)^2.x^2 - 7x + 12.4x - 9 = 15.x = 4 solves x^2 - 7x + 12 = 0.(x + 1)/(x - 5).(x^2 - 1)/(x - 1) needs a warning.t = sp.symbols("t").(x + 3)**2.x**2 + 6*x + 9.(x - 4)*(x - 3).x = 6.0.x - 5.x = 1.Symbolic computation is powerful when you know what question you are asking. Guess the command, run it, and then interpret the result mathematically.
Guess first. Should x be typed as a Python string or a SymPy symbol before building x^2 + 1?
Guided exercise.
The expression is symbolic, so SymPy can expand, factor, differentiate later, and substitute exact values.
Checkpoint. Create symbols a and b, then build the expression (a + b)^2. Which SymPy command expands it?
Guess first. Is x**2 - 4 an equation by itself?
Guided exercise.
An expression can be simplified or factored. An equation can be solved.
Checkpoint. Write a SymPy equation for 3x + 2 = 11. Then solve it.
Guess first. Why should you check a symbolic answer after solving?
Guided exercise.
solve gives candidates
checksol tests candidates in the original equation
This matters especially for rational and radical equations, where restrictions or squaring can create invalid candidates.
Checkpoint. Solve sqrt(x + 5) = x - 1 with SymPy, then check each solution in the original equation.
Guess first. Which is more informative: sqrt(2) or 1.41421356?
Guided exercise.
Exact forms preserve structure. Decimal approximations help with measurement and graphing. Use exact answers while solving, then approximate when interpreting.
Checkpoint. Compute an exact value for log(5) and then a decimal approximation. State when each form is useful.
The solver layer expects SymPy objects. If a student starts with typed text, parse it first, then solve the parsed equation.
Answer: x = 3
Method: linear equation
Steps:
1. Original equation:
Eq(3*x + 2, 11)
2. Move everything to one side:
Eq(3*x - 9, 0)
This writes the equation in the form ax + b = 0.
3. Move the constant term:
Eq(3*x, 9)
4. Divide by the coefficient of the variable:
Eq(x, 3)
Check:
x = 3: valid
(x**2 - x - 6, (x - 3)*(x + 2))
[(4, True)]