logo

Python – 複数行のステートメント

この記事では、Python プログラミング言語の複数行ステートメントの概念を理解します。

Python でのステートメント:

Python では、ステートメントは、Python インタプリタが読み取って実行できる論理コマンドです。それは、代入ステートメントまたは Python の式である可能性があります。



Python の複数行ステートメント:

Python では、ステートメントは通常 1 行で記述され、これらの行の最後の文字は改行です。ステートメントを 1 行以上に拡張するには、中かっこ {}、括弧 ()、四角形 []、セミコロン ;、および継続文字スラッシュ を使用できます。コード内の要件に応じて、これらのいずれかを使用できます。行継続文字を使用すると、長いステートメントを多数の行 () に明示的に分割できます。

コード:

Python3








# Initialize the lines using continuation character> g>=> 'geeks> for>> geeks'> print>(g)>

>

>

上記のコードで継続文字を使用しない場合、コードは終了していない文字列リテラル エラーを返します。

出力:

geeksforgeeks>

行の継続は 2 つの異なる方法に分けられます。

  • 明示的な行の継続
  • 暗黙的な行の継続

(明示的な行継続) を使用する:

このタイプの複数行ステートメントでは、行継続文字 () を使用してステートメントを複数行に分割します。

例:

この例では、Python プログラミングの複数行で同じ行を続けるための明示的な行継続である「」記号を使用して、テキストと数式を初期化しています。

Python3




# Initializing a text using the> # Explicit multi-line statement.> text>=> 'A Computer Science portal> for> geeks. It contains well written, well> >thought>and> well explained> >computer science>and> programming> >articles'> print>(' Initializing a text using> >the Explicit multi>->line statement', text)> # Initializing a mathematical expression> # using the Explicit multi-line statement.> add>=> 50> +> > >40> -> > >52> print>(' Initializing a mathematical expression> >using the Explicit multi>->line statement', add)>

Excelで在職期間を計算する
>

>

出力:

Initializing a text using the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well thought and well explained computer science and programming articles Initializing a mathematical expression using the Explicit multi-line statement 38>

括弧を使用する (暗黙的な行継続):

このタイプの複数行ステートメントでは、括弧 ( )、括弧 [ ]、および中括弧 { } を使用してステートメントを分割するときに、暗黙的な行継続が使用されます。

例:

この例では、括弧 ( )、括弧 [ ]、および中括弧 { } 記号を使用してリストと数式を初期化しています。これは、Python プログラミングで複数の行で同じ行を継続するための暗黙の行継続です。

Python3




# Initializing a string> # using parentheis '()'.> g>=> (f>'geeks'> >f>'for'> >f>'geeks'>)> print>(g)> # Initializing a list using the> # Implicit multi-line statement.> list> => [>5>,> >4>,>3>,>2>,>1> >]> print>()> print>('Initializing a>list> using the> >Implicit multi>->line statement',>list>)> # Initializing a mathematical expression> # using the Implicit multi-line statement.> add>=> (>50> +> >40> -> >52>)> print>()> print>('Initializing a mathematical expression> >using the Explicit multi>->line statement', add)>

>

>

Androidでアプリケーションを再表示する方法

出力:

geeksforgeeks Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 38>

三重引用符(改行)の使用

例:

Python3




# Initializing a string> # using triple qoute.> g>=> '''geeks> for> geeks'''> print>(g)> print>()> print>(f>'escape charactor: {g!r}'>)>

>

>

出力

geeks for geeks escape charactor: 'geeks
for
geeks'>