Python Forum
difference between statement and expression
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
difference between statement and expression
#1
hi
On the site realpython.com, I saw that was said that yield is a statement.
what is the difference between a statement and an expression in Python? plz, explain.
thanks
Reply
#2
It's always a good idea to start with documentation:

Expression:

Quote:A piece of syntax which can be evaluated to some value. In other words, an expression is an accumulation of expression elements like literals, names, attribute access, operators or function calls which all return a value. In contrast to many other languages, not all language constructs are expressions. There are also statements which cannot be used as expressions, such as while. Assignments are also statements, not expressions.

Statement:

Quote: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, such as if, while or for.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(Oct-16-2023, 12:48 PM)perfringo Wrote: It's always a good idea to start with documentation:

Expression:

Quote:A piece of syntax which can be evaluated to some value. In other words, an expression is an accumulation of expression elements like literals, names, attribute access, operators or function calls which all return a value. In contrast to many other languages, not all language constructs are expressions. There are also statements which cannot be used as expressions, such as while. Assignments are also statements, not expressions.

Statement:

Quote: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, such as if, while or for.

hi
is it true that expression is oneline code, while statement is a block code?
i read above , but i can not understand the difference.
Reply
#4
All code are statements. Only code that returns a value is an expression.
i = 0  # Statement
i == 0  # Statement and expression
while i < 5:   # A statment that contains an expression, i < 5 is an expression
    print(i)  # only a statement.  No returned value
    i = i + 1  # A statement,  i + 1 is an expression
akbarza likes this post
Reply
#5
(Oct-17-2023, 10:58 AM)deanhystad Wrote: All code are statements.
(Oct-17-2023, 10:58 AM)deanhystad Wrote:
print(i)  # only a statement.  No returned value
This is not correct. As mentioned in the definition, referenced by @perfringo

(Oct-16-2023, 12:48 PM)perfringo Wrote: an expression is an accumulation of expression elements like literals, names, attribute access, operators or function calls which all return a value.
In python3 print is a function. When call print(i) it returns (i.e. evaluates to) None.
Any function call, incl. call of print() is an expression. In your example (and normally) we just throw away the value None as we don't care.
Compare with python2 where print is indeed statement

python3

>>> spam = print('eggs')
eggs
>>> spam is None
True
>>>
python2

>>> spam = print 'eggs'
  File "<stdin>", line 1
    spam = print 'eggs'
                 ^
SyntaxError: invalid syntax
akbarza likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Why not use the Python grammar specification to understand what a statement is and what an expression is? An expression is a special kind of 'simple_stmt' called a 'star_expressions'.

Also read the Python language reference.
akbarza likes this post
Reply
#7
thanks all
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  yield usage as statement or expression akbarza 5 845 Oct-23-2023, 11:43 AM
Last Post: Gribouillis
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,694 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Pass results of expression to another expression cmdr_eggplant 2 2,305 Mar-26-2020, 06:59 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020