Shape an expressionLesson 08 of 48 · 7 min
Collect the terms you need
Group an expression by powers of a chosen symbol.
Maths refresher: Like terms
Terms with the same power of a variable can be combined by adding their coefficients.
For and , the two terms become . The parameters can remain symbolic while you perform the same grouping.
An expression can contain several symbols. Collecting with respect to one of them groups its powers while treating the others as coefficients. This is useful when a model depends on both an independent variable and adjustable parameters.
- Quadratic term
- Linear term
- Constant term
Use collect after expansion when necessary. The result can expose how strongly each parameter or power contributes without substituting particular values. The coeff method extracts the coefficient of a specified power from an appropriate expanded expression.
Below, x is the grouping variable while a and b remain symbolic coefficients. Compare the full expression with the collected form and the coefficient of x. Grouping is a representation choice; it does not change the mathematical value.
When collecting in x, must a and b first be assigned numerical values?
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
Expression: 2*a*x**2 + a*x + b*x + b Collected in x: 2*a*x**2 + b + x*(a + b) Coefficient of x: a + b Collected in a: a*(2*x**2 + x) + b*x + b
Read the starter result
Compare Collected in x with Collected in a. The grouping changes, and all the terms still represent the same expression.
Edit and compare
Append +b*x**2 to the expression line. Change .coeff(x,1) to .coeff(x,2) and its label from "Coefficient of x" to "Coefficient of x squared". Select Run Python. Where does the new coefficient appear in Collected in x?
Scope. coeff is a structural extraction tool; expansion or collection may be needed before asking for a particular coefficient.
Try a fresh case
In , what is the coefficient of x?
Need a hint?
Hint 1
Treat a and b as fixed while grouping by x.
Hint 2
Identify the factor multiplying the first power of x.