Pythonのステートメントの意味とは?改行とかの使い方を解説する

スポンサーリンク

Pythonステートメントは、Pythonインタープリタによって実行されるコード文のことです。

スポンサーリンク

Pythonのステートメントの例

簡単な文例を見てみましょう。

以下のコードには、3つのステートメントがあります。

count = 10 # statement 1
class Foo:# statement 2
    pass # statement 3

Pythonの複数行のステートメントについて

Pythonのステートメント(文)は通常、1行で記述されて、改行文字が文の終わりを示します。

文が非常に長い場合は、バックスラッシュ()を使って明示的に複数行に分けることができます

それでは、複数行のステートメントの例を見てみましょう。

message = "Hello There. 
You have come to the right place to learn Python Programming. 
Follow the tutorials to become expert in Python.  
Don't forget to share it with your friends too."

math_result = 1 + 2 + 3 + 4 + 
          5 + 6 + 7 + 8 + 
          9 + 10

print(message)
print(math_result)

結果は以下の通りです。

Hello There. You have come to the right place to learn Python Programming. Follow the tutorials to become expert in Python.  Don't forget to share it with your friends too.
55

Pythonで一行に複数のステートメント(文)を入れる方法

1行に複数のステートメントを記述するためには、セミコロン(;)を使用すると1行に複数のステートメントを記述することができます

x = 1 ; y = 2 ; z = 3
タイトルとURLをコピーしました