Python
Programming
Expressions
Statements
Python Basics

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.

Browse interview questions

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:

python
12 + 3
2"hello".upper()
3len([1, 2, 3])
4[x * 2 for x in range(3)]

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.

python
result = (10 - 4) * 2
print(result)

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:

python
1x = 10
2if x > 5:
3    print("large")
4
5for item in [1, 2, 3]:
6    print(item)
7
8def greet(name):
9    return f"Hello, {name}"

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:

python
1temperature = 21
2
3if temperature > 20:
4    print("open the window")

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:

python
print("side effect only")

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:

python
total = 4 + 5

because 4 + 5 is an expression with a value.

You cannot write:

python
total = for x in range(5):
    ...

because for is a statement, not an expression. Python does have expression forms that resemble statements, such as conditional expressions and comprehensions:

python
status = "adult" if age >= 18 else "minor"
squares = [x * x for x in range(5)]

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.

python
1numbers = [1, 2, 3, 4]
2
3if (count := len(numbers)) > 3:
4    print(count)

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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.