logo

Python の制御構造

ほとんどのプログラムは、単純な一連のステートメントを実行することで動作しません。コードは、変数値の変化に応じて選択を行い、プログラム内のいくつかの経路をたどることができるように記述されています。

すべてのプログラミング言語には、これらの制御フローの実行を可能にする一連の制御構造があらかじめ組み込まれており、これが考えられます。

このチュートリアルでは、Python プログラムにループと分岐、つまり条件を追加する方法を検討します。

制御構造の種類

制御フローとは、プログラムが実行中に従うシーケンスを指します。

条件、ループ、および呼び出し関数は、Python プログラムの制御方法に大きな影響を与えます。

Python には 3 種類の制御構造があります。

  • シーケンシャル - プログラムのデフォルトの動作
  • 選択 - この構造は、条件をチェックして分岐することによって決定を行うために使用されます。
  • 繰り返し - この構造はループ、つまりコード ブロックの特定の部分を繰り返し実行するために使用されます。

一連

シーケンシャル ステートメントは、実行プロセスが順番に行われる一連のステートメントです。シーケンシャル ステートメントの問題は、いずれかの行でロジックが壊れると、ソース コード全体の実行が中断されることです。

コード

Javaの文字列を連結する
 # Python program to show how a sequential control structure works # We will initialize some variables # Then operations will be done # And, at last, results will be printed # Execution flow will be the same as the code is written, and there is no hidden flow a = 20 b = 10 c = a - b d = a + b e = a * b print('The result of the subtraction is: ', c) print('The result of the addition is: ', d) print('The result of the multiplication is: ', e) 

出力:

 The result of the subtraction is: 10 The result of the addition is : 30 The result of the multiplication is: 200 

選択/決定制御ステートメント

選択制御構造で使用されるステートメントは、分岐ステートメント、または基本的な役割が決定を行うことであるため、決定制御ステートメントとも呼ばれます。

プログラムは、これらの選択ステートメントを使用して多くの条件をテストでき、指定された条件が true かどうかに応じて、異なるコード ブロックを実行できます。

意思決定制御構造にはさまざまな形式があります。最も一般的に使用される制御構造をいくつか示します。

  • 場合のみ
  • if-else
  • ネストされた if
  • 完全な if-elif-else

シンプルな場合

Python の if ステートメントは制御フロー ステートメントと呼ばれます。選択ステートメントは、特定のコードを実行するのに役立ちますが、それは特定の状況でのみです。基本的な if ステートメントでテストする条件は 1 つだけです。

if ステートメントの基本的な構造は次のとおりです。

構文

 if : The code block to be executed if the condition is True 

これらのステートメントは常に実行されます。これらはメインコードの一部です。

if ステートメントの後にインデントされて記述されたすべてのステートメントは、if キーワードの後の条件指定が True の場合に実行されます。 if 条件に関係なく常に実行されるコード ステートメントのみがメイン コードに合わせて記述されたステートメントです。 Python は、これらのタイプのインデントを使用して、特定の制御フロー ステートメントのコード ブロックを識別します。指定された制御構造は、インデントされたステートメントのフローのみを変更します。

以下にいくつかの例を示します。

