05-Python

Python(Part.25)| pythonの基礎【反復構造】(二重for文)| The Basics of python [Iterative Structures] (double for statement)

python| まとめ | 現役エンジニア&プログラミングスクール講師「python」のまとめページです。pythonに関して抑えておきたい知識や文法やにについて記事をまとめています。まとめページの下部には「おすすめの学習書籍」「おすすめのITスクール情報」「おすすめ求人サイト」について情報を掲載中...

目標(Objectives)

「二重for文」について理解する。
Understand the “double for statement.”

反復構造(for文)
Understand the “double for statement.”

基本制御構造の復習
Review of basic control structures

基本制御構造とは(再掲載)
What is basic control structure (repetition)

「制御構造」のうち、基本とする3つの構造として「順次構造」「選択構造」「反復構造」が存在します。これらは「基本制御構造」と呼ばれて、次の構造をもっています。

  1. 「順次構造」…プログラムを書かれた順番で実行します。
  2. 「選択構造」…条件の成立・不成立によって実行するプログラムを選択します。
  3. 「反復構造」…条件の成立・不成立によって実行するプログラムを反復します。

Among the “control structures,” there are three basic structures: sequential, selective, and repetitive. These are called “basic control structures” and have the following structure.

  1. Sequential structure: Programs are executed in the order in which they are written.
  2. Selective structure: The program is selected to be executed according to whether the condition is satisfied or not.
  3. Iterative structure: The program to be executed is iterated according to whether the condition is satisfied or not.

「二重for文」の記述とその動き
Double for statement” description and its behavior

「二重for文」の記述
Description of “double for statement

二重の「for文」は次のように記述します。外側の「for文」のブロックに、もうひとつの「for文」が存在する形になります。

A double “for statement” is described as follows. Another “for statement” exists in the outer “for statement” block.

【English translation of captcha】
変数
→The variable
イテラブルオブジェクト
→Iterable objects
ブロック
→Block

「二重for文」の動き
The behavior of “double for statement”

二重の繰り返しでは、外側の繰り返し処理の1ステップ毎に、内側の繰り返し処理が全ステップ行われることになります。
In a double iteration, for each step of the outer iteration, all steps of the inner iteration are performed.

二重for文のように何かの処理が入れ子の状態になっているものを「ネスト」といいます。今回のケースでは「for文」が「for文」にネストされていることになります。

A situation in which some processes are nested, as in a double for statement, is called “nesting. In this case, the inner “for statement” is nested in the outer “for statement.

【English translation of captcha】
変数
→The variable
ブロック
→Block
毎年12か月実行されます。
→It runs 12 months each year.

例えば下のプログラムの実行結果や動きは次のようになります。

year_list= [2024,2025,2026]
month_list = [1,2,3,4,5,6,7,8,9,10,11,12]

for this_year in year_list:
    print(this_year, "年")
    for this_month in month_list:
        print(this_month, "月", end=" ")

    #↓ここは外側のfor文のブロック内の処理です。
    #内側のfor文の処理が全て終わったあと、改行のためにprint()を実行しています。
    print() 

【English translation of code】
年→year、月→month
ここは外側のfor文のブロック内の処理です。
→This is the processing of the outer for statement block.
内側のfor文の処理が全て終わったあと、改行のためにprint()を実行しています。
→After all steps have been processed, print() is executed for a new line.

実行結果
Execution Result

この実行結果を二重for文のイラストと同様の色分けを行うと次のようになります。
The result of this execution, with color coding similar to the illustration of the double for statement, is shown below.

繰り返し文のネストではマトリックス(行列)を作成することができます。
Nesting of repeating statements allows for the creation of matrices (matrices).

次のようなプログラムでは掛け算の九九一覧を表示することが可能です。
The following program can display a list of multiplication nines.

for left_value in [ 1,2,3,4,5,6,7,8,9 ]:
    for right_value in [ 1,2,3,4,5,6,7,8,9 ]:
        print(left_value, "✕", right_value, "=", left_value*right_value, end="\t")
    print()

実行結果
Execution Result

今回は以上になります。
That’s all for this time.

