liveBook · Manning

Expression

As explained in Python How-To

An expression is a syntactic form in Python that can be evaluated to produce a value.

An expression is a syntactic form that evaluates to a value or object. Variables, dictionary lookups, function calls, arithmetic, and strings can all serve as expressions. A statement performs an action instead; for example, assert checks a condition rather than producing a value for use elsewhere. section 2.1.3

tasks = ["homework", "laundry", "grocery shopping"]
assert f"First Task: {tasks[0]}" == 'First Task: homework'                #1
 
task_name = "grocery shopping"
assert f"Task Name: {task_name.title()}" == 'Task Name: Grocery Shopping' #2
 
number = 5
assert f"Square: {number*number}" == 'Square: 25'                         #3

See expressions evaluated directly inside f-string braces: a list lookup produces homework, a method call produces title-cased text, and multiplication produces 25. section 2.1.3

F-strings can evaluate expressions inside curly braces, which avoids unnecessary intermediate variables. Keep those expressions simple, however. A dense calculation may work but can make the intended output difficult to understand. Prefer named intermediate steps when they improve readability, such as storing scores, their total, the subject count, and the resulting average separately. section 2.1.3

scores = [95, 98, 97, 96, 97, 93]

total_score = sum(scores)
subject_count = len(scores)
average_score = total_score / subject_count

summary_text = f"Your Average Score: {average_score}."

Compare a complicated average calculation embedded in an f-string with the clearer version that separates data, calculation steps, and formatted output. section 2.1.3

A lambda function consists of the lambda keyword, its arguments, and one expression after a colon. That expression supplies the function’s value; a return statement is invalid because return is a statement. For example, doubler = lambda x: x * 2 creates a callable function that produces 10 for doubler(5) and 16 for doubler(8). section 7.1.1

doubler = lambda x: x * 2

Inspect the minimal lambda definition, then compare it with the separate examples showing its function type and calls. section 7.1.1

How Python How-To covers it

How deep it goes
Defines evaluation versus action, then demonstrates f-string readability and lambda syntax without pursuing Python evaluation internals.
Examples
Demonstrates list lookup, method calls, arithmetic, average-score formatting, and a doubling lambda.
What it assumes
Assumes basic Python values, variables, functions, strings, lists, and dictionaries while teaching the expression-statement distinction explicitly.
What you can do after
Apply expressions in f-strings and lambda functions while separating complicated calculations into readable steps.
How it connects
Connects expressions with variables, function calls, f-strings, statements, and lambda functions.

Also explained in

Compare how 11 books explain expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.