コード

 # Python program to show how a simple if keyword works # Initializing some variables v = 5 t = 4 print(&apos;The initial value of v is&apos;, v, &apos;and that of t is &apos;,t) # Creating a selection control structure if v &gt; t : print(v, &apos;is bigger than &apos;, t) v -= 2 print(&apos;The new value of v is&apos;, v, &apos;and the t is &apos;,t) # Creating the second control structure if v <t : print(v , 'is smaller than ', t) v +="1" print('the new value of is v) # creating the third control structure if t: v, ' and t,', t, are equal') < pre> <p> <strong>Output:</strong> </p> <pre> The initial value of v is 5 and that of t is 4 5 is bigger than 4 The new value of v is 3 and the t is 4 3 is smaller than 4 the new value of v is 4 The value of v, 4 and t, 4, are equal </pre> <h3>if-else</h3> <p>If the condition given in if is False, the if-else block will perform the code t=given in the else block.</p> <p> <strong>Code</strong> </p> <pre> # Python program to show how to use the if-else control structure # Initializing two variables v = 4 t = 5 print(&apos;The value of v is &apos;, v, &apos;and that of t is &apos;, t) # Checking the condition if v &gt; t : print(&apos;v is greater than t&apos;) # Giving the instructions to perform if the if condition is not true else : print(&apos;v is less than t&apos;) </pre> <p> <strong>Output:</strong> </p> <pre> The value of v is 4 and that of t is 5 v is less than t </pre> <h2>Repetition</h2> <p>To repeat a certain set of statements, we use the repetition structure.</p> <p>There are generally two loop statements to implement the repetition structure:</p> <ul> <li>The for loop</li> <li>The while loop</li> </ul> <h3>For Loop</h3> <p>We use a for loop to iterate over an iterable Python sequence. Examples of these data structures are lists, strings, tuples, dictionaries, etc. Under the for loop code block, we write the commands we want to execute repeatedly for each sequence item.</p> <p> <strong>Code</strong> </p> <pre> # Python program to show how to execute a for loop # Creating a sequence. In this case, a list l = [2, 4, 7, 1, 6, 4] # Executing the for loops for i in range(len(l)): print(l[i], end = &apos;, &apos;) print(&apos;
&apos;) for j in range(0,10): print(j, end = &apos;, &apos;) </pre> <p> <strong>Output:</strong> </p> <pre> 2, 4, 7, 1, 6, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, </pre> <h3>While Loop</h3> <p>While loops are also used to execute a certain code block repeatedly, the difference is that loops continue to work until a given precondition is satisfied. The expression is checked before each execution. Once the condition results in Boolean False, the loop stops the iteration.</p> <p> <strong>Code</strong> </p> <pre> # Python program to show how to execute a while loop b = 9 a = 2 # Starting the while loop # The condition a <b 1 will be checked before each iteration while a < b: print(a, end=" " ) + print('while loop is completed') pre> <p> <strong>Output:</strong> </p> <pre> 2 3 4 5 6 7 8 While loop is completed </pre> <hr></b></pre></t>

if-else

if で指定された条件が False の場合、if-else ブロックは else ブロックで指定されたコード t= を実行します。

コード

 # Python program to show how to use the if-else control structure # Initializing two variables v = 4 t = 5 print(&apos;The value of v is &apos;, v, &apos;and that of t is &apos;, t) # Checking the condition if v &gt; t : print(&apos;v is greater than t&apos;) # Giving the instructions to perform if the if condition is not true else : print(&apos;v is less than t&apos;) 

出力:

デスクトップ.iniとは何ですか
 The value of v is 4 and that of t is 5 v is less than t 

繰り返し

特定の一連のステートメントを繰り返すには、繰り返し構造を使用します。

通常、繰り返し構造を実装するには 2 つのループ ステートメントがあります。

  • for ループ
  • while ループ

For ループ

for ループを使用して、反復可能な Python シーケンスを反復処理します。これらのデータ構造の例としては、リスト、文字列、タプル、辞書などが挙げられます。 for ループ コード ブロックの下に、シーケンス項目ごとに繰り返し実行するコマンドを記述します。

コード

 # Python program to show how to execute a for loop # Creating a sequence. In this case, a list l = [2, 4, 7, 1, 6, 4] # Executing the for loops for i in range(len(l)): print(l[i], end = &apos;, &apos;) print(&apos;
&apos;) for j in range(0,10): print(j, end = &apos;, &apos;) 

出力:

 2, 4, 7, 1, 6, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 

While ループ

ループは特定のコード ブロックを繰り返し実行するためにも使用されますが、異なる点は、ループは特定の前提条件が満たされるまで動作し続けることです。式は実行前に毎回チェックされます。条件の結果がブール値 False になると、ループは反復を停止します。

コード

 # Python program to show how to execute a while loop b = 9 a = 2 # Starting the while loop # The condition a <b 1 will be checked before each iteration while a < b: print(a, end=" " ) + print(\'while loop is completed\') pre> <p> <strong>Output:</strong> </p> <pre> 2 3 4 5 6 7 8 While loop is completed </pre> <hr></b>