What is the difference between an expression and a statement in Python?












268















In Python, what is the difference between expressions and statements?










share|improve this question





























    268















    In Python, what is the difference between expressions and statements?










    share|improve this question



























      268












      268








      268


      110






      In Python, what is the difference between expressions and statements?










      share|improve this question
















      In Python, what is the difference between expressions and statements?







      python expression






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 1 '16 at 22:24









      Chris Martin

      23.8k450109




      23.8k450109










      asked Jan 18 '11 at 19:19









      wassimanswassimans

      3,29193756




      3,29193756
























          12 Answers
          12






          active

          oldest

          votes


















          202














          Expressions only contain identifiers, literals and operators, where operators include arithmetic and boolean operators, the function call operator () the subscription operator and similar, and can be reduced to some kind of "value", which can be any Python object. Examples:



          3 + 5
          map(lambda x: x*x, range(10))
          [a.x for a in some_iterable]
          yield 7


          Statements (see 1, 2), on the other hand, are everything that can make up a line (or several lines) of Python code. Note that expressions are statements as well. Examples:



          # all the above expressions
          print 42
          if x: do_y()
          return
          a = 7





          share|improve this answer





















          • 8





            expressions are parts of statements

            – bismigalis
            Nov 25 '13 at 17:45






          • 45





            @bismigalis: Every valid Python expression can be used as a statement (called an "expression statement"). In this sense, expressions are statements.

            – Sven Marnach
            Nov 25 '13 at 18:05








          • 6





            yes you are right, I didnt consider this case.

            – bismigalis
            Feb 3 '14 at 19:54











          • Expressions can also include function calls (including calling classes for object instantiation). Technically these are "identifiers" exactly like names bound to values in an = statement ... even though the binding is through the 'def' or 'class' keywords. However, for this answer I would separately spell out function calls to make that clear.

            – Jim Dennis
            Feb 8 '15 at 23:26













          • @JimDennis: The list of examples I gave includes a function call (second example). I also included links to the full grammar, so I guess I covered this.

            – Sven Marnach
            Feb 9 '15 at 22:02



















          96














          Expression -- from my dictionary:




          expression: Mathematics a collection
          of symbols that jointly express a
          quantity : the expression for the
          circumference of a circle is 2πr.




          In gross general terms: Expressions produce at least one value.



          In Python, expressions are covered extensively in the Python Language Reference In general, expressions in Python are composed of a syntactically legal combination of Atoms, Primaries and Operators.



          Python expressions from Wikipedia



          Examples of expressions:



          Literals and syntactically correct combinations with Operators and built-in functions or the call of a user-written functions:



          >>> 23
          23
          >>> 23l
          23L
          >>> range(4)
          [0, 1, 2, 3]
          >>> 2L*bin(2)
          '0b100b10'
          >>> def func(a): # Statement, just part of the example...
          ... return a*a # Statement...
          ...
          >>> func(3)*4
          36
          >>> func(5) is func(a=5)
          True


          Statement from Wikipedia:




          In computer programming a statement
          can be thought of as the smallest
          standalone element of an imperative
          programming language. A program is
          formed by a sequence of one or more
          statements. A statement will have
          internal components (e.g.,
          expressions).




          Python statements from Wikipedia



          In gross general terms: Statements Do Something and are often composed of expressions (or other statements)



          The Python Language Reference covers Simple Statements and Compound Statements extensively.



          The distinction of "Statements do something" and "expressions produce a value" distinction can become blurry however:





          • List Comprehensions are considered "Expressions" but they have looping constructs and therfore also Do Something.

          • The if is usually a statement, such as if x<0: x=0 but you can also have a conditional expression like x=0 if x<0 else 1 that are expressions. In other languages, like C, this form is called an operator like this x=x<0?0:1;

          • You can write you own Expressions by writing a function. def func(a): return a*a is an expression when used but made up of statements when defined.

          • An expression that returns None is a procedure in Python: def proc(): pass Syntactically, you can use proc() as an expression, but that is probably a bug...

          • Python is a bit more strict than say C is on the differences between an Expression and Statement. In C, any expression is a legal statement. You can have func(x=2); Is that an Expression or Statement? (Answer: Expression used as a Statement with a side-effect.) The form func(x=2) is illegal in Python (or at least it has a different meaning func(a=3) sets the named argument a to 3)






          share|improve this answer

































            59














            Though this isn't related to Python:



            An expression evaluates to a value.
            A statement does something.



            >>> x = 1
            >>> y = x + 1 # an expression
            >>> print y # a statement (in 2.x)
            2





            share|improve this answer



















            • 3





              But note that in all language except the really really "pure" ones, expressions can "do something" (more formally: have a side effect) just as well.

              – user395760
              Jan 18 '11 at 19:32













            • @delnan: Can you give an example (curious)? I am not aware.

              – user225312
              Jan 18 '11 at 19:32






            • 3





              Likewise, somelist.append(123). Most function calls, really.

              – Thomas K
              Jan 18 '11 at 19:40






            • 12





              y = x + 1 is not an expression but a statement. Try eval("y = x + 1") and you'll have an error.

              – Arglanir
              Feb 4 '13 at 9:52






            • 3





              y = x +1 is an expression statement

              – eosimosu
              Oct 15 '16 at 6:52



















            8














            Statements represent an action or command e.g print statements, assignment statements.



            print 'hello', x = 1


            Expression is a combination of variables, operations and values that yields a result value.



            5 * 5 # yields 25


            Lastly, expression statements



            print 5*5





            share|improve this answer

































              3














              An expression is something that can be reduced to a value, for example "1+3" or "foo = 1+3".



              It's easy to check:



              print foo = 1+3


              If it doesn't work, it's a statement, if it does, it's an expression.



              Another statement could be:



              class Foo(Bar): pass


              as it cannot be reduced to a value.






              share|improve this answer





















              • 1





                As executing your first example would show, assignment is not an expression (not really, that is - a = b = expr is allowed, as a special case) in Python. In languages drawing more inspiration from C, it is.

                – user395760
                Jan 18 '11 at 19:26













              • class Foo(bar): is the beginning of a statement, not a complete statement.

                – Sven Marnach
                Jan 18 '11 at 19:28






              • 1





                foo = 1+3 is NOT an expression. It is a statement (an assignment to be precise). The part 1+3 is an expression though.

                – Pithikos
                Apr 17 '15 at 13:25






              • 1





                My formulation is very, very precise: "If it doesn't work, it's a statement, if it does, it's an expression.". Execute it, and you'll have your answer.

                – Flavius
                Jan 31 '17 at 7:32



















              3














              An expression is something, while a statement does something.

              An expression is a statement as well, but it must have a return.



              >>> 2 * 2          #expression
              >>> print(2 * 2)     #statement


              PS:The interpreter always prints out the values of all expressions.






              share|improve this answer


























              • in my opinion, a statement is an expression with a null value

                – Adalcar
                Oct 18 '17 at 9:35



















              3















              1. An expression is a statement that returns a value. So if it can appear on the right side of an assignment, or as a parameter to a method call, it is an expression.

              2. Some code can be both an expression or a statement, depending on the context. The language may have a means to differentiate between the two when they are ambiguous.






              share|improve this answer


























              • the question asks specifically about python, so although the explanation is extensive and great for JavaScript, it is off-topic for this question.

                – martin-martin
                Jul 16 '18 at 6:28











              • @martin-martin, I will keep my explanation but delete my examples, and then it won't be off topic per your downvote.

                – Steven Spungin
                Jul 18 '18 at 11:47











              • sweet! would be worth it to keep your previous around and post it in a relevant thread, it is a thorough explanation. :)

                – martin-martin
                Jul 18 '18 at 14:29



















              0














              A statement contains a keyword.



              An expression does not contain a keyword.



              print "hello" is statement, because print is a keyword.



              "hello" is an expression, but list compression is against this.



              The following is an expression statement, and it is true without list comprehension:



              (x*2 for x in range(10))





              share|improve this answer





















              • 4





                That strongly depends on your definition of a 'keyword'. x = 1 is a perfectly fine statement, but does not contain keywords.

                – Joost
                May 8 '14 at 20:56



















              0














              Expressions:




              • Expressions are formed by combining objects and operators.

              • An expression has a value, which has a type.

              • Syntax for a simple expression:<object><operator><object>


              2.0 + 3 is an expression which evaluates to 5.0 and has a type float associated with it.



              Statements



              Statements are composed of expression(s). It can span multiple lines.






              share|improve this answer































                0














                In simple words: a statement is made of one or more expressions, whereas an expression is made of one or more of Identifiers (names), Literals, and Operators.






                share|improve this answer































                  -1














                  Python calls expressions "expression statements", so the question is perhaps not fully formed.



                  A statement consists of pretty much anything you can do in Python: calculating a value, assigning a value, deleting a variable, printing a value, returning from a function, raising an exception, etc. The full list is here: http://docs.python.org/reference/simple_stmts.html#



                  An expression statement is limited to calling functions (e.g.,
                  math.cos(theta)"), operators ( e.g., "2+3"), etc. to produce a value.






                  share|improve this answer



















                  • 9





                    No, Python doesn't call expressions "expression statements". Python calls statements only consisting of a single expression "expression statements".

                    – Sven Marnach
                    Jan 18 '11 at 19:37











                  • ... and it's not alone doing so.

                    – user395760
                    Jan 18 '11 at 19:51



















                  -1














                  I think an expression contains operators + operands and the object that holds the result of the operation... e.g.



                  var sum = a + b;


                  but a statement is simply a line of a code (it may be an expression) or block of code... e.g.



                  fun printHello(name: String?): Unit {
                  if (name != null)
                  println("Hello ${name}")
                  else
                  println("Hi there!")
                  // `return Unit` or `return` is optional


                  }






                  share|improve this answer























                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4728073%2fwhat-is-the-difference-between-an-expression-and-a-statement-in-python%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    12 Answers
                    12






                    active

                    oldest

                    votes








                    12 Answers
                    12






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    202














                    Expressions only contain identifiers, literals and operators, where operators include arithmetic and boolean operators, the function call operator () the subscription operator and similar, and can be reduced to some kind of "value", which can be any Python object. Examples:



                    3 + 5
                    map(lambda x: x*x, range(10))
                    [a.x for a in some_iterable]
                    yield 7


                    Statements (see 1, 2), on the other hand, are everything that can make up a line (or several lines) of Python code. Note that expressions are statements as well. Examples:



                    # all the above expressions
                    print 42
                    if x: do_y()
                    return
                    a = 7





                    share|improve this answer





















                    • 8





                      expressions are parts of statements

                      – bismigalis
                      Nov 25 '13 at 17:45






                    • 45





                      @bismigalis: Every valid Python expression can be used as a statement (called an "expression statement"). In this sense, expressions are statements.

                      – Sven Marnach
                      Nov 25 '13 at 18:05








                    • 6





                      yes you are right, I didnt consider this case.

                      – bismigalis
                      Feb 3 '14 at 19:54











                    • Expressions can also include function calls (including calling classes for object instantiation). Technically these are "identifiers" exactly like names bound to values in an = statement ... even though the binding is through the 'def' or 'class' keywords. However, for this answer I would separately spell out function calls to make that clear.

                      – Jim Dennis
                      Feb 8 '15 at 23:26













                    • @JimDennis: The list of examples I gave includes a function call (second example). I also included links to the full grammar, so I guess I covered this.

                      – Sven Marnach
                      Feb 9 '15 at 22:02
















                    202














                    Expressions only contain identifiers, literals and operators, where operators include arithmetic and boolean operators, the function call operator () the subscription operator and similar, and can be reduced to some kind of "value", which can be any Python object. Examples:



                    3 + 5
                    map(lambda x: x*x, range(10))
                    [a.x for a in some_iterable]
                    yield 7


                    Statements (see 1, 2), on the other hand, are everything that can make up a line (or several lines) of Python code. Note that expressions are statements as well. Examples:



                    # all the above expressions
                    print 42
                    if x: do_y()
                    return
                    a = 7





                    share|improve this answer





















                    • 8





                      expressions are parts of statements

                      – bismigalis
                      Nov 25 '13 at 17:45






                    • 45





                      @bismigalis: Every valid Python expression can be used as a statement (called an "expression statement"). In this sense, expressions are statements.

                      – Sven Marnach
                      Nov 25 '13 at 18:05








                    • 6





                      yes you are right, I didnt consider this case.

                      – bismigalis
                      Feb 3 '14 at 19:54











                    • Expressions can also include function calls (including calling classes for object instantiation). Technically these are "identifiers" exactly like names bound to values in an = statement ... even though the binding is through the 'def' or 'class' keywords. However, for this answer I would separately spell out function calls to make that clear.

                      – Jim Dennis
                      Feb 8 '15 at 23:26













                    • @JimDennis: The list of examples I gave includes a function call (second example). I also included links to the full grammar, so I guess I covered this.

                      – Sven Marnach
                      Feb 9 '15 at 22:02














                    202












                    202








                    202







                    Expressions only contain identifiers, literals and operators, where operators include arithmetic and boolean operators, the function call operator () the subscription operator and similar, and can be reduced to some kind of "value", which can be any Python object. Examples:



                    3 + 5
                    map(lambda x: x*x, range(10))
                    [a.x for a in some_iterable]
                    yield 7


                    Statements (see 1, 2), on the other hand, are everything that can make up a line (or several lines) of Python code. Note that expressions are statements as well. Examples:



                    # all the above expressions
                    print 42
                    if x: do_y()
                    return
                    a = 7





                    share|improve this answer















                    Expressions only contain identifiers, literals and operators, where operators include arithmetic and boolean operators, the function call operator () the subscription operator and similar, and can be reduced to some kind of "value", which can be any Python object. Examples:



                    3 + 5
                    map(lambda x: x*x, range(10))
                    [a.x for a in some_iterable]
                    yield 7


                    Statements (see 1, 2), on the other hand, are everything that can make up a line (or several lines) of Python code. Note that expressions are statements as well. Examples:



                    # all the above expressions
                    print 42
                    if x: do_y()
                    return
                    a = 7






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 18 '15 at 23:42









                    songololo

                    2,04122134




                    2,04122134










                    answered Jan 18 '11 at 19:27









                    Sven MarnachSven Marnach

                    352k78748695




                    352k78748695








                    • 8





                      expressions are parts of statements

                      – bismigalis
                      Nov 25 '13 at 17:45






                    • 45





                      @bismigalis: Every valid Python expression can be used as a statement (called an "expression statement"). In this sense, expressions are statements.

                      – Sven Marnach
                      Nov 25 '13 at 18:05








                    • 6





                      yes you are right, I didnt consider this case.

                      – bismigalis
                      Feb 3 '14 at 19:54











                    • Expressions can also include function calls (including calling classes for object instantiation). Technically these are "identifiers" exactly like names bound to values in an = statement ... even though the binding is through the 'def' or 'class' keywords. However, for this answer I would separately spell out function calls to make that clear.

                      – Jim Dennis
                      Feb 8 '15 at 23:26













                    • @JimDennis: The list of examples I gave includes a function call (second example). I also included links to the full grammar, so I guess I covered this.

                      – Sven Marnach
                      Feb 9 '15 at 22:02














                    • 8





                      expressions are parts of statements

                      – bismigalis
                      Nov 25 '13 at 17:45






                    • 45





                      @bismigalis: Every valid Python expression can be used as a statement (called an "expression statement"). In this sense, expressions are statements.

                      – Sven Marnach
                      Nov 25 '13 at 18:05








                    • 6





                      yes you are right, I didnt consider this case.

                      – bismigalis
                      Feb 3 '14 at 19:54











                    • Expressions can also include function calls (including calling classes for object instantiation). Technically these are "identifiers" exactly like names bound to values in an = statement ... even though the binding is through the 'def' or 'class' keywords. However, for this answer I would separately spell out function calls to make that clear.

                      – Jim Dennis
                      Feb 8 '15 at 23:26













                    • @JimDennis: The list of examples I gave includes a function call (second example). I also included links to the full grammar, so I guess I covered this.

                      – Sven Marnach
                      Feb 9 '15 at 22:02








                    8




                    8





                    expressions are parts of statements

                    – bismigalis
                    Nov 25 '13 at 17:45





                    expressions are parts of statements

                    – bismigalis
                    Nov 25 '13 at 17:45




                    45




                    45





                    @bismigalis: Every valid Python expression can be used as a statement (called an "expression statement"). In this sense, expressions are statements.

                    – Sven Marnach
                    Nov 25 '13 at 18:05







                    @bismigalis: Every valid Python expression can be used as a statement (called an "expression statement"). In this sense, expressions are statements.

                    – Sven Marnach
                    Nov 25 '13 at 18:05






                    6




                    6





                    yes you are right, I didnt consider this case.

                    – bismigalis
                    Feb 3 '14 at 19:54





                    yes you are right, I didnt consider this case.

                    – bismigalis
                    Feb 3 '14 at 19:54













                    Expressions can also include function calls (including calling classes for object instantiation). Technically these are "identifiers" exactly like names bound to values in an = statement ... even though the binding is through the 'def' or 'class' keywords. However, for this answer I would separately spell out function calls to make that clear.

                    – Jim Dennis
                    Feb 8 '15 at 23:26







                    Expressions can also include function calls (including calling classes for object instantiation). Technically these are "identifiers" exactly like names bound to values in an = statement ... even though the binding is through the 'def' or 'class' keywords. However, for this answer I would separately spell out function calls to make that clear.

                    – Jim Dennis
                    Feb 8 '15 at 23:26















                    @JimDennis: The list of examples I gave includes a function call (second example). I also included links to the full grammar, so I guess I covered this.

                    – Sven Marnach
                    Feb 9 '15 at 22:02





                    @JimDennis: The list of examples I gave includes a function call (second example). I also included links to the full grammar, so I guess I covered this.

                    – Sven Marnach
                    Feb 9 '15 at 22:02













                    96














                    Expression -- from my dictionary:




                    expression: Mathematics a collection
                    of symbols that jointly express a
                    quantity : the expression for the
                    circumference of a circle is 2πr.




                    In gross general terms: Expressions produce at least one value.



                    In Python, expressions are covered extensively in the Python Language Reference In general, expressions in Python are composed of a syntactically legal combination of Atoms, Primaries and Operators.



                    Python expressions from Wikipedia



                    Examples of expressions:



                    Literals and syntactically correct combinations with Operators and built-in functions or the call of a user-written functions:



                    >>> 23
                    23
                    >>> 23l
                    23L
                    >>> range(4)
                    [0, 1, 2, 3]
                    >>> 2L*bin(2)
                    '0b100b10'
                    >>> def func(a): # Statement, just part of the example...
                    ... return a*a # Statement...
                    ...
                    >>> func(3)*4
                    36
                    >>> func(5) is func(a=5)
                    True


                    Statement from Wikipedia:




                    In computer programming a statement
                    can be thought of as the smallest
                    standalone element of an imperative
                    programming language. A program is
                    formed by a sequence of one or more
                    statements. A statement will have
                    internal components (e.g.,
                    expressions).




                    Python statements from Wikipedia



                    In gross general terms: Statements Do Something and are often composed of expressions (or other statements)



                    The Python Language Reference covers Simple Statements and Compound Statements extensively.



                    The distinction of "Statements do something" and "expressions produce a value" distinction can become blurry however:





                    • List Comprehensions are considered "Expressions" but they have looping constructs and therfore also Do Something.

                    • The if is usually a statement, such as if x<0: x=0 but you can also have a conditional expression like x=0 if x<0 else 1 that are expressions. In other languages, like C, this form is called an operator like this x=x<0?0:1;

                    • You can write you own Expressions by writing a function. def func(a): return a*a is an expression when used but made up of statements when defined.

                    • An expression that returns None is a procedure in Python: def proc(): pass Syntactically, you can use proc() as an expression, but that is probably a bug...

                    • Python is a bit more strict than say C is on the differences between an Expression and Statement. In C, any expression is a legal statement. You can have func(x=2); Is that an Expression or Statement? (Answer: Expression used as a Statement with a side-effect.) The form func(x=2) is illegal in Python (or at least it has a different meaning func(a=3) sets the named argument a to 3)






                    share|improve this answer






























                      96














                      Expression -- from my dictionary:




                      expression: Mathematics a collection
                      of symbols that jointly express a
                      quantity : the expression for the
                      circumference of a circle is 2πr.




                      In gross general terms: Expressions produce at least one value.



                      In Python, expressions are covered extensively in the Python Language Reference In general, expressions in Python are composed of a syntactically legal combination of Atoms, Primaries and Operators.



                      Python expressions from Wikipedia



                      Examples of expressions:



                      Literals and syntactically correct combinations with Operators and built-in functions or the call of a user-written functions:



                      >>> 23
                      23
                      >>> 23l
                      23L
                      >>> range(4)
                      [0, 1, 2, 3]
                      >>> 2L*bin(2)
                      '0b100b10'
                      >>> def func(a): # Statement, just part of the example...
                      ... return a*a # Statement...
                      ...
                      >>> func(3)*4
                      36
                      >>> func(5) is func(a=5)
                      True


                      Statement from Wikipedia:




                      In computer programming a statement
                      can be thought of as the smallest
                      standalone element of an imperative
                      programming language. A program is
                      formed by a sequence of one or more
                      statements. A statement will have
                      internal components (e.g.,
                      expressions).




                      Python statements from Wikipedia



                      In gross general terms: Statements Do Something and are often composed of expressions (or other statements)



                      The Python Language Reference covers Simple Statements and Compound Statements extensively.



                      The distinction of "Statements do something" and "expressions produce a value" distinction can become blurry however:





                      • List Comprehensions are considered "Expressions" but they have looping constructs and therfore also Do Something.

                      • The if is usually a statement, such as if x<0: x=0 but you can also have a conditional expression like x=0 if x<0 else 1 that are expressions. In other languages, like C, this form is called an operator like this x=x<0?0:1;

                      • You can write you own Expressions by writing a function. def func(a): return a*a is an expression when used but made up of statements when defined.

                      • An expression that returns None is a procedure in Python: def proc(): pass Syntactically, you can use proc() as an expression, but that is probably a bug...

                      • Python is a bit more strict than say C is on the differences between an Expression and Statement. In C, any expression is a legal statement. You can have func(x=2); Is that an Expression or Statement? (Answer: Expression used as a Statement with a side-effect.) The form func(x=2) is illegal in Python (or at least it has a different meaning func(a=3) sets the named argument a to 3)






                      share|improve this answer




























                        96












                        96








                        96







                        Expression -- from my dictionary:




                        expression: Mathematics a collection
                        of symbols that jointly express a
                        quantity : the expression for the
                        circumference of a circle is 2πr.




                        In gross general terms: Expressions produce at least one value.



                        In Python, expressions are covered extensively in the Python Language Reference In general, expressions in Python are composed of a syntactically legal combination of Atoms, Primaries and Operators.



                        Python expressions from Wikipedia



                        Examples of expressions:



                        Literals and syntactically correct combinations with Operators and built-in functions or the call of a user-written functions:



                        >>> 23
                        23
                        >>> 23l
                        23L
                        >>> range(4)
                        [0, 1, 2, 3]
                        >>> 2L*bin(2)
                        '0b100b10'
                        >>> def func(a): # Statement, just part of the example...
                        ... return a*a # Statement...
                        ...
                        >>> func(3)*4
                        36
                        >>> func(5) is func(a=5)
                        True


                        Statement from Wikipedia:




                        In computer programming a statement
                        can be thought of as the smallest
                        standalone element of an imperative
                        programming language. A program is
                        formed by a sequence of one or more
                        statements. A statement will have
                        internal components (e.g.,
                        expressions).




                        Python statements from Wikipedia



                        In gross general terms: Statements Do Something and are often composed of expressions (or other statements)



                        The Python Language Reference covers Simple Statements and Compound Statements extensively.



                        The distinction of "Statements do something" and "expressions produce a value" distinction can become blurry however:





                        • List Comprehensions are considered "Expressions" but they have looping constructs and therfore also Do Something.

                        • The if is usually a statement, such as if x<0: x=0 but you can also have a conditional expression like x=0 if x<0 else 1 that are expressions. In other languages, like C, this form is called an operator like this x=x<0?0:1;

                        • You can write you own Expressions by writing a function. def func(a): return a*a is an expression when used but made up of statements when defined.

                        • An expression that returns None is a procedure in Python: def proc(): pass Syntactically, you can use proc() as an expression, but that is probably a bug...

                        • Python is a bit more strict than say C is on the differences between an Expression and Statement. In C, any expression is a legal statement. You can have func(x=2); Is that an Expression or Statement? (Answer: Expression used as a Statement with a side-effect.) The form func(x=2) is illegal in Python (or at least it has a different meaning func(a=3) sets the named argument a to 3)






                        share|improve this answer















                        Expression -- from my dictionary:




                        expression: Mathematics a collection
                        of symbols that jointly express a
                        quantity : the expression for the
                        circumference of a circle is 2πr.




                        In gross general terms: Expressions produce at least one value.



                        In Python, expressions are covered extensively in the Python Language Reference In general, expressions in Python are composed of a syntactically legal combination of Atoms, Primaries and Operators.



                        Python expressions from Wikipedia



                        Examples of expressions:



                        Literals and syntactically correct combinations with Operators and built-in functions or the call of a user-written functions:



                        >>> 23
                        23
                        >>> 23l
                        23L
                        >>> range(4)
                        [0, 1, 2, 3]
                        >>> 2L*bin(2)
                        '0b100b10'
                        >>> def func(a): # Statement, just part of the example...
                        ... return a*a # Statement...
                        ...
                        >>> func(3)*4
                        36
                        >>> func(5) is func(a=5)
                        True


                        Statement from Wikipedia:




                        In computer programming a statement
                        can be thought of as the smallest
                        standalone element of an imperative
                        programming language. A program is
                        formed by a sequence of one or more
                        statements. A statement will have
                        internal components (e.g.,
                        expressions).




                        Python statements from Wikipedia



                        In gross general terms: Statements Do Something and are often composed of expressions (or other statements)



                        The Python Language Reference covers Simple Statements and Compound Statements extensively.



                        The distinction of "Statements do something" and "expressions produce a value" distinction can become blurry however:





                        • List Comprehensions are considered "Expressions" but they have looping constructs and therfore also Do Something.

                        • The if is usually a statement, such as if x<0: x=0 but you can also have a conditional expression like x=0 if x<0 else 1 that are expressions. In other languages, like C, this form is called an operator like this x=x<0?0:1;

                        • You can write you own Expressions by writing a function. def func(a): return a*a is an expression when used but made up of statements when defined.

                        • An expression that returns None is a procedure in Python: def proc(): pass Syntactically, you can use proc() as an expression, but that is probably a bug...

                        • Python is a bit more strict than say C is on the differences between an Expression and Statement. In C, any expression is a legal statement. You can have func(x=2); Is that an Expression or Statement? (Answer: Expression used as a Statement with a side-effect.) The form func(x=2) is illegal in Python (or at least it has a different meaning func(a=3) sets the named argument a to 3)







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited May 23 '17 at 12:26









                        Community

                        11




                        11










                        answered Jan 19 '11 at 0:25









                        dawgdawg

                        59.2k1283152




                        59.2k1283152























                            59














                            Though this isn't related to Python:



                            An expression evaluates to a value.
                            A statement does something.



                            >>> x = 1
                            >>> y = x + 1 # an expression
                            >>> print y # a statement (in 2.x)
                            2





                            share|improve this answer



















                            • 3





                              But note that in all language except the really really "pure" ones, expressions can "do something" (more formally: have a side effect) just as well.

                              – user395760
                              Jan 18 '11 at 19:32













                            • @delnan: Can you give an example (curious)? I am not aware.

                              – user225312
                              Jan 18 '11 at 19:32






                            • 3





                              Likewise, somelist.append(123). Most function calls, really.

                              – Thomas K
                              Jan 18 '11 at 19:40






                            • 12





                              y = x + 1 is not an expression but a statement. Try eval("y = x + 1") and you'll have an error.

                              – Arglanir
                              Feb 4 '13 at 9:52






                            • 3





                              y = x +1 is an expression statement

                              – eosimosu
                              Oct 15 '16 at 6:52
















                            59














                            Though this isn't related to Python:



                            An expression evaluates to a value.
                            A statement does something.



                            >>> x = 1
                            >>> y = x + 1 # an expression
                            >>> print y # a statement (in 2.x)
                            2





                            share|improve this answer



















                            • 3





                              But note that in all language except the really really "pure" ones, expressions can "do something" (more formally: have a side effect) just as well.

                              – user395760
                              Jan 18 '11 at 19:32













                            • @delnan: Can you give an example (curious)? I am not aware.

                              – user225312
                              Jan 18 '11 at 19:32






                            • 3





                              Likewise, somelist.append(123). Most function calls, really.

                              – Thomas K
                              Jan 18 '11 at 19:40






                            • 12





                              y = x + 1 is not an expression but a statement. Try eval("y = x + 1") and you'll have an error.

                              – Arglanir
                              Feb 4 '13 at 9:52






                            • 3





                              y = x +1 is an expression statement

                              – eosimosu
                              Oct 15 '16 at 6:52














                            59












                            59








                            59







                            Though this isn't related to Python:



                            An expression evaluates to a value.
                            A statement does something.



                            >>> x = 1
                            >>> y = x + 1 # an expression
                            >>> print y # a statement (in 2.x)
                            2





                            share|improve this answer













                            Though this isn't related to Python:



                            An expression evaluates to a value.
                            A statement does something.



                            >>> x = 1
                            >>> y = x + 1 # an expression
                            >>> print y # a statement (in 2.x)
                            2






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 18 '11 at 19:29









                            user225312user225312

                            64.3k58144171




                            64.3k58144171








                            • 3





                              But note that in all language except the really really "pure" ones, expressions can "do something" (more formally: have a side effect) just as well.

                              – user395760
                              Jan 18 '11 at 19:32













                            • @delnan: Can you give an example (curious)? I am not aware.

                              – user225312
                              Jan 18 '11 at 19:32






                            • 3





                              Likewise, somelist.append(123). Most function calls, really.

                              – Thomas K
                              Jan 18 '11 at 19:40






                            • 12





                              y = x + 1 is not an expression but a statement. Try eval("y = x + 1") and you'll have an error.

                              – Arglanir
                              Feb 4 '13 at 9:52






                            • 3





                              y = x +1 is an expression statement

                              – eosimosu
                              Oct 15 '16 at 6:52














                            • 3





                              But note that in all language except the really really "pure" ones, expressions can "do something" (more formally: have a side effect) just as well.

                              – user395760
                              Jan 18 '11 at 19:32













                            • @delnan: Can you give an example (curious)? I am not aware.

                              – user225312
                              Jan 18 '11 at 19:32






                            • 3





                              Likewise, somelist.append(123). Most function calls, really.

                              – Thomas K
                              Jan 18 '11 at 19:40






                            • 12





                              y = x + 1 is not an expression but a statement. Try eval("y = x + 1") and you'll have an error.

                              – Arglanir
                              Feb 4 '13 at 9:52






                            • 3





                              y = x +1 is an expression statement

                              – eosimosu
                              Oct 15 '16 at 6:52








                            3




                            3





                            But note that in all language except the really really "pure" ones, expressions can "do something" (more formally: have a side effect) just as well.

                            – user395760
                            Jan 18 '11 at 19:32







                            But note that in all language except the really really "pure" ones, expressions can "do something" (more formally: have a side effect) just as well.

                            – user395760
                            Jan 18 '11 at 19:32















                            @delnan: Can you give an example (curious)? I am not aware.

                            – user225312
                            Jan 18 '11 at 19:32





                            @delnan: Can you give an example (curious)? I am not aware.

                            – user225312
                            Jan 18 '11 at 19:32




                            3




                            3





                            Likewise, somelist.append(123). Most function calls, really.

                            – Thomas K
                            Jan 18 '11 at 19:40





                            Likewise, somelist.append(123). Most function calls, really.

                            – Thomas K
                            Jan 18 '11 at 19:40




                            12




                            12





                            y = x + 1 is not an expression but a statement. Try eval("y = x + 1") and you'll have an error.

                            – Arglanir
                            Feb 4 '13 at 9:52





                            y = x + 1 is not an expression but a statement. Try eval("y = x + 1") and you'll have an error.

                            – Arglanir
                            Feb 4 '13 at 9:52




                            3




                            3





                            y = x +1 is an expression statement

                            – eosimosu
                            Oct 15 '16 at 6:52





                            y = x +1 is an expression statement

                            – eosimosu
                            Oct 15 '16 at 6:52











                            8














                            Statements represent an action or command e.g print statements, assignment statements.



                            print 'hello', x = 1


                            Expression is a combination of variables, operations and values that yields a result value.



                            5 * 5 # yields 25


                            Lastly, expression statements



                            print 5*5





                            share|improve this answer






























                              8














                              Statements represent an action or command e.g print statements, assignment statements.



                              print 'hello', x = 1


                              Expression is a combination of variables, operations and values that yields a result value.



                              5 * 5 # yields 25


                              Lastly, expression statements



                              print 5*5





                              share|improve this answer




























                                8












                                8








                                8







                                Statements represent an action or command e.g print statements, assignment statements.



                                print 'hello', x = 1


                                Expression is a combination of variables, operations and values that yields a result value.



                                5 * 5 # yields 25


                                Lastly, expression statements



                                print 5*5





                                share|improve this answer















                                Statements represent an action or command e.g print statements, assignment statements.



                                print 'hello', x = 1


                                Expression is a combination of variables, operations and values that yields a result value.



                                5 * 5 # yields 25


                                Lastly, expression statements



                                print 5*5






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Jan 10 '17 at 15:27

























                                answered Apr 10 '15 at 18:59









                                eosimosueosimosu

                                1,2351624




                                1,2351624























                                    3














                                    An expression is something that can be reduced to a value, for example "1+3" or "foo = 1+3".



                                    It's easy to check:



                                    print foo = 1+3


                                    If it doesn't work, it's a statement, if it does, it's an expression.



                                    Another statement could be:



                                    class Foo(Bar): pass


                                    as it cannot be reduced to a value.






                                    share|improve this answer





















                                    • 1





                                      As executing your first example would show, assignment is not an expression (not really, that is - a = b = expr is allowed, as a special case) in Python. In languages drawing more inspiration from C, it is.

                                      – user395760
                                      Jan 18 '11 at 19:26













                                    • class Foo(bar): is the beginning of a statement, not a complete statement.

                                      – Sven Marnach
                                      Jan 18 '11 at 19:28






                                    • 1





                                      foo = 1+3 is NOT an expression. It is a statement (an assignment to be precise). The part 1+3 is an expression though.

                                      – Pithikos
                                      Apr 17 '15 at 13:25






                                    • 1





                                      My formulation is very, very precise: "If it doesn't work, it's a statement, if it does, it's an expression.". Execute it, and you'll have your answer.

                                      – Flavius
                                      Jan 31 '17 at 7:32
















                                    3














                                    An expression is something that can be reduced to a value, for example "1+3" or "foo = 1+3".



                                    It's easy to check:



                                    print foo = 1+3


                                    If it doesn't work, it's a statement, if it does, it's an expression.



                                    Another statement could be:



                                    class Foo(Bar): pass


                                    as it cannot be reduced to a value.






                                    share|improve this answer





















                                    • 1





                                      As executing your first example would show, assignment is not an expression (not really, that is - a = b = expr is allowed, as a special case) in Python. In languages drawing more inspiration from C, it is.

                                      – user395760
                                      Jan 18 '11 at 19:26













                                    • class Foo(bar): is the beginning of a statement, not a complete statement.

                                      – Sven Marnach
                                      Jan 18 '11 at 19:28






                                    • 1





                                      foo = 1+3 is NOT an expression. It is a statement (an assignment to be precise). The part 1+3 is an expression though.

                                      – Pithikos
                                      Apr 17 '15 at 13:25






                                    • 1





                                      My formulation is very, very precise: "If it doesn't work, it's a statement, if it does, it's an expression.". Execute it, and you'll have your answer.

                                      – Flavius
                                      Jan 31 '17 at 7:32














                                    3












                                    3








                                    3







                                    An expression is something that can be reduced to a value, for example "1+3" or "foo = 1+3".



                                    It's easy to check:



                                    print foo = 1+3


                                    If it doesn't work, it's a statement, if it does, it's an expression.



                                    Another statement could be:



                                    class Foo(Bar): pass


                                    as it cannot be reduced to a value.






                                    share|improve this answer















                                    An expression is something that can be reduced to a value, for example "1+3" or "foo = 1+3".



                                    It's easy to check:



                                    print foo = 1+3


                                    If it doesn't work, it's a statement, if it does, it's an expression.



                                    Another statement could be:



                                    class Foo(Bar): pass


                                    as it cannot be reduced to a value.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Jan 31 '17 at 7:31

























                                    answered Jan 18 '11 at 19:24









                                    FlaviusFlavius

                                    7,8451065110




                                    7,8451065110








                                    • 1





                                      As executing your first example would show, assignment is not an expression (not really, that is - a = b = expr is allowed, as a special case) in Python. In languages drawing more inspiration from C, it is.

                                      – user395760
                                      Jan 18 '11 at 19:26













                                    • class Foo(bar): is the beginning of a statement, not a complete statement.

                                      – Sven Marnach
                                      Jan 18 '11 at 19:28






                                    • 1





                                      foo = 1+3 is NOT an expression. It is a statement (an assignment to be precise). The part 1+3 is an expression though.

                                      – Pithikos
                                      Apr 17 '15 at 13:25






                                    • 1





                                      My formulation is very, very precise: "If it doesn't work, it's a statement, if it does, it's an expression.". Execute it, and you'll have your answer.

                                      – Flavius
                                      Jan 31 '17 at 7:32














                                    • 1





                                      As executing your first example would show, assignment is not an expression (not really, that is - a = b = expr is allowed, as a special case) in Python. In languages drawing more inspiration from C, it is.

                                      – user395760
                                      Jan 18 '11 at 19:26













                                    • class Foo(bar): is the beginning of a statement, not a complete statement.

                                      – Sven Marnach
                                      Jan 18 '11 at 19:28






                                    • 1





                                      foo = 1+3 is NOT an expression. It is a statement (an assignment to be precise). The part 1+3 is an expression though.

                                      – Pithikos
                                      Apr 17 '15 at 13:25






                                    • 1





                                      My formulation is very, very precise: "If it doesn't work, it's a statement, if it does, it's an expression.". Execute it, and you'll have your answer.

                                      – Flavius
                                      Jan 31 '17 at 7:32








                                    1




                                    1





                                    As executing your first example would show, assignment is not an expression (not really, that is - a = b = expr is allowed, as a special case) in Python. In languages drawing more inspiration from C, it is.

                                    – user395760
                                    Jan 18 '11 at 19:26







                                    As executing your first example would show, assignment is not an expression (not really, that is - a = b = expr is allowed, as a special case) in Python. In languages drawing more inspiration from C, it is.

                                    – user395760
                                    Jan 18 '11 at 19:26















                                    class Foo(bar): is the beginning of a statement, not a complete statement.

                                    – Sven Marnach
                                    Jan 18 '11 at 19:28





                                    class Foo(bar): is the beginning of a statement, not a complete statement.

                                    – Sven Marnach
                                    Jan 18 '11 at 19:28




                                    1




                                    1





                                    foo = 1+3 is NOT an expression. It is a statement (an assignment to be precise). The part 1+3 is an expression though.

                                    – Pithikos
                                    Apr 17 '15 at 13:25





                                    foo = 1+3 is NOT an expression. It is a statement (an assignment to be precise). The part 1+3 is an expression though.

                                    – Pithikos
                                    Apr 17 '15 at 13:25




                                    1




                                    1





                                    My formulation is very, very precise: "If it doesn't work, it's a statement, if it does, it's an expression.". Execute it, and you'll have your answer.

                                    – Flavius
                                    Jan 31 '17 at 7:32





                                    My formulation is very, very precise: "If it doesn't work, it's a statement, if it does, it's an expression.". Execute it, and you'll have your answer.

                                    – Flavius
                                    Jan 31 '17 at 7:32











                                    3














                                    An expression is something, while a statement does something.

                                    An expression is a statement as well, but it must have a return.



                                    >>> 2 * 2          #expression
                                    >>> print(2 * 2)     #statement


                                    PS:The interpreter always prints out the values of all expressions.






                                    share|improve this answer


























                                    • in my opinion, a statement is an expression with a null value

                                      – Adalcar
                                      Oct 18 '17 at 9:35
















                                    3














                                    An expression is something, while a statement does something.

                                    An expression is a statement as well, but it must have a return.



                                    >>> 2 * 2          #expression
                                    >>> print(2 * 2)     #statement


                                    PS:The interpreter always prints out the values of all expressions.






                                    share|improve this answer


























                                    • in my opinion, a statement is an expression with a null value

                                      – Adalcar
                                      Oct 18 '17 at 9:35














                                    3












                                    3








                                    3







                                    An expression is something, while a statement does something.

                                    An expression is a statement as well, but it must have a return.



                                    >>> 2 * 2          #expression
                                    >>> print(2 * 2)     #statement


                                    PS:The interpreter always prints out the values of all expressions.






                                    share|improve this answer















                                    An expression is something, while a statement does something.

                                    An expression is a statement as well, but it must have a return.



                                    >>> 2 * 2          #expression
                                    >>> print(2 * 2)     #statement


                                    PS:The interpreter always prints out the values of all expressions.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Oct 18 '17 at 9:56









                                    Adalcar

                                    1,101523




                                    1,101523










                                    answered Oct 18 '17 at 9:29









                                    donald jiangdonald jiang

                                    313




                                    313













                                    • in my opinion, a statement is an expression with a null value

                                      – Adalcar
                                      Oct 18 '17 at 9:35



















                                    • in my opinion, a statement is an expression with a null value

                                      – Adalcar
                                      Oct 18 '17 at 9:35

















                                    in my opinion, a statement is an expression with a null value

                                    – Adalcar
                                    Oct 18 '17 at 9:35





                                    in my opinion, a statement is an expression with a null value

                                    – Adalcar
                                    Oct 18 '17 at 9:35











                                    3















                                    1. An expression is a statement that returns a value. So if it can appear on the right side of an assignment, or as a parameter to a method call, it is an expression.

                                    2. Some code can be both an expression or a statement, depending on the context. The language may have a means to differentiate between the two when they are ambiguous.






                                    share|improve this answer


























                                    • the question asks specifically about python, so although the explanation is extensive and great for JavaScript, it is off-topic for this question.

                                      – martin-martin
                                      Jul 16 '18 at 6:28











                                    • @martin-martin, I will keep my explanation but delete my examples, and then it won't be off topic per your downvote.

                                      – Steven Spungin
                                      Jul 18 '18 at 11:47











                                    • sweet! would be worth it to keep your previous around and post it in a relevant thread, it is a thorough explanation. :)

                                      – martin-martin
                                      Jul 18 '18 at 14:29
















                                    3















                                    1. An expression is a statement that returns a value. So if it can appear on the right side of an assignment, or as a parameter to a method call, it is an expression.

                                    2. Some code can be both an expression or a statement, depending on the context. The language may have a means to differentiate between the two when they are ambiguous.






                                    share|improve this answer


























                                    • the question asks specifically about python, so although the explanation is extensive and great for JavaScript, it is off-topic for this question.

                                      – martin-martin
                                      Jul 16 '18 at 6:28











                                    • @martin-martin, I will keep my explanation but delete my examples, and then it won't be off topic per your downvote.

                                      – Steven Spungin
                                      Jul 18 '18 at 11:47











                                    • sweet! would be worth it to keep your previous around and post it in a relevant thread, it is a thorough explanation. :)

                                      – martin-martin
                                      Jul 18 '18 at 14:29














                                    3












                                    3








                                    3








                                    1. An expression is a statement that returns a value. So if it can appear on the right side of an assignment, or as a parameter to a method call, it is an expression.

                                    2. Some code can be both an expression or a statement, depending on the context. The language may have a means to differentiate between the two when they are ambiguous.






                                    share|improve this answer
















                                    1. An expression is a statement that returns a value. So if it can appear on the right side of an assignment, or as a parameter to a method call, it is an expression.

                                    2. Some code can be both an expression or a statement, depending on the context. The language may have a means to differentiate between the two when they are ambiguous.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Jul 18 '18 at 11:48

























                                    answered Jan 3 '18 at 13:32









                                    Steven SpunginSteven Spungin

                                    7,18232432




                                    7,18232432













                                    • the question asks specifically about python, so although the explanation is extensive and great for JavaScript, it is off-topic for this question.

                                      – martin-martin
                                      Jul 16 '18 at 6:28











                                    • @martin-martin, I will keep my explanation but delete my examples, and then it won't be off topic per your downvote.

                                      – Steven Spungin
                                      Jul 18 '18 at 11:47











                                    • sweet! would be worth it to keep your previous around and post it in a relevant thread, it is a thorough explanation. :)

                                      – martin-martin
                                      Jul 18 '18 at 14:29



















                                    • the question asks specifically about python, so although the explanation is extensive and great for JavaScript, it is off-topic for this question.

                                      – martin-martin
                                      Jul 16 '18 at 6:28











                                    • @martin-martin, I will keep my explanation but delete my examples, and then it won't be off topic per your downvote.

                                      – Steven Spungin
                                      Jul 18 '18 at 11:47











                                    • sweet! would be worth it to keep your previous around and post it in a relevant thread, it is a thorough explanation. :)

                                      – martin-martin
                                      Jul 18 '18 at 14:29

















                                    the question asks specifically about python, so although the explanation is extensive and great for JavaScript, it is off-topic for this question.

                                    – martin-martin
                                    Jul 16 '18 at 6:28





                                    the question asks specifically about python, so although the explanation is extensive and great for JavaScript, it is off-topic for this question.

                                    – martin-martin
                                    Jul 16 '18 at 6:28













                                    @martin-martin, I will keep my explanation but delete my examples, and then it won't be off topic per your downvote.

                                    – Steven Spungin
                                    Jul 18 '18 at 11:47





                                    @martin-martin, I will keep my explanation but delete my examples, and then it won't be off topic per your downvote.

                                    – Steven Spungin
                                    Jul 18 '18 at 11:47













                                    sweet! would be worth it to keep your previous around and post it in a relevant thread, it is a thorough explanation. :)

                                    – martin-martin
                                    Jul 18 '18 at 14:29





                                    sweet! would be worth it to keep your previous around and post it in a relevant thread, it is a thorough explanation. :)

                                    – martin-martin
                                    Jul 18 '18 at 14:29











                                    0














                                    A statement contains a keyword.



                                    An expression does not contain a keyword.



                                    print "hello" is statement, because print is a keyword.



                                    "hello" is an expression, but list compression is against this.



                                    The following is an expression statement, and it is true without list comprehension:



                                    (x*2 for x in range(10))





                                    share|improve this answer





















                                    • 4





                                      That strongly depends on your definition of a 'keyword'. x = 1 is a perfectly fine statement, but does not contain keywords.

                                      – Joost
                                      May 8 '14 at 20:56
















                                    0














                                    A statement contains a keyword.



                                    An expression does not contain a keyword.



                                    print "hello" is statement, because print is a keyword.



                                    "hello" is an expression, but list compression is against this.



                                    The following is an expression statement, and it is true without list comprehension:



                                    (x*2 for x in range(10))





                                    share|improve this answer





















                                    • 4





                                      That strongly depends on your definition of a 'keyword'. x = 1 is a perfectly fine statement, but does not contain keywords.

                                      – Joost
                                      May 8 '14 at 20:56














                                    0












                                    0








                                    0







                                    A statement contains a keyword.



                                    An expression does not contain a keyword.



                                    print "hello" is statement, because print is a keyword.



                                    "hello" is an expression, but list compression is against this.



                                    The following is an expression statement, and it is true without list comprehension:



                                    (x*2 for x in range(10))





                                    share|improve this answer















                                    A statement contains a keyword.



                                    An expression does not contain a keyword.



                                    print "hello" is statement, because print is a keyword.



                                    "hello" is an expression, but list compression is against this.



                                    The following is an expression statement, and it is true without list comprehension:



                                    (x*2 for x in range(10))






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Mar 31 '14 at 13:55









                                    Racil Hilan

                                    20.4k123042




                                    20.4k123042










                                    answered Mar 31 '14 at 13:29









                                    abifromkeralaabifromkerala

                                    91




                                    91








                                    • 4





                                      That strongly depends on your definition of a 'keyword'. x = 1 is a perfectly fine statement, but does not contain keywords.

                                      – Joost
                                      May 8 '14 at 20:56














                                    • 4





                                      That strongly depends on your definition of a 'keyword'. x = 1 is a perfectly fine statement, but does not contain keywords.

                                      – Joost
                                      May 8 '14 at 20:56








                                    4




                                    4





                                    That strongly depends on your definition of a 'keyword'. x = 1 is a perfectly fine statement, but does not contain keywords.

                                    – Joost
                                    May 8 '14 at 20:56





                                    That strongly depends on your definition of a 'keyword'. x = 1 is a perfectly fine statement, but does not contain keywords.

                                    – Joost
                                    May 8 '14 at 20:56











                                    0














                                    Expressions:




                                    • Expressions are formed by combining objects and operators.

                                    • An expression has a value, which has a type.

                                    • Syntax for a simple expression:<object><operator><object>


                                    2.0 + 3 is an expression which evaluates to 5.0 and has a type float associated with it.



                                    Statements



                                    Statements are composed of expression(s). It can span multiple lines.






                                    share|improve this answer




























                                      0














                                      Expressions:




                                      • Expressions are formed by combining objects and operators.

                                      • An expression has a value, which has a type.

                                      • Syntax for a simple expression:<object><operator><object>


                                      2.0 + 3 is an expression which evaluates to 5.0 and has a type float associated with it.



                                      Statements



                                      Statements are composed of expression(s). It can span multiple lines.






                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        Expressions:




                                        • Expressions are formed by combining objects and operators.

                                        • An expression has a value, which has a type.

                                        • Syntax for a simple expression:<object><operator><object>


                                        2.0 + 3 is an expression which evaluates to 5.0 and has a type float associated with it.



                                        Statements



                                        Statements are composed of expression(s). It can span multiple lines.






                                        share|improve this answer













                                        Expressions:




                                        • Expressions are formed by combining objects and operators.

                                        • An expression has a value, which has a type.

                                        • Syntax for a simple expression:<object><operator><object>


                                        2.0 + 3 is an expression which evaluates to 5.0 and has a type float associated with it.



                                        Statements



                                        Statements are composed of expression(s). It can span multiple lines.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Aug 22 '17 at 23:33









                                        ssokheyssokhey

                                        4010




                                        4010























                                            0














                                            In simple words: a statement is made of one or more expressions, whereas an expression is made of one or more of Identifiers (names), Literals, and Operators.






                                            share|improve this answer




























                                              0














                                              In simple words: a statement is made of one or more expressions, whereas an expression is made of one or more of Identifiers (names), Literals, and Operators.






                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                In simple words: a statement is made of one or more expressions, whereas an expression is made of one or more of Identifiers (names), Literals, and Operators.






                                                share|improve this answer













                                                In simple words: a statement is made of one or more expressions, whereas an expression is made of one or more of Identifiers (names), Literals, and Operators.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Oct 1 '18 at 18:18









                                                FouadDevFouadDev

                                                686




                                                686























                                                    -1














                                                    Python calls expressions "expression statements", so the question is perhaps not fully formed.



                                                    A statement consists of pretty much anything you can do in Python: calculating a value, assigning a value, deleting a variable, printing a value, returning from a function, raising an exception, etc. The full list is here: http://docs.python.org/reference/simple_stmts.html#



                                                    An expression statement is limited to calling functions (e.g.,
                                                    math.cos(theta)"), operators ( e.g., "2+3"), etc. to produce a value.






                                                    share|improve this answer



















                                                    • 9





                                                      No, Python doesn't call expressions "expression statements". Python calls statements only consisting of a single expression "expression statements".

                                                      – Sven Marnach
                                                      Jan 18 '11 at 19:37











                                                    • ... and it's not alone doing so.

                                                      – user395760
                                                      Jan 18 '11 at 19:51
















                                                    -1














                                                    Python calls expressions "expression statements", so the question is perhaps not fully formed.



                                                    A statement consists of pretty much anything you can do in Python: calculating a value, assigning a value, deleting a variable, printing a value, returning from a function, raising an exception, etc. The full list is here: http://docs.python.org/reference/simple_stmts.html#



                                                    An expression statement is limited to calling functions (e.g.,
                                                    math.cos(theta)"), operators ( e.g., "2+3"), etc. to produce a value.






                                                    share|improve this answer



















                                                    • 9





                                                      No, Python doesn't call expressions "expression statements". Python calls statements only consisting of a single expression "expression statements".

                                                      – Sven Marnach
                                                      Jan 18 '11 at 19:37











                                                    • ... and it's not alone doing so.

                                                      – user395760
                                                      Jan 18 '11 at 19:51














                                                    -1












                                                    -1








                                                    -1







                                                    Python calls expressions "expression statements", so the question is perhaps not fully formed.



                                                    A statement consists of pretty much anything you can do in Python: calculating a value, assigning a value, deleting a variable, printing a value, returning from a function, raising an exception, etc. The full list is here: http://docs.python.org/reference/simple_stmts.html#



                                                    An expression statement is limited to calling functions (e.g.,
                                                    math.cos(theta)"), operators ( e.g., "2+3"), etc. to produce a value.






                                                    share|improve this answer













                                                    Python calls expressions "expression statements", so the question is perhaps not fully formed.



                                                    A statement consists of pretty much anything you can do in Python: calculating a value, assigning a value, deleting a variable, printing a value, returning from a function, raising an exception, etc. The full list is here: http://docs.python.org/reference/simple_stmts.html#



                                                    An expression statement is limited to calling functions (e.g.,
                                                    math.cos(theta)"), operators ( e.g., "2+3"), etc. to produce a value.







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Jan 18 '11 at 19:29









                                                    Walter NissenWalter Nissen

                                                    5,30642025




                                                    5,30642025








                                                    • 9





                                                      No, Python doesn't call expressions "expression statements". Python calls statements only consisting of a single expression "expression statements".

                                                      – Sven Marnach
                                                      Jan 18 '11 at 19:37











                                                    • ... and it's not alone doing so.

                                                      – user395760
                                                      Jan 18 '11 at 19:51














                                                    • 9





                                                      No, Python doesn't call expressions "expression statements". Python calls statements only consisting of a single expression "expression statements".

                                                      – Sven Marnach
                                                      Jan 18 '11 at 19:37











                                                    • ... and it's not alone doing so.

                                                      – user395760
                                                      Jan 18 '11 at 19:51








                                                    9




                                                    9





                                                    No, Python doesn't call expressions "expression statements". Python calls statements only consisting of a single expression "expression statements".

                                                    – Sven Marnach
                                                    Jan 18 '11 at 19:37





                                                    No, Python doesn't call expressions "expression statements". Python calls statements only consisting of a single expression "expression statements".

                                                    – Sven Marnach
                                                    Jan 18 '11 at 19:37













                                                    ... and it's not alone doing so.

                                                    – user395760
                                                    Jan 18 '11 at 19:51





                                                    ... and it's not alone doing so.

                                                    – user395760
                                                    Jan 18 '11 at 19:51











                                                    -1














                                                    I think an expression contains operators + operands and the object that holds the result of the operation... e.g.



                                                    var sum = a + b;


                                                    but a statement is simply a line of a code (it may be an expression) or block of code... e.g.



                                                    fun printHello(name: String?): Unit {
                                                    if (name != null)
                                                    println("Hello ${name}")
                                                    else
                                                    println("Hi there!")
                                                    // `return Unit` or `return` is optional


                                                    }






                                                    share|improve this answer




























                                                      -1














                                                      I think an expression contains operators + operands and the object that holds the result of the operation... e.g.



                                                      var sum = a + b;


                                                      but a statement is simply a line of a code (it may be an expression) or block of code... e.g.



                                                      fun printHello(name: String?): Unit {
                                                      if (name != null)
                                                      println("Hello ${name}")
                                                      else
                                                      println("Hi there!")
                                                      // `return Unit` or `return` is optional


                                                      }






                                                      share|improve this answer


























                                                        -1












                                                        -1








                                                        -1







                                                        I think an expression contains operators + operands and the object that holds the result of the operation... e.g.



                                                        var sum = a + b;


                                                        but a statement is simply a line of a code (it may be an expression) or block of code... e.g.



                                                        fun printHello(name: String?): Unit {
                                                        if (name != null)
                                                        println("Hello ${name}")
                                                        else
                                                        println("Hi there!")
                                                        // `return Unit` or `return` is optional


                                                        }






                                                        share|improve this answer













                                                        I think an expression contains operators + operands and the object that holds the result of the operation... e.g.



                                                        var sum = a + b;


                                                        but a statement is simply a line of a code (it may be an expression) or block of code... e.g.



                                                        fun printHello(name: String?): Unit {
                                                        if (name != null)
                                                        println("Hello ${name}")
                                                        else
                                                        println("Hi there!")
                                                        // `return Unit` or `return` is optional


                                                        }







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Jul 27 '17 at 13:02









                                                        Rashid IqbalRashid Iqbal

                                                        27048




                                                        27048






























                                                            draft saved

                                                            draft discarded




















































                                                            Thanks for contributing an answer to Stack Overflow!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function () {
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4728073%2fwhat-is-the-difference-between-an-expression-and-a-statement-in-python%23new-answer', 'question_page');
                                                            }
                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            Popular posts from this blog

                                                            Mossoró

                                                            Error while reading .h5 file using the rhdf5 package in R

                                                            Pushsharp Apns notification error: 'InvalidToken'