liveBook · Manning

Expression

As explained in Python Workout: 50 ten-minute exercises

An expression is a syntactic construct that Python evaluates to produce a value.

An expression is a Python construct evaluated to produce a value. It can transform each item in a comprehension or appear inside an f-string, where Python evaluates it while formatting the string. section 7.5 section 1.6

[x*x                   #1
 for x in range(10)    #2
 if x%2 == 0]          #3

This comprehension uses x*x as its transformation expression and keeps only even values with an if condition. section 7.5

Comprehensions combine transformation and filtering. Their map and filter equivalents separate those jobs: map changes every input while filter decides which original elements remain. section 7.5 section 7.5

Expressions inside formatted strings

name = 'world' x = 100 y = 'abcd' f'x * 2 = {x*2}, and y.capitalize() is {y.capitalize()}' The braces contain expressions, not merely variable names, so arithmetic and method calls are evaluated during formatting. section 1.6

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains expressions through comprehensions and f-strings with runnable examples, without treating Python expression syntax or internals comprehensively.
Examples
Demonstrates squaring even numbers, mapping word lengths, filtering long words, and computing values inside formatted strings.
What you can do after
Apply expressions through comprehensions and f-strings to transform data, filter results, and format computed values.
How it connects
Connects expressions to list comprehensions, map, filter, iterables, and f-strings.

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 Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.