Expression and statement in python
In Python, what is the difference between expressions and statements? Show
Chris Martin 29.7k8 gold badges74 silver badges131 bronze badges asked Jan 18, 2011 at 19:19
1 Expressions only contain identifiers, literals and operators, where operators include arithmetic
and boolean operators, the function call operator
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:
songololo 4,4063 gold badges32 silver badges49 bronze badges answered Jan 18, 2011 at 19:27
Sven MarnachSven Marnach 542k114 gold badges914 silver badges816 bronze badges 15 Expression -- from the New Oxford American Dictionary:
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:
Statement from Wikipedia:
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:
answered Jan 19, 2011 at 0:25
dawgdawg 92.4k23 gold badges121 silver badges200 bronze badges 2 Though this isn't related to Python: An
Amir 2,0491 gold badge17 silver badges24 bronze badges answered Jan 18, 2011 at 19:29
user225312user225312 120k66 gold badges167 silver badges181 bronze badges 8 An expression is something that can be reduced to a value, for example It's easy to check:
If it doesn't work, it's a statement, if it does, it's an expression. Another statement could be:
as it cannot be reduced to a value.
np8 22.8k9 gold badges78 silver badges89 bronze badges answered Jan 18, 2011 at 19:24
FlaviusFlavius 13.3k13 gold badges80 silver badges124 bronze badges 6 Statements represent an action or command e.g print statements, assignment statements.
Expression is a combination of variables, operations and values that yields a result value.
Lastly, expression statements
answered Apr 10, 2015 at 18:59
Emmanuel OsimosuEmmanuel Osimosu 5,1622 gold badges35 silver badges38 bronze badges An expression is something, while a statement does
something.
PS:The interpreter always prints out the values of all expressions.
Adalcar 1,45810 silver badges25 bronze badges answered Oct 18, 2017 at 9:29
4
answered Jan 3, 2018 at 13:32
Steven SpunginSteven Spungin 23.7k5 gold badges77 silver badges66 bronze badges 0 Expressions always evaluate to a value, statements don't. e.g. variable declaration and assignment are statements because they do not return a value
Here we have two operands - a variable 'sum' on the left and an expression on the right. The whole thing is a statement, but the bit on the right is an expression as that piece of code returns a value.
Function calls, arithmetic and boolean operations are good examples of expressions. Expressions are often part of a statement. The distinction between the two is often required to indicate whether we require a pice of code to return a value. answered Aug 8, 2020 at 9:15
Alex SAlex S 611 silver badge3 bronze badges STATEMENT: A Statement is a action or a command that does something. Ex: If-Else,Loops..etc
EXPRESSION: A Expression is a combination of values, operators and literals which yields something.
answered Mar 9, 2019 at 15:33
Raja ShekarRaja Shekar 1232 silver badges12 bronze badges 2 ReferencesExpressions and statements2.3 Expressions and statements - thinkpython2 by Allen B. Downey 2.10. Statements and Expressions - How to Think like a Computer Scientist by Paul Resnick and Brad Miller An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions:
A statement is a unit of code that has an effect, like creating a variable or displaying a value.
answered Aug 25, 2020 at 8:51
blessedblessed 6051 gold badge9 silver badges12 bronze badges An expression translates to a value. A statement consumes a value* to produce a result**. *That
includes an empty value, like: **This result can be any action that changes something; e.g. changes the memory ( x = 1) or changes something on the screen ( print("x") ). A few notes:
answered Nov 18, 2020 at 10:18
PontiosPontios 2,23925 silver badges30 bronze badges Statements before could change the state of our Python program: create or update variables, define function, etc. And expressions just return some value can't change the global state or local state in a function. But now we got answered Dec 28, 2019 at 15:24
roachsinairoachsinai 5194 silver badges12 bronze badges Expressions:
Statements Statements are composed of expression(s). It can span multiple lines. answered Aug 22, 2017 at 23:33
ObiWanObiWan 1961 silver badge12 bronze badges A statement contains a keyword. An expression does not contain a keyword.
The following is an expression statement, and it is true without list comprehension:
Racil Hilan 24k12 gold badges48 silver badges52 bronze badges answered Mar 31, 2014 at 13:29
2 Think of statements as consecutive actions or instructions that your program executes. So, value assignments, if clauses, together with for and while loops, are all statements. Function and class definitions are statements, too. Think of expressions as anything that can be put into an if clause. Typical examples of expressions are literals, values returned by operators (excluding in-place operators), and comprehensions, such as list, dictionary, and set comprehensions. Function calls and method calls are expressions, too. Python 3.8 introduced the dedicated := operator, which assigns a value to the variable but acts as an expression instead of a statement. Due to its visual appearance, it was quickly nicknamed the walrus operator. answered May 20 at 8:52
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, 2011 at 19:29
Walter NissenWalter Nissen 15.8k4 gold badges25 silver badges27 bronze badges 12 Not the answer you're looking for? Browse other questions tagged python expression or ask your own question. |