Think symbolicallyLesson 01 of 48 · 6 min
Your first symbolic expression
Construct an expression and recognise its mathematical meaning.
Maths refresher: Squares and brackets
Squaring a sum multiplies the whole sum by itself. The middle terms do not disappear.
At , both forms give 9. Squaring each term separately would give 5 and changes the expression.
A symbolic expression represents a calculation without requiring a numerical value for every quantity. SymPy keeps the symbol inside the result, so you can transform the expression before choosing a value for .
The mathematics squares the entire sum. In Python, write (x+1)**2: parentheses group the sum and ** means a power. Multiplication needs an explicit *. Importing sympy as sp gives a short, consistent prefix for SymPy’s functions.
Run the example and compare the original expression with its expanded form. Both describe the same polynomial. The results dictionary labels the expressions displayed by this lab; the final loop also prints them when you download and run the file locally.
Will and expand to the same polynomial?
Write a prediction (optional)
Exact symbolic investigation
Select Run Python with the code unchanged.
Your saved draft is open. To use the published code, select Restore example in Tools. Use Recover draft to bring back your code.
Tools
About Python
Python and the course packages can load while you read. Each run starts with fresh user variables; imported modules stay loaded. Stop replaces the worker. Runtime details ↗
Ctrl+Enter or ⌘+Enter runs Python.
Execution error
Technical error details
Your result
Python text output
Compare with the worked example
Executed from the published Python during the site build. Your own run appears above. Build environment ↗
Worked text output
Original: (x + 1)**2 Expanded: x**2 + 2*x + 1
Read the starter result
Read Original and Expanded. Both keep x as a symbol. In the diagram, the two side rectangles give the middle term in the starter’s expanded form.
Edit and compare
In the editor, replace expression = (x+1)**2 with expression = (x+2)**2. Predict the middle and constant terms. Select Run Python and compare Expanded and the area diagram with your first result.
Scope. Expansion changes form. It does not assign a value to x or solve an equation.
Try a fresh case
Which Python expression means the square of the sum of x and 3?
Need a hint?
Hint 1
Decide what the exponent applies to.
Hint 2
Use ** after the complete bracketed sum.