Possible Duplicate:
Expression Versus Statement
What's the difference between expression and statement in Python?
I have never thought of this question until I am learning Python generator that said "use yield as an expression"
Also, can you explain this question in the context of Python generator that 'use yield as an expression'?
An expression can be evaluated to return a value. Any expression can also be used as a statement.
To put it another way, if you can write a = ...
, then ...
is an expression. So 2*3
and zip(x,y)
are expressions.
Something like raise Exception
is a statement but not an expression: you can't write a = (raise Exception)
.
yield being an expression means that b = (yield a)
is valid code in a generator. If you use the generator's send()
method, b
is set to the value you pass in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With