expression
In Python, an expression is a combination of values, variables, operators, and function calls that can be evaluated to produce a value. Unlike statements, which perform actions, expressions always resolve to a single value.
A simple expression might be a literal value like 42
or a variable name like x
. More complex expressions can combine multiple elements using operators (e.g., a + b * 2
) or function calls (e.g., len(my_list) + 5
).
Key Characteristics
- Always evaluates to a value
- Can be used wherever Python expects a value
- Can be nested within other expressions
- Has no side effects (though function calls within expressions may have side effects)
Examples
x = 5 # 5 is an expression
y = x + 3 # x + 3 is an expression
z = (x + 2) * 3 # (x + 2) * 3 is an expression
name = "Alice" # "Alice" is an expression
Related Resources
Tutorial
Expression vs Statement in Python: What's the Difference?
In this tutorial, you'll explore the differences between an expression and a statement in Python. You'll learn how expressions evaluate to values, while statements can cause side effects. You'll also explore the gray areas between them, which will enhance your Python programming skills.
For additional information on related topics, take a look at the following resources:
By Dan Bader • Updated Jan. 17, 2025