Read a Line of Python

Name the parts of report lines you already understand: literals, names, operators, expressions, function calls, arguments, and statements. Read each line from its inner values to its complete effect.

We have already read and changed several Python lines. A small set of terms now lets us explain their parts precisely.

Values Come from Expressions

An expression is code that produces a value. In this line,

mean = total / count

total / count is an expression. It contains two names, total and count, and the division operator /. The values used by an operator are its operands.

A value written directly in source code is a literal. The numbers 4 and 20.5 and the string "°C" are literals:

A name is not the value itself. Python evaluates a name by finding the value it currently refers to.

A Statement Is a Complete Instruction

An assignment is a statement: a complete instruction that changes what a name refers to. Read it from the inside outward:

  1. find the current values of total and count;
  2. apply / to produce 20.5;
  3. assign that result to mean.

This order connects the parts of the source line to its effect.

Q1. Find the value-producing part

In distance = abs(reading - 20), which complete part produces the value that is assigned to distance?

Choose one

Select one choice, then check.

HintRead the right side

The whole expression to the right of = produces the new value.

SolutionThe function call produces the value

Python evaluates reading - 20, passes that result to abs(), and assigns the returned value to distance.

Not attempted
Review

Not marked done.

Function Calls Use Arguments

print() and abs() are built-in functions. A function call asks a function to perform its task. The values or expressions between its parentheses are arguments.

Consider the final report line:

print(f"Mean: {mean:.1f} {unit}")

Read it in this order:

  1. find the values referred to by mean and unit;
  2. build the formatted string Mean: 20.5 °C;
  3. pass that string as the argument to print();
  4. print() displays the text.

The inner f-string produces a value. The complete print() call is the instruction that displays it. To follow this line, evaluate the inner parts before the outer call.

Source partRole in the report line
mean, unitnames whose current values are read
"Mean: ..."an f-string literal that builds report text
f"Mean: {mean:.1f} {unit}"the argument supplied to print()
print(...)the function call and complete instruction

Q2. Read from the inside outward

What happens first while Python runs print("total:", 18 + 21)?

Choose one

Select one choice, then check.

HintStart with the inner expression

print() needs the result of 18 + 21 before it can display the arguments.

SolutionThe addition runs first

Python calculates 39, then calls print() with "total:" and 39. The output is total: 39.

Not attempted
Review

Not marked done.

Q3. Name each role

In total = reading_1 + reading_2, which items are operands of +?

Choose one

Select one choice, then check.

HintLook beside the plus sign

The two names immediately to the left and right of + supply its values.

SolutionThe reading names are operands

Python reads the values of reading_1 and reading_2, then + combines those two operands.

Not attempted
Review

Not marked done.

Literals and names supply values; operators and function calls form expressions; a statement gives Python a complete instruction. Reading from the inside outward explains how a line produces its effect.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.