Re: [Tutor] difference between expressions and statements
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
Re: [Tutor] difference between expressions and statements
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
Re: [Tutor] difference between expressions and statements
On 4/10/2014 5:48 PM, Jared Nielsen wrote: Thanks for the thorough answer, Bob. I now understand the difference. Thanks for the ACK. It helps me remember I have something to contribute. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] difference between expressions and statements
Could someone explain the difference between expressions and statements? I know that expressions are statements that produce a value. Yes, that's pretty much it. If you can point your finger at the thing and say that it produces a value, it's an expression. Are any of the following expressions? print(42) print(spam) spam = 42 print(spam) See: https://docs.python.org/2/reference/expressions.html Technically, the only things that count as expressions have to fit the shape of something described in that documentation link. Your question about assignment being an expression or not is something you can find out by seeing where Assignment statements show in: https://docs.python.org/2/reference/index.html Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] difference between expressions and statements
On 09/04/14 17:49, Jared Nielsen wrote: Hi Pythons, Could someone explain the difference between expressions and statements? I know that expressions are statements that produce a value. Yep, that's it. I'm unclear on functions and especially strings. Unclear in what way? Both functions and strings are expressions, in that they produce, or are, values. Are any of the following expressions? print(42) print(spam) spam = 42 print(spam) Yes, 3 are expressions, and all 4 are statements containing expressions. Is the first example producing a value or simply displaying an integer? It does actually produce a value but its not the integer that is displayed. The default value for any function (including print() ) is None... You can prove that by trying: print( print(42) ) 42 None The 42 is the output displayed by the innermost print() The None is the value returned by the inner print function. The Python interpreter normally suppresses the None from a print function but because I explicitly told it to print the return from print it did it in this case. Does a string count as a value? Yes, certainly. Is a variable assignment considered a value? No, its a statement but not an expression. (In Python at least, in some other languages the rules are different) If I print a variable is that considered production of a value? Yes, as above. But the value produced is the None returned by the print function not the value that print displays. HTH And did I just do your homework? hmmm... I'll give you the benefit of the doubt. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] difference between expressions and statements
Jared Nielsen nielsen.ja...@gmail.com writes: Could someone explain the difference between expressions and statements? For general programming terminology, the Wikipedia articles tend to be good. URL:https://en.wikipedia.org/wiki/Expression_%28computer_science%29 URL:https://en.wikipedia.org/wiki/Statement_%28computer_science%29 Roughly: * An expression evaluates to some single value. * A statement does some action. I know that expressions are statements that produce a value. Expressions are not statements. A statement *may* be an expression. Statements in Python can consist of an expression, or can consist of other things. I'm unclear on functions and especially strings. You're looking for a strict dichotomy which doesn't exist, and I think that's confusing you. Are any of the following expressions? They can all be statements. print(42) print(spam) These are function calls. A function call evaluates to a value, so is always an expression. spam = 42 Assignment is only a statement in Python. The statement is an instruction to perform the assignment. The left side is an expression evaluating to a reference; the right side is an expression evaluating to a value. Is a variable assignment considered a value? In Python, assignment (try not to think in terms of “variable”; assignment is the act of binding a reference to a value) is always a statement. In other languages, assignment can return a value and is therefore an expression. That is not the case in Python. Is the first example producing a value or simply displaying an integer? Does a string count as a value? Yes to all these. Learn more about statements – especially the fact that statements consist sometimes of expressions alone, sometimes of other things including expressions – at the Python Language Reference URL:https://docs.python.org/3/reference/expressions.html URL:https://docs.python.org/3/reference/simple_stmts.html. -- \ “If nature has made any one thing less susceptible than all | `\others of exclusive property, it is the action of the thinking | _o__) power called an idea” —Thomas Jefferson | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] difference between expressions and statements
Jared Nielsen nielsen.ja...@gmail.com Wrote in message: Hi Pythons, Could someone explain the difference between expressions and statements? I know that expressions are statements that produce a value. 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? Does a string count as a value? Is a variable assignment considered a value? If I print a variable is that considered production of a value? Can't answer till you specify Python version. The other answers all seem to assume version 3.x. In version 2, these are all statements, none are expressions. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor