Difference between statement and expression

Difference between statement and expression

What is a statement?Permalink

A statement is a piece of code that does something.

It performs action(s) or instruction(s) that has an effect.

For example, assigning value to a variable is a statement:

x = 5

It performs an assignment, and the effect is that now variable x has a value of 5.

As well as class definition, a for loop or if..else:

# definition of a class
class MyClass:
    pass

# for loop
for n in [1,2,3]:
    print(str(n))

if x == 6:
    print('it is six')
else:
    print('it is not six')
  • The class definition above defines a new class, and the effect is we have a new class which we can use.
  • for loop above loops over the list of numbers, and the effect is that it printed them.
  • if..else statement above checks the value of x and prints the string depending on the value.

What is an expression?Permalink

An expression is a piece of code that produces a value. We often say that expression can be evaluated (or reduced) to a value.

So an expression is anything that results in a value.

For example, this simple addition is an expression, as it evaluates a value of 5.

1 + 3
# evaluates to 5

Logical operators are also expressions because they always produce a bool value (True or False):

True and False
# evaluates to False

Another example is comparison operators; they always produce a bool value:

4 == 5
# evaluates to False

An expression can be part of another expression so that we can build more complex expressions.

4 == 5 or 2 == 2
# evaluaates to True

An expression can be part of a statement. For example if..else statement uses the expression in the if part as it needs the bool value to know what to execute:

x = 5
if x == 2:
    print('True branch')
else:
    print('False branch')

DifferencePermalink

A statement is a piece of code that does something.

An expression is a piece of code that evaluates to a value.

So the difference between statement and expression is that expression always produces (evaluates to) a value.

Every expression is a statement as well - it does something: it returns a value.

But not every statement is an expression unless it produces value.

So, an expression is a statement that produces (evaluates to) a value.

Expression always has a result which can be assign to a variable. Statement that is not also an expression has no result and therefore can’t be assigned to a variable.

We can do this:

# assign expression to a variable
x = 2 * 2
print(x)
# outputs 4

But we can’t do this:

# assign statement to x not possible

x = for n in [1,2]: print(n)

# outputs error
#  File "<stdin>", line 1
#    x = for n in [1,2]: print(n)
#        ^^^
# SyntaxError: invalid syntax

Trying to assign a statement to variable x is not valid; there is nothing to assign because the statement does not produce a value.

You might also like

Join the newsletter

Subscribe to get new articles about Python, code and programming into your inbox!