[Python of Hikari-] Chapter 05-07 Control syntax (conditional branching of comprehension notation)

[Python] Chapter 05-07 Conditional branching of comprehensions

First, take a look at the program below (no need to write a program)

L = []  #Create an empty list L
print(L)  #Output element of L

for i in range(10):
    if i % 3 == 0:  #When the remainder divided by 3 is 0
         L.append(i)  #Append element to list L with append method

print(L)

[Execution result] </ font> [] [0, 3, 6, 9]

Briefly, an empty list ** L ** is created, and the elements of the empty list are output once with the ** print function **. (Of course it's empty) Then, move to the for statement, but if i is a multiple of 3 (that is, the remainder after dividing by 3 is 0), use the ** append method ** to add ** L ** elements one by one. I will continue to do it.

Finally, the print function outputs the contents of the element. However, the process including this if statement can also be described in the comprehension notation.

Conditional branching by inclusion notation

Now, let's express the above program in a list comprehension.

[Calculation result by variable for variable in for repeat target if condition]

Let's describe the same processing content as the above program in the comprehension notation. This time, enter the following code from ** Python Console **.

>>> [i for i in range(10) if i % 3 == 0]
[0, 3, 6, 9]

By writing a condition with an if statement after the inclusion notation in Chapter 05-06 earlier, it becomes a conditional branch of the inclusion notation. **: ** (colon) is not required.

Also, let's improve the "calculation result by variable" part a little. Now let's find the value of 1 or more and 100 ** or less ** that is a multiple of 10 multiplied by 3. Enter the following code from the ** Python Console **.

>>> [i*3 for i in range(1, 101) if i % 10 == 0]
[30, 60, 90, 120, 150, 180, 210, 240, 270, 300]

Let's look at another conditional branch of comprehension notation. Now let's find the value of 10 or more and less than 50 ** ** whose squared value is less than 200. Enter the following code from the ** Python Console **.

>>> [i for i in range(10, 50) if i**2 < 200]
[10, 11, 12, 13, 14]

Sure, if you square the elements in the above list, you'll see that they're all less than 200.

And one more thing, you can also sum the results from the list above. Use the ** sum function ** as follows.

>>> sum([i for i in range(10, 50) if i**2 < 200])
60

Practice problem

We have prepared exercises. Please try to solve it. Please use ** Python Console **. [1] Use the ** for statement ** to create a program that calculates the total value of only odd numbers among integers between 1 and 100. The total will be 2500.

Please answer the questions in [2] and [1] using the inclusion notation. [3] For numbers between 1 and 100, create a list of multiples of 5 using the inclusion notation, and calculate the average value of the elements in that list. (Hint: use the len function)

Finally

You can also write conditional branches in comprehensions. Furthermore, although I didn't mention it last time, you can calculate the total or average of the results by using the comprehension notation, so let's learn a simple method.

Return to [Table of Contents Link]

Recommended Posts