Thanks for the thorough answer, Bob. I now understand the difference. On Apr 10, 2014 2:11 PM, "bob gailer" <bgai...@gmail.com> wrote:
> Caveat: I began this before there were any other responses. So this may be > overkill - but I ike to be thorough. > > On 4/9/2014 12:49 PM, Jared Nielsen wrote: > >> Hi Pythons, >> Could someone explain the difference between expressions and statements? >> >>> I know that expressions are statements that produce a value. >>> >> No. Expressions are not statements. These are mutually exclusive. >> Expressions do produce values. >> > An attempt at a thorough answer: > > In the language reference glossary under "expression" you will find: > > "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.... There are also statements which cannot be used as expressions, > such as if. Assignments are also statements, not expressions." > > Tthe above is a quote; I don't like some of the grammar. > > In your examples print is a function. So all calls to print are > expressions. > > In the language reference you will also find: > > 7. Simple statements > 7.1. Expression statements > 7.2. Assignment statements > 7.3. The assert statement > 7.4. The pass statement > 7.5. The del statement > 7.6. The return statement > 7.7. The yield statement > 7.8. The raise statement > 7.9. The break statement > 7.10. The continue statement > 7.11. The import statement > 7.12. The global statement > 7.13. The nonlocal statement > 8. Compound statements > 8.1. The if statement > 8.2. The while statement > 8.3. The for statement > 8.4. The try statement > 8.5. The with statement > 8.6. Function definitions > 8.7. Class definitions > > With the exception of > - 7.1. Expression statements > - all of the above are either start with a keyword except 7.2 assignment > which is indicated by an equal sign (=) . > - all of the above cause something to happen (except pass), and do not > return a value. > > 7.1. Expression statement is either one expression or several separated by > commas. > Used interactively to display value(s). > Used anywhere to make a function call. > >> I'm unclear on functions and especially strings. >> Are any of the following expressions? >> >> print(42) >> print("spam") >> spam = 42 >> print(spam) >> >> Is the first example producing a value or simply displaying an integer? >> > All function calls return a value. In the case of print the return value > is always None. > spam = 42 is a statement. (indicated by the = sign. 42 is a value. > >> Does a string count as a value? >> Yes - however I suspect you are limiting "string" to something within >> quotes. Those are "string literals". >> Is a variable assignment considered a value? >> > No > >> If I print a variable is that considered production of a value? >> > See above comment on print. > > Long but comprehensive answer. Feel free to ask questions. > > Note there are various subtleties here -some keywords may be used to > start a statement or in an expression - e.g. if, else, for yield. > > This also raises the fact that else (inter ala) is neither an expression > or a statement; rather it is part of a compound statement. Nothing is > simple. > > Oh there is more but I may never hit send.... > >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor