Expression vs Statement in Python: What's the Difference?
by:
blow post content copied from Real Python
click here to view original post
After working with Python for a while, you’ll eventually come across two seemingly similar terms: expression and statement. When you browse the official documentation or dig through a Python-related thread on an online forum, you may get the impression that people use these terms interchangeably. That’s often true, but confusingly enough, there are cases when the expression vs statement distinction becomes important.
So, what’s the difference between expressions and statements in Python?
Get Your Code: Click here to download the free sample code you’ll use to learn about the difference between expressions and statements.
Take the Quiz: Test your knowledge with our interactive “Expression vs Statement in Python: What's the Difference?” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Expression vs Statement in Python: What's the Difference?In this quiz, you'll test your understanding of Python expressions vs statements. Knowing the difference between these two is crucial for writing efficient and readable Python code.
In Short: Expressions Have Values and Statements Cause Side Effects
When you open the Python glossary, you’ll find the following two definitions:
Expression: A piece of syntax which can be evaluated to some value. (…) (Source)
Statement: A statement is part of a suite (a “block” of code). A statement is either an expression or one of several constructs with a keyword, (…) (Source)
Well, that isn’t particularly helpful, is it? Fortunately, you can summarize the most important facts about expressions and statements in as little as three points:
- All instructions in Python fall under the broad category of statements.
- By this definition, all expressions are also statements—sometimes called expression statements.
- Not every statement is an expression.
In a technical sense, every line or block of code is a statement in Python. That includes expressions, which represent a special kind of statement. What makes an expression special? You’ll find out now.
Expressions: Statements With Values
Essentially, you can substitute all expressions in your code with the computed values, which they’d produce at runtime, without changing the overall behavior of your program. Statements, on the other hand, can’t be replaced with equivalent values unless they’re expressions.
Consider the following code snippet:
>>> x = 42
>>> y = x + 8
>>> print(y)
50
In this example, all three lines of code contain statements. The first two are assignment statements, while the third one is a call to the print()
function.
When you look at each line more closely, you can start disassembling the corresponding statement into subcomponents. For example, the assignment operator (=
) consists of the parts on the left and the right. The part to the left of the equal sign indicates the variable name, such as x
or y
, and the part on the right is the value assigned to that variable.
The word value is the key here. Notice that the variable x
is assigned a literal value, 42
, that’s baked right into your code. In contrast, the following line assigns an arithmetic expression, x + 8
, to the variable y
. Python must first calculate or evaluate such an expression to determine the final value for the variable when your program is running.
Arithmetic expressions are just one example of Python expressions. Others include logical expressions, conditional expressions, and more. What they all have in common is a value to which they evaluate, although each value will generally be different. As a result, you can safely substitute any expression with the corresponding value:
>>> x = 42
>>> y = 50
>>> print(y)
50
This short program gives the same result as before and is functionally identical to the previous one. You’ve calculated the arithmetic expression by hand and inserted the resulting value in its place.
Note that you can evaluate x + 8
, but you can’t do the same with the assignment y = x + 8
, even though it incorporates an expression. The whole line of code represents a pure statement with no intrinsic value. So, what’s the point of having such statements? It’s time to dive into Python statements and find out.
Statements: Instructions With Side Effects
Statements that aren’t expressions cause side effects, which change the state of your program or affect an external resource, such as a file on disk. For example, when you assign a value to a variable, you define or redefine that variable somewhere in Python’s memory. Similarly, when you call print()
, you effectively write to the standard output stream (stdout), which, by default, displays text on the screen.
Note: While statements encompass expressions, most people use the word statement informally when they refer to pure statements or instructions with no value.
Okay. You’ve covered statements that are expressions and statements that aren’t expressions. From now on, you can refer to them as pure expressions and pure statements, respectively. But it turns out there’s a middle ground here.
Read the full article at https://realpython.com/python-expression-vs-statement/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
December 04, 2024 at 07:30PM
Click here for more details...
=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
Post a Comment