Understanding Python Statements and Expressions
When you start learning Python, you will see terms like statement and expression. These words sound technical, but the idea is simple.
A statement is code that tells Python to do something.
An expression is code that Python evaluates to a value.
You will often see both working together in the same line of code. Once you understand that, beginner code becomes much easier to read.
Quick rule
x = 10
print(x + 5)
# x = 10 is a statement
# x + 5 is an expression
# print(x + 5) is a statement that contains an expression
Use this simple rule: an expression produces a value, while a statement performs an action.
What this page helps you understand
- A statement is a line of code that does something
- An expression is code that produces a value
- Many Python lines contain both a statement and an expression
- You do not need advanced theory to use this idea in practice
What is a statement in Python?
A statement tells Python to perform an action.
Common examples of statements include:
- variable assignment
ifstatementsforloopsimportstatements- function definitions with
def
Statements often:
- control the flow of a program
- create or change variables
- run blocks of code
- perform actions
A statement does not need to return a value.
Examples of statements
x = 10
import math
if x > 5:
print("x is greater than 5")
In this example:
x = 10is an assignment statementimport mathis an import statementif x > 5:starts anifstatementprint("x is greater than 5")is commonly treated as a statement in beginner code because it performs an action
If you are still learning Python basics, it helps to first understand Python syntax basics because statements are a big part of Python syntax.
What is an expression in Python?
An expression is code that Python can evaluate to a value.
That value might be:
- a number
- a string
TrueorFalse- a list
- the result of a function call
Examples of expressions
5
"hello"
x + 1
len("Sam")
age > 18
Each of these produces a value:
5produces the value5"hello"produces a stringx + 1produces a calculated resultlen("Sam")produces3age > 18produces eitherTrueorFalse
Expressions can be simple or built from smaller parts.
You will often see expressions on the right side of = in Python variables.
Statement vs expression: the simple difference
The easiest way to remember the difference is this:
- Expression = produces a value
- Statement = performs an action
Here is a simple example:
price = 20
tax = 5
total = price + tax
print(total)
What is happening here?
total = price + taxis an assignment statementprice + taxis an expressionprint(total)performs an action, so beginners usually treat it as a statementtotalinsideprint(total)is an expression because Python evaluates it to its current value
Common examples beginners see
These are patterns you will see often in real code.
Assignment
name = "Sam"
name = "Sam"is a statement"Sam"is an expression
Printing a value
print(name)
print(name)is usually treated as a statement in beginner explanationsnameis an expression
Condition in an if statement
if score > 50:
print("Pass")
if score > 50:is anifstatementscore > 50is the condition expression
If you want to go deeper into conditions, see Python if statements explained.
Loop
for item in items:
print(item)
for item in items:is aforstatementitemanditemsare names used in the statement
How statements and expressions work together
Statements and expressions are not separate worlds. In real Python code, statements often contain expressions.
Assignment uses an expression
x = 2 + 3
print(x)
x = 2 + 3is a statement2 + 3is the expression- Python evaluates the expression first, then assigns the result to
x
Output:
5
If statements use expressions as conditions
age = 20
if age >= 18:
print("Adult")
if age >= 18:is a statementage >= 18is an expression that becomesTrueorFalse
Function calls can produce values
name = "hello"
length = len(name)
print(length)
length = len(name)is a statementlen(name)is an expression because it returns a valuenameis also an expression when Python reads its value
If functions still feel unclear, Python functions explained is a good next step.
How to identify each one in code
When you are not sure, ask these questions.
Ask: does this code produce a value?
If yes, it is an expression.
Examples:
10"cat"xx + 1len(text)4 > 2
Ask: does this code tell Python to do something?
If yes, it is a statement.
Examples:
x = 10if x > 5:for item in items:while count < 3:def greet():import math
Look for common statement patterns
These often signal statements:
=ifforwhiledefimport
Look for common expression patterns
These often signal expressions:
- numbers
- strings
- variable names
- calculations
- comparisons
- function results
Why this matters for beginners
This idea helps more than many beginners expect.
It can help you:
- read code more clearly
- understand what each part of a line is doing
- make better sense of error messages
- learn variables, conditions, loops, and functions faster
- write cleaner Python code
You do not need formal computer science theory here. You just need to recognize:
- what part gives a value
- what part performs an action
That is enough for most beginner Python code.
Common mistakes
Beginners often get confused in these situations.
Thinking every line of code is only one thing
Some lines contain both a statement and one or more expressions.
Example:
x = 10 + 5
- whole line: statement
10 + 5: expression
Confusing assignment with calculation
In this code:
total = price + tax
total = ...is the statementprice + taxis the calculation expression
Assuming print() is the same as the value it displays
x = 7
print(x)
print(x) shows the value on the screen, but it is not the same thing as the value itself.
xis an expressionprint(x)performs an action
Not realizing conditions are expressions
x = 10
print(x > 5)
Here, x > 5 is an expression. It produces True or False.
Try these examples yourself
Run these small examples and notice which parts are statements and which parts are expressions.
print(type(5))
print(2 + 3)
x = 10
print(x)
print(len("hello"))
print(4 > 2)
Expected output:
<class 'int'>
5
10
5
True
What to notice:
5,2 + 3,x,len("hello"), and4 > 2are expressions- the
print(...)lines perform actions x = 10is a statement that sets a variable
If you are new to variables, you may also want to read what a variable means in Python.
FAQ
Is print() a statement or an expression in Python?
In beginner-focused Python explanations, print(...) is best treated as a statement because it performs an action. The value inside print(...) is usually an expression.
Can a statement contain an expression?
Yes. This is very common.
Example:
x = 2 + 3
This is a statement that contains the expression 2 + 3.
Is a variable name an expression?
Yes. A variable name like x can be used as an expression because Python evaluates it to its current value.
Why do I need to learn statements and expressions?
It helps you understand how Python code is built. That makes it easier to read examples, write code, and fix mistakes.
See also
- Python syntax basics explained
- Python variables explained for beginners
- Python if statements explained
- Python functions explained
- What is a variable in Python?
Understanding statements and expressions is a strong first step. The next best topics are Python syntax and variables, because these ideas become much clearer when you see them in real beginner code.