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
Python
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