difference between statements（文）and expressions（式）in Python

#経緯

学校の宿題で[reading](http://composingprograms.com/pages/11-getting-started.html)が出ているので読んでいたら`expressions`と`statements`という概念にぶつかる。読んでもいまいち違いがピンとこないので調べてみることに。~~学校側もこれだけ読んで全部理解しろと言っているわけではないと信じてる…~~

#def. of statements（文）and expressions（式）
[reading](http://composingprograms.com/pages/11-getting-started.html)による定義は

> 
Statements typically describe actions. When the Python interpreter executes a statement, it carries out the corresponding action.

ってことは`x = 1`とか`y = "apple"`とかを意味してるの？っていうのが自分の解釈でしたが、[stackoverflow](http://stackoverflow.com/questions/4728073/what-is-the-difference-between-an-expression-and-a-statement-in-python)には

>
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. Statements (see 1, 2), on the other hand, are everything that can make up a line (or several lines) of Python code.

ってことは`expressions`は`statements`としても捉えられるってことになる。`expression`だけで完結させられることもできる一方で`statement`は（[この辺](https://docs.python.org/2/reference/simple_stmts.html)を読んでると余計そう思わせる）どちらかというと他のブロックと上手く繋がって動くという印象。`statement`の中にはほぼ確実に`expression`がある。

[quora](https://www.quora.com/Whats-the-difference-between-a-statement-and-an-expression-in-Python)より

>A statement is a complete line of code that performs some action, while an expression is any section of the code that evaluates to a value. Expressions can be combined “horizontally” into larger expressions using operators, while statements can only be combined “vertically” by writing one after another, or with block constructs. Every expression can be used as a statement (whose effect is to evaluate the expression and ignore the resulting value), but most statements cannot be used as expressions. 

またこちらの[stackoverflow](http://stackoverflow.com/questions/4782590/what-is-an-expression-in-python)の`expression`に対する解答も分かりやすかった。

>
Expressions represent something, like a number, a string, or an instance of a class. Any value is an expression!
Anything that does something is a statement. Any assignment to a variable or function call is a statement. Any value 
contained in that statement in an expression. 

>`foo = "hello"` is a statement that assigns foo to the value of the expression `"hello"`. Since the code `"hello"` is a simple expression, meaning it contains no operations, nothing is actually evaluated, so foo is just assigned to `"hello"`. More complex expressions actually evaluate things, like adding numbers. Using the word expression seems like it is making things more confusing. Expressions are nothing but values, except they can have operations like addition or subtraction.

>`eval` evaluates the string as if it were a python expression. `Eval` does takes an expression as an argument. However, there's nothing special about this since every single value is an expression. Saying "eval takes a value as an argument" is saying exactly the same thing, but it sounds much simpler. :D
`eval( "2+2" )` passes the string `"2+2"` to the function. The function evaluates the expression contained in the string, which comes out to 4.

>The book by Zelle says eval(<string>) evaluates string as an expression, what does that exactly mean if string is already an expression?

>Any string is an expression since it represents a value. However, what is in the string has absolutely no impact on it being an expression. If its a value, its an expression. When it is "evaluated as an expression by eval", the characters inside the string are executed as if they were a python expression.

さらに[ここ](http://d.hatena.ne.jp/yone-ken/20090421/p1)でも

>
文はそれ単独で完結する言語要素です。式はそれ単独では基本的に完結せず、文または式の一部として使用される言語要素です。また、式の最大の特徴として、値を返すという点が挙げられます（文は値を返しません）。例えば、定数式はその値そのものを返します。条件式は真偽値(True/False)を返す式です。条件式の一つとして比較式がありますが、条件式そのものはTrue/Falseを返せばなんでもよいため、単なるBooleanのTrue(定数式)もまた条件式になります。比較式は比較演算子を挟んで左辺と右辺の式を比較し、その結果をTrue/Falseで返すため、条件式になることができます。

ちなみに__`evaluate argument`とは簡単に言えば`calling function`を意味する__。関数の括弧の中の引数を実際に引き渡すときに起こる。__`evaluate function`は`calling function`とは異なる__。これはただ単に関数があるよって定義されただけ。なので正しい順番は`evaluate function`→`evaluate argument`→`function gets called`となる。

#追記

わざわざ探さなくとも次の[講義](https://www.youtube.com/watch?v=vguCdBIHQmI)で紹介されていたので一応（笑）

>an expression describes a computation and evaluates to a value. all expressions can use function call notation

`add ( 2 , 3)`という`call expression`はそれぞれ一つ一つが`expression`でもある。`add`は`operator`、`()`の中に入っているものは`operand`と呼ばれる。`call expression`はまず`operator`と`operand`の認識し、`operand`を`argument`として扱い`function`を用いて`value`をだす。ちなみに自明ではあるがこれは`2 + 3`と表すことも出来る。`add(2,3)`から`2 + 3`へと変換することを__syntactic sugar__と呼ぶ。また数学で使われるサイン`+, *, - or /`を__infix notation__と呼ぶ。

書いてあることは上と全く同じですが[こちら](http://composingprograms.com/pages/12-elements-of-programming.html)にも記載されていたので一部転載

>
Executing `x = 2` does not return a value nor evaluate a function on some arguments, since the purpose of assignment is instead to bind a name to a value. In general, statements are not evaluated but executed; they do not produce a value but instead make some change. Each type of expression or statement has its own evaluation or execution procedure.

#追記#2

<a href="http://composingprograms.com/pages/15-control.html#statements" target="_blank">readingの"Statements" section</a>より転載
>Expressions can also be executed as statements, in which case they are evaluated, but their value is discarded. Executing a pure function has no effect, but executing a non-pure function can cause effects as a consequence of function application.

ref: <a href="http://qiita.com/weedslayer/items/9ac165613bc896a1aaa1">pure functionとnon-pure functionの違い</a>

```
def square(x):
    mul(x, x)# Watch out! This call doesn't return a value.
```

`expression`も`statement`と同様、処理は行われますが、`value`は表示されません。そこで`assignment, def, and return statements`などと合わせて`value`を出します。

#追記#3

後の講義で教授が`expression`と`statement`について解説していたのを元に個人的に簡単なダイアグラムを作ってみたので参考まで。

![Screen Shot 2016-01-31 at 2.18.24 AM.png](https://qiita-image-store.s3.amazonaws.com/0/87666/25899b82-5997-5114-15b9-ebdde5e0ea85.png)