Expressions and Statements
An expression produces a value.
2 + 3
The value is 5.
A statement is an instruction Python executes.
score = 2 + 3
This statement assigns the value 5 to the name score.
Expressions Can Be Inside Statements
This line contains an expression inside a statement:
print(2 + 3)
The expression is:
2 + 3
The statement is the whole instruction:
print(2 + 3)
Python evaluates the expression, then passes the result to print().
Assignment Statements
This statement:
loss = 0.5
does not print anything. It changes what the name loss refers to.
If you want to see the value, add a print statement:
Programs Combine Values and Actions
Later, you will read lines like:
prediction = weight * x + bias
The right side is an expression. It computes a value.
The whole line is a statement. It stores that value under the name
prediction.
That pattern appears everywhere in numerical code.
Expression inside assignment
The right side computes a value; the assignment stores it under a name.
Ready to run.
Assignment Is Not a Value
Do not expect assignment to show output:
score = 10
This line is useful, but silent.
What value does the right side produce?
prediction = 3 * 4 + 2
Compute it first, then check your number.
HintEvaluate the right side
Multiplication happens before addition: begin with 3 * 4.
SolutionThe expression produces fourteen
3 * 4 produces 12, and 12 + 2 produces 14. The assignment then makes
prediction refer to that value.
Separate Computing from Doing
Expressions compute values. Statements do work. Most useful Python lines combine the two.