「python」おすすめ書籍 ベスト3 | 現役エンジニア&プログラミングスクール講師「python」の学習でお勧めしたい書籍をご紹介しています。お勧めする理由としては、考え方、イメージなどを適切に捉えていること、「生のpython」に焦点をあてて解説をしている書籍であることなどが理由です。勿論、この他にも良い書籍はありますが、特に質の高かったものを選んで記事にしています。ページの下部には「おすすめのITスクール情報」「おすすめ求人サイト」について情報を掲載中。...

ブックマークのすすめ

「ほわほわぶろぐ」を常に検索するのが面倒だという方はブックマークをお勧めします。ブックマークの設定は別記事にて掲載しています。

「お気に入り」の登録・削除方法【Google Chrome / Microsoft Edge】| 現役エンジニア&プログラミングスクール講師「お気に入り」の登録・削除方法【Google Chrome / Microsoft Edge】について解説している記事です。削除方法も掲載しています。...
【パソコン選び】失敗しないための重要ポイント | 現役エンジニア&プログラミングスクール講師【パソコン選び】失敗しないための重要ポイントについての記事です。パソコンのタイプと購入時に検討すべき点・家電量販店で見かけるCPUの見方・購入者が必要とするメモリ容量・HDDとSSDについて・ディスプレイの種類・バッテリーの持ち時間や保証・Officeソフト・ウィルス対策ソフトについて書いています。...
RELATED POST
05-Python

Python(Part.13)| pythonの基礎【反復構造(while文)】| 現役エンジニア&プログラミングスクール講師

2023年11月13日
プログラミング学習 おすすめスクール おすすめ書籍情報発信 パソコン初心者 エンジニア希望者 新人エンジニア IT業界への就職・転職希望者 サポートサイト Programming learning Recommended schools Recommended books Information dissemination Computer beginners Prospective engineers New engineers Prospective job seekers in the IT industry Support site
05-Python

Python(Part.17)| pythonのリスト(利用編-2)| 現役エンジニア&プログラミングスクール講師

2023年12月3日
プログラミング学習 おすすめスクール おすすめ書籍情報発信 パソコン初心者 エンジニア希望者 新人エンジニア IT業界への就職・転職希望者 サポートサイト Programming learning Recommended schools Recommended books Information dissemination Computer beginners Prospective engineers New engineers Prospective job seekers in the IT industry Support site
05-Python

Python(Part.9)| pythonの基礎【制御構造】| 現役エンジニア&プログラミングスクール講師

2023年9月26日
プログラミング学習 おすすめスクール おすすめ書籍情報発信 パソコン初心者 エンジニア希望者 新人エンジニア IT業界への就職・転職希望者 サポートサイト Programming learning Recommended schools Recommended books Information dissemination Computer beginners Prospective engineers New engineers Prospective job seekers in the IT industry Support site
05-Python

「python」おすすめ書籍 ベスト3 | 現役エンジニア&プログラミングスクール講師

2023年9月20日
プログラミング学習 おすすめスクール おすすめ書籍情報発信 パソコン初心者 エンジニア希望者 新人エンジニア IT業界への就職・転職希望者 サポートサイト Programming learning Recommended schools Recommended books Information dissemination Computer beginners Prospective engineers New engineers Prospective job seekers in the IT industry Support site
05-Python

Python(Part.26)| pythonの基礎【選択構造】(match文)(1)| The Basics of python [Selection Structure] (match statement)(1)

2024年5月8日
プログラミング学習 おすすめスクール おすすめ書籍情報発信 パソコン初心者 エンジニア希望者 新人エンジニア IT業界への就職・転職希望者 サポートサイト Programming learning Recommended schools Recommended books Information dissemination Computer beginners Prospective engineers New engineers Prospective job seekers in the IT industry Support site
05-Python

Python(Part.2)| pythonの開発環境と実行 | 現役エンジニア&プログラミングスクール講師

2023年6月24日
プログラミング学習 おすすめスクール おすすめ書籍情報発信 パソコン初心者 エンジニア希望者 新人エンジニア IT業界への就職・転職希望者 サポートサイト Programming learning Recommended schools Recommended books Information dissemination Computer beginners Prospective engineers New engineers Prospective job seekers in the IT industry Support site