[Python of Hikari-] Chapter 05-05 Control syntax (for statement-multiple loops-)

[Python] Chapter 05-05 for Statement-Multiple Loops-

In the for statement, I wrote the processing in the for statement after **: ** of for i in range (10) :. In fact, you can also write a for statement inside this for statement.

This time, I will touch on multiple loops, such as writing a for statement inside a for statement.

Multiple loops using for statement

When writing a double loop of for, you need to use different variables for the outer for and the inner for.

for variable 1 in repeat target:
for variable 2 in repeat target:
Contents of repeated processing

Let's look at it concretely. First, let's create a program that outputs a multiplication table, which is often explained in double loops. Create a file with the file name 05-05-01.py </ font> in chap05 </ font>, and use the following code Please write.

05-05-01.py


for i in range(1, 10):  #(1)
    for j in range(1, 10):  #(2)
        print(i*j, end='\t') #(3)  end='\t'Outputs tabs without line breaks
    print('') #(4)Process to break a line to go to the next stage


05-05-01.py Execution result

[Execution result] </ font> * Officially, the output is aligned. (Please note that it is slightly off on Qiita) </ font> 1 2  3  4  5  6  7  8  9  2 4  6  8  10 12 14 16 18  3 6  9  12 15 18 21 24 27  4 8  12 16 20 24 28 32 36  5 10 15 20 25 30 35 40 45  6 12 18 24 30 36 42 48 54  7 14 21 28 35 42 49 56 63  8 16 24 32 40 48 56 64 72  9 18 27 36 45 54 63 72 81

This time, I will explain using comment sentences (1) to (4).

** [1st loop (outside)] </ font> ** First of all, ** for i in range (1, 10): ** in (1), but the reason why I didn't set it to range (10) is that if I set it to range (10), it will start from 0. , Start is described as 1. (At this point, ** i = 1 **.)

** [1st loop (outside)] </ font> [1st loop (inside)] </ font> ** Next, enter the contents of (2) ** for i in range (1, 10): **. Immediately you will see ** for j in range (1, 10): **. (At this point, ** i = 1, j = 1 </ font> **.)

Next, ** i * j ** is calculated in the print function of (3). And it is output as it is, at this point ** i = 1, j = 1 , so the output result will be 1 </ font>. Since ** end ='\ t' is specified in the print function, the next value is output to the right without line breaks. Then return to (2).

** [1st loop (outside)] </ font> [2nd loop (inside)] </ font> ** It is the processing of ** for j in range (1, 10): ** in (2). (At this point, ** i = 1, j = 2 </ font> **.)

Next, ** i * j ** is calculated in the print function of (3). And it is output as it is, at this point ** i = 1, j = 2 , so the output result will be 2 </ font>. Since ** end ='\ t' is specified in the print function, the next value is output to the right without line breaks. Then return to (2).

** [1st loop (outside)] </ font> [3rd loop (inside)] </ font> ** It is the processing of ** for j in range (1, 10): ** in (2). (At this point, ** i = 1, j = 3 </ font> **.)

Next, ** i * j ** is calculated in the print function of (3). And it is output as it is, at this point ** i = 1, j = 3 , so the output result will be 3 </ font>. Since ** end ='\ t' is specified in the print function, the next value is output to the right without line breaks. Then return to (2).

Repeat until <font color = # 0cc> <j = 9. > </ font>

** [1st loop (outside)] </ font> [9th loop (inside)] </ font> ** It is the processing of ** for j in range (1, 10): ** in (2). (At this point, ** i = 1, j = 9 </ font> **.)

Next, ** i * j ** is calculated in the print function of (3). And it is output as it is, at this point ** i = 1, j = 9 , so the output result will be 9 </ font>. Since ** end ='\ t' is specified in the print function, the next value is output to the right without line breaks. Then return to (2).

And since it is beyond the range of range (10), it exits ** for j </ font> in range (1, 10): **. (Note that it has not been removed from *** i ** yet) Then, start a new line with print ('') in (4). This is because the multiplication table 1st stage is over and it moves to the 2nd stage.

** [2nd loop (outside)] </ font> ** Then, it returns to ** for i </ font> in range ( 1 </ font>, 10): ** in (1). (At this point, ** i = 2, j = 9 **.)

** [2nd loop (outside)] </ font> [1st loop (inside)] </ font> ** Next, enter the contents of (2) ** for i in range (1, 10): **. Immediately you will see ** for j in range (1, 10): **. (At this point, ** i = 2, j = 1 </ font> **.)

Next, ** i * j ** is calculated in the print function of (3). And it is output as it is, at this point ** i = 2, j = 1 , so the output result will be 2 </ font>. Since ** end ='\ t' is specified in the print function, the next value is output to the right without line breaks. Then return to (2).

<font color = # 0cc> <After that, repeat until i = 9, j = 9. > </ font>

Then, when you get out of the double loop, the process ends.

Finally

As I explained, multiple loops are quite complicated, but in the end, the number of processes has increased, and it seems that you can understand it if you follow them in order. This double-loop program has been cut off in the middle, but please follow the process. By following the program on your desk, you will be able to use it as a power for algorithm-related problems that are asked in the Fundamental Information Technology Engineer Examination, so please try it.

Return to [Table of Contents Link]

Recommended Posts