[Python of Hikari-] Chapter 05-03 Control syntax (for statement-extracting elements from list-)

[Python] Chapter 05-03 for Statement-Retrieving Elements from List-

As with any program, there are basically the following processing formats.

--Sequential processing --Control (branch) --Repeat

Sequential processing is processing such as "assignment" and "addition", and control is branch processing by "if statement". This time I would like to touch on this ** repeat **.

There are various types of iterative processing, but let's first look at the ** for statement **.

for statement-Retrieving from list-

First, let's look at the process of extracting elements from the list.

The for statement is described as follows.

for variable in repeat target:
Processing content

Basically, the list will be listed in the "Repeat target" section. Repeat within the for statement for the number of ** elements in this list **. Let's actually write a program and check it. In chap05 </ font>, create a file with the file name 05-03-01.py </ font>, and the following code Please write.

.05-03-01.py


TohokuL = ['Aomori', 'Iwate', 'Akita', 'Miyagi', 'Yamagata', 'Fukushima']

for i in TohokuL:
    print(i)
    
print('I got out of the For statement.')


05-03-01.py Execution result

[Execution result] </ font> Aomori Iwate Akita Miyagi Yamagata Fukushima I got out of the For statement.

I will follow in order. First, TohokuL = ['Aomori', 'Iwate', 'Akita', 'Miyagi', 'Yamagata', 'Fukushima'] I created a list ** TohokuL ** in, which I explained in the list.

Next, go to the contents of for. This time, we will repeat the 6 elements of ** Tohoku L **. This means that the ** loop will be repeated 6 times **.

** [1st loop] </ font> ** The ** 0th ** of ** Tohoku L **,'Aomori', is processed. This'Aomori'is assigned to a variable called ** i **. Then, the processing in the for statement is performed as it is, and'Aomori', which is the content of ** i **, is output. Then, as it is, it returns to ** for statement **.

** [2nd loop] </ font> ** The ** first ** of ** Tohoku L **,'Iwate', is processed. As before,'Iwate' is assigned to the variable ** i **. Then, the processing in the for statement is performed, and'Iwate', which is the content of ** i **, is output. Then, as it is, it returns to the ** for statement **.

** [3rd loop] </ font> ** The ** second ** of ** Tohoku L **,'Akita', is processed. As before,'Akita' is assigned to the variable ** i **. Then, the processing in the for statement is performed, and'Alita', which is the content of ** i **, is output. Then, as it is, it returns to ** for statement **.

** Repeat this for up to 6 loops. ** When the 6th loop is finished, the process of the for statement is exited for the first time, and the subsequent processing (this time, "Exited from the For minute" is output) is performed.

* </ font> By the way, it is output even if it is executed by tuple instead of list.

Calculation of sum by for statement

As a basic problem of the for statement, consider finding the sum of the integers in the list. For example, if you have a list of integers numL = [5, 8, 9, 1, 2], create a program that calculates the sum and outputs 25. Create a file with the file name 05-03-02.py </ font> in chap05 </ font>, and use the following code Please write.

sum = 0
numL = [5, 8, 9, 1, 2]

for n in numL:
    sum += n

print(f'{numL}Sum of list elements of:{sum}')


05-03-02.py Execution result

[Execution result] </ font> Sum of list elements in [5, 8, 9, 1, 2]: 25

First, about ** sum = 0 **. Sum is calculated for this ** sum variable **. However, when ** sum ** suddenly appears, the contents of the ** sum ** variable are unknown. Therefore, initialize it with 0. (At this point ** sum = 0 **)

Next, I am creating a list. ** numL = [5, 8, 9, 1, 2] **.

Then, it starts processing the for statement.

** [1st loop] </ font> ** First, ** numL ** retrieves the 0th element ** 5 ** and assigns it to ** n . And ** sum + = n ** is ["Chapter 02-01"](https://qiita.com/ko0821/items/8355b4e192aa76a0e8ae#%E4%BB%A3%E5%85%A5%E6%BC % 94% E7% AE% 97% E5% AD% 90% E3% 81% AB% E3% 81% A4% E3% 81% 84% E3% 81% A6) This is the assignment operator described above. In other words, " sum + = n " is the same as " sum = sum + n **". Therefore, the result of adding ** 5 ** to ** sum ** is assigned to ** sum **, and the value of the result ** sum ** is ** 5 **. sum = 0 + 5 Then, as it is, it returns to ** for statement **. (At this point ** sum = 5 **)

** [2nd loop] </ font> ** ** numL ** retrieves the first element ** 8 ** and assigns it to ** n **. Then, the result of adding ** 8 ** to ** sum ** is assigned to ** sum **, and the value of the result ** sum ** becomes ** 13 **. sum = 5 + 8 Then, as it is, it returns to ** for statement **. (At this point ** sum = 13 **)

** [3rd loop] </ font> ** ** numL ** retrieves the second element ** 9 ** and assigns it to ** n **. Then, the result of adding ** 9 ** to ** sum ** is assigned to ** sum **, and the value of the result ** sum ** becomes ** 22 **. sum = 13 + 9 Then, as it is, it returns to ** for statement **. (At this point ** sum = 22 **)

** [4th loop] </ font> ** ** numL ** retrieves the third element ** 1 ** and assigns it to ** n **. Then, the result of adding ** 1 ** to ** sum ** is assigned to ** sum **, and the value of the result ** sum ** becomes ** 23 **. sum = 22 + 1 Then, as it is, it returns to ** for statement **. (At this point ** sum = 23 **)

** [5th loop] </ font> ** ** numL ** retrieves the fourth element, ** 2 **, and assigns it to ** n **. Then, the result of adding ** 2 ** to ** sum ** is assigned to ** sum **, and the value of the result ** sum ** becomes ** 25 **. sum = 23 + 2 Then, as it is, it returns to ** for statement **. (At this point ** sum = 25 **)

Then you can get out of the for statement. The result is output as ** 25 **.

Practice problem

We have prepared exercises. Please try to solve it. In addition, please use the file name specified in [] and create it in chap05 </ font>. You can specify any variable name you like. [05-03-p1.py] [1] Set the list of numL = [3, 5, 6, 10, 7, 8] and create a program to calculate the average value. [05-03-p2.py] [2] Set the list of numL = [3, 5, 6, 10, 7, 8] and create a program that outputs only odd numbers. (Hint: use if statement)

Finally

Repeat is a program that appears everywhere like the if statement. Especially in the Information-Technology Engineers Examination, algorithm-related problems will always appear, so be sure to keep them in mind.

Return to [Table of Contents Link]

Recommended Posts