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.

ax+bx=(a+b)xax+bx=(a+b)x
Worked example

For a=2a=2 and b=5b=5, the two xx terms become 7x7x. The parameters can remain symbolic while you perform the same grouping.

Maths prerequisite map ↗

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.

Group by powers of x
  1. Quadratic term
    2a  ⋅x22a\;\cdot x^2
  2. Linear term
    (a+b)  ⋅x(a+b)\;\cdot x
  3. Constant term
    bb
Starter reference. Read each variable term as coefficient × power. The coefficients are 2a and a + b; the constant term is b. The symbols a and b stay as parameters.

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.

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 ↗

Expression
2ax2+ax+bx+b2 a x^{2} + a x + b x + b
Collected in x
2ax2+b+x(a+b)2 a x^{2} + b + x \left(a + b\right)
Coefficient of x
a+ba + b
Collected in a
a(2x2+x)+bx+ba \left(2 x^{2} + x\right) + b x + b
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?

Return to the editor

Scope. coeff is a structural extraction tool; expansion or collection may be needed before asking for a particular coefficient.

Try a fresh case

In (a+b)x+3(a+b)x+3, what is the coefficient of x?

Choose an answer
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.

Your notesNotes save in this browser when storage is available.