Expressions and Statements in Python
By
Darío Rivera
Posted On
in
Python
A statement is an execution unit, while an expression is an evaluation unit. Simply put, everything that returns a value in Python is an expression. Understanding this will help you create clearer and cleaner programs.
Let's look at the following program as an example:
import platform
version = platform.python_version()
print('This is python version {}'.format(version))
The first line is a statement, it does not return any value, but can be executed by itself to import platform. This means that every line of code in Python is a statement. The third line of code is both a statement (as it is a line of code) and an expression since we are assigning a value to a variable. Finally, the last line is also an expression since although we are not explicitly assigning the value of a function, the function itself is returning a value.