Think symbolicallyLesson 03 of 48 · 7 min
A symbol is an object
Distinguish a symbol from a Python name and from its printed label.
Maths refresher: Variables and values
A mathematical variable can stand for many permitted values. A Python assignment binds a name now; it is not an equation to solve.
If f is built using a symbol x, later assigning the Python name x=10 does not replace x inside that already-built symbolic object.
A Python variable is a name referring to an object. A SymPy Symbol is an object representing an unspecified quantity. Reassigning the Python name later does not rewrite expressions already built from that symbol.
- Build
- Keep
- Reassign Python name
- Stored expression
Symbols also carry assumptions. Two symbols can print with the same label but differ because one is positive and another has no such assumption. Mixing them accidentally can leave expressions that look simpler than their internal structure really is.
Construct symbols once and reuse those objects consistently. The example retains the original symbol in saved_x, then reassigns the Python name x. Inspect the expression’s free_symbols to identify the mathematical quantities that remain inside it.
After x=10, will an earlier expression built from the symbol x automatically become a number?
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
Existing expression: x + 2
Python name now: 10
Remaining symbols: {x}
Same labels, same objects?: False
Explicit replacement: 12
Read the starter result
Read Existing expression, Python name now and Remaining symbols. Assigning 10 to the Python name leaves the earlier expression unchanged. Explicit replacement uses the saved symbol object to put 10 into it.
Edit and compare
Replace positive_x = sp.Symbol("x",positive=True) with positive_x = sp.Symbol("p",positive=True). Select Run Python. Which symbol remains in Remaining symbols, and how do the symbol labels affect Same labels, same objects??
Scope. This lesson addresses ordinary symbolic objects, not every advanced binding or scoping construct in SymPy.
Try a fresh case
An expression unexpectedly contains two symbols printed as t. What should you inspect?
Need a hint?
Hint 1
A printed label is only part of a Symbol.
Hint 2
Compare assumptions and object equality.