What is the difference between an expression and a statement in Python?
In Python, what is the difference between expressions and statements?
python expression
add a comment |
In Python, what is the difference between expressions and statements?
python expression
add a comment |
In Python, what is the difference between expressions and statements?
python expression
In Python, what is the difference between expressions and statements?
python expression
python expression
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
add a comment |
add a comment |
12 Answers
12
active
oldest
votes
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
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
|
show 1 more comment
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
ifis usually a statement, such asif x<0: x=0but you can also have a conditional expression likex=0 if x<0 else 1that are expressions. In other languages, like C, this form is called an operator like thisx=x<0?0:1; - You can write you own Expressions by writing a function.
def func(a): return a*ais an expression when used but made up of statements when defined. - An expression that returns
Noneis a procedure in Python:def proc(): passSyntactically, you can useproc()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 formfunc(x=2)is illegal in Python (or at least it has a different meaningfunc(a=3)sets the named argumentato 3)
add a comment |
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
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
|
show 2 more comments
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
add a comment |
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.
1
As executing your first example would show, assignment is not an expression (not really, that is -a = b = expris 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+3is NOT an expression. It is a statement (an assignment to be precise). The part1+3is 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
add a comment |
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.
in my opinion, a statement is an expression with a null value
– Adalcar
Oct 18 '17 at 9:35
add a comment |
- 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.
- 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.
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
add a comment |
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))
4
That strongly depends on your definition of a 'keyword'.x = 1is a perfectly fine statement, but does not contain keywords.
– Joost
May 8 '14 at 20:56
add a comment |
Expressions:
- Expressions are formed by combining
objectsandoperators. - 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.
add a comment |
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.
add a comment |
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.
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
add a comment |
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
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
|
show 1 more comment
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
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
|
show 1 more comment
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
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
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
|
show 1 more comment
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
|
show 1 more comment
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
ifis usually a statement, such asif x<0: x=0but you can also have a conditional expression likex=0 if x<0 else 1that are expressions. In other languages, like C, this form is called an operator like thisx=x<0?0:1; - You can write you own Expressions by writing a function.
def func(a): return a*ais an expression when used but made up of statements when defined. - An expression that returns
Noneis a procedure in Python:def proc(): passSyntactically, you can useproc()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 formfunc(x=2)is illegal in Python (or at least it has a different meaningfunc(a=3)sets the named argumentato 3)
add a comment |
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
ifis usually a statement, such asif x<0: x=0but you can also have a conditional expression likex=0 if x<0 else 1that are expressions. In other languages, like C, this form is called an operator like thisx=x<0?0:1; - You can write you own Expressions by writing a function.
def func(a): return a*ais an expression when used but made up of statements when defined. - An expression that returns
Noneis a procedure in Python:def proc(): passSyntactically, you can useproc()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 formfunc(x=2)is illegal in Python (or at least it has a different meaningfunc(a=3)sets the named argumentato 3)
add a comment |
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
ifis usually a statement, such asif x<0: x=0but you can also have a conditional expression likex=0 if x<0 else 1that are expressions. In other languages, like C, this form is called an operator like thisx=x<0?0:1; - You can write you own Expressions by writing a function.
def func(a): return a*ais an expression when used but made up of statements when defined. - An expression that returns
Noneis a procedure in Python:def proc(): passSyntactically, you can useproc()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 formfunc(x=2)is illegal in Python (or at least it has a different meaningfunc(a=3)sets the named argumentato 3)
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
ifis usually a statement, such asif x<0: x=0but you can also have a conditional expression likex=0 if x<0 else 1that are expressions. In other languages, like C, this form is called an operator like thisx=x<0?0:1; - You can write you own Expressions by writing a function.
def func(a): return a*ais an expression when used but made up of statements when defined. - An expression that returns
Noneis a procedure in Python:def proc(): passSyntactically, you can useproc()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 formfunc(x=2)is illegal in Python (or at least it has a different meaningfunc(a=3)sets the named argumentato 3)
edited May 23 '17 at 12:26
Community♦
11
11
answered Jan 19 '11 at 0:25
dawgdawg
59.2k1283152
59.2k1283152
add a comment |
add a comment |
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
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
|
show 2 more comments
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
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
|
show 2 more comments
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
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
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
|
show 2 more comments
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
|
show 2 more comments
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
add a comment |
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
add a comment |
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
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
edited Jan 10 '17 at 15:27
answered Apr 10 '15 at 18:59
eosimosueosimosu
1,2351624
1,2351624
add a comment |
add a comment |
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.
1
As executing your first example would show, assignment is not an expression (not really, that is -a = b = expris 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+3is NOT an expression. It is a statement (an assignment to be precise). The part1+3is 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
add a comment |
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.
1
As executing your first example would show, assignment is not an expression (not really, that is -a = b = expris 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+3is NOT an expression. It is a statement (an assignment to be precise). The part1+3is 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
add a comment |
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.
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.
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 = expris 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+3is NOT an expression. It is a statement (an assignment to be precise). The part1+3is 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
add a comment |
1
As executing your first example would show, assignment is not an expression (not really, that is -a = b = expris 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+3is NOT an expression. It is a statement (an assignment to be precise). The part1+3is 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
add a comment |
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.
in my opinion, a statement is an expression with a null value
– Adalcar
Oct 18 '17 at 9:35
add a comment |
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.
in my opinion, a statement is an expression with a null value
– Adalcar
Oct 18 '17 at 9:35
add a comment |
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.
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.
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
add a comment |
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
add a comment |
- 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.
- 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.
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
add a comment |
- 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.
- 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.
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
add a comment |
- 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.
- 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.
- 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.
- 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.
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
add a comment |
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
add a comment |
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))
4
That strongly depends on your definition of a 'keyword'.x = 1is a perfectly fine statement, but does not contain keywords.
– Joost
May 8 '14 at 20:56
add a comment |
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))
4
That strongly depends on your definition of a 'keyword'.x = 1is a perfectly fine statement, but does not contain keywords.
– Joost
May 8 '14 at 20:56
add a comment |
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))
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))
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 = 1is a perfectly fine statement, but does not contain keywords.
– Joost
May 8 '14 at 20:56
add a comment |
4
That strongly depends on your definition of a 'keyword'.x = 1is 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
add a comment |
Expressions:
- Expressions are formed by combining
objectsandoperators. - 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.
add a comment |
Expressions:
- Expressions are formed by combining
objectsandoperators. - 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.
add a comment |
Expressions:
- Expressions are formed by combining
objectsandoperators. - 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.
Expressions:
- Expressions are formed by combining
objectsandoperators. - 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.
answered Aug 22 '17 at 23:33
ssokheyssokhey
4010
4010
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Oct 1 '18 at 18:18
FouadDevFouadDev
686
686
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
answered Jul 27 '17 at 13:02
Rashid IqbalRashid Iqbal
27048
27048
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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