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.

f(x)=x+2f(x)=x+2
Worked example

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.

Maths prerequisite map ↗

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.

Names and objects
  1. Build
    x↦Symbol⁡(x)x\mapsto\operatorname{Symbol}(x)
  2. Keep
    expression=x+1\mathrm{expression}=x+1
  3. Reassign Python name
    x↦10x\mapsto10
  4. Stored expression
    expression=x+1\mathrm{expression}=x+1
Starter reference. Assignment changes a Python name’s binding. The existing expression still refers to its original symbol object.

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.

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.

Compare with the worked example

Executed from the published Python during the site build. Your own run appears above. Build environment ↗

Existing expression
x+2x + 2
Python name now
1010
Remaining symbols
{x}\left\{x\right\}
Same labels, same objects?
False\text{False}
Explicit replacement
1212
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??

Return to the editor

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?

Choose an answer
Need a hint?
Hint 1

A printed label is only part of a Symbol.

Hint 2

Compare assumptions and object equality.

Your notesNotes save in this browser when storage is available.