Skip to content

Instantly share code, notes, and snippets.

@TakesxiSximada
Created October 25, 2016 08:40
Show Gist options
  • Save TakesxiSximada/afd713fcbbe9dfd720468f2e6f38a9de to your computer and use it in GitHub Desktop.
Save TakesxiSximada/afd713fcbbe9dfd720468f2e6f38a9de to your computer and use it in GitHub Desktop.
式と文

式と文

プログラミング言語には式と文があります。

Pythonの式と文

式はevalしたときになんらかの値を返す構文のことです。このなんらかの値はNone含みます。

式の例

  • int(1) は1を返すので式です。
  • 1 は1を返すので式です。
  • "expression" は"expression"を返すので式です。
  • (ii + 1 for ii in range(10)) はジェネレータオブジェクトを返すので式です。

式じゃない例

  • a = 1は何も返さないので式ではなりません。

    >>> eval('a = 1')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1
        a = 1
          ^
    SyntaxError: invalid syntax
    
  • for ii in range(10): pass は何も返さないので式ではありません。

print は?

Python2とPython3ではprintの扱いが違います。 Python2ではprintは文です。そのためevalできません。

>>> eval('print 1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    print 1
        ^
SyntaxError: invalid syntax
>>> quit()

Python3ではprintは式になりました。そのためevalできます。

>>> eval('print(1)')
1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment