What is the difference between an expression and a statement in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The difference between an expression and a statement is one of the most useful mental models in Python. Once you understand it, syntax errors become easier to diagnose and Python code starts to look much more predictable.
What Counts as an Expression
An expression is any piece of Python code that evaluates to a value. That value might be a number, a string, a list, a function result, or even None, but the important part is that the interpreter can reduce the code to a result.
These are all expressions:
Each line produces a value. You can assign that value, pass it into a function, compare it, or use it as part of a larger expression.
The right-hand side is an expression whose value becomes 12.
What Counts as a Statement
A statement is an instruction that Python executes. Statements control flow, define names, or change program state. They do not primarily exist to produce a reusable value.
Examples of statements include:
Assignment, if, for, def, return, import, and class are all statements. They tell Python to do something.
How Expressions and Statements Work Together
Statements often contain expressions inside them. In x = 10 + 5, the assignment is the statement and 10 + 5 is the expression. In an if statement, the condition is an expression:
temperature > 20 evaluates to either True or False, and the if statement uses that value to decide what to execute.
A function call is also an expression. When you put it on its own line, it becomes an expression statement:
print("side effect only") still evaluates as an expression, but because it appears by itself, Python treats the whole line as a statement to execute for its side effect.
Why the Distinction Matters
The distinction explains why some constructs fit in places where others do not.
You can write:
because 4 + 5 is an expression with a value.
You cannot write:
because for is a statement, not an expression. Python does have expression forms that resemble statements, such as conditional expressions and comprehensions:
These are valid because they evaluate to values.
The Assignment Expression Exception
Python added the assignment expression operator :=, often called the walrus operator. It lets you assign a value inside an expression context.
This does not erase the expression-versus-statement distinction. Regular assignment with = is still a statement. The walrus operator is simply a special expression form that also binds a name.
A Practical Rule of Thumb
Ask a simple question: "Does this piece of code produce a value I can use immediately?" If the answer is yes, it is probably an expression. If it mainly tells Python to perform an action, it is probably a statement.
That rule is not just academic. It helps when reading tracebacks, designing APIs, and understanding why comprehensions work inside function calls while loops do not.
Common Pitfalls
One common mistake is thinking that anything on the right side of = must be a statement because it causes work to happen. In reality, function calls, arithmetic, and comprehensions are expressions even when they do useful work.
Another common mistake is trying to use = inside a condition. Regular assignment is a statement, so Python rejects code such as if x = 5:. If you truly need assignment inside an expression, use := carefully.
A third pitfall is forgetting that a standalone expression can still be a statement. Calling a function on its own line is a classic example.
Summary
- An expression evaluates to a value
- A statement tells Python to perform an action
- Statements often contain expressions inside them
- A standalone expression can appear as an expression statement
- Regular assignment with
=is a statement, while:=is an expression form
Related reading
- What is the difference between dict.items and dict.iteritems in Python 2?
- What is the difference between flatten and ravel functions in numpy?
- What is the difference between is None and None
- What is the difference between iterator and iterable and how to use them?
- What is the difference between join and merge in Pandas?
- What is the difference between json.dump and json.dumps in python?
- What is the difference between json.load and json.loads functions
- What is the difference between Lock and RLock
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.