[Python of Hikari-] Chapter 05-04 Control syntax (for statement-use of range function-)

[Python] Chapter 05-04 for Statement-Using range Function-

Last time, I specified it as a list, took out the elements and output them, and calculated the sum. Let's look back a little. A program that finds the sum in a list.

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

for n in numL:
    sum += n

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

Right now, I have specified 5 elements in this list, but if there are 100 or 1000 elements in the list, it is difficult to specify each time. There is a ** range function ** as an easy way to specify such elements, so I will explain about it.

range function

Use the ** range function ** to repeat a specified range of numbers. Generally, it is as follows.

function Description
range(start, end, step) from startend-1Returns an object that represents the range up to. The width of each element is step. start and step are optional.

If you omit start and step and write ** range (10) **, it is the same as specifying ** range (0, 10, 1) **. In other words, range (10) is 10 numbers 0, 1, 2,…, 9.

I will actually write the program. Enter the following code from ** Python Console **.

>>> range(10)
range(0, 10)

If you use the range function, it will be written as range (0, 10) instead of a list. (Such a notation is sometimes called a ** iterable ** object notation.)

To list this, write as follows.

>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Also, if you specify start and step, it will be as follows.

>>> list(range(5, 20, 2))
[5, 7, 9, 11, 13, 15, 17, 19]

Since step is 2 and starts from 5, it is displayed by skipping one with an integer from 5 to 19.

Use of range function for for statement

Let's actually write the program. Create a file with the file name 05-04-01.py </ font> in chap05 </ font>, and use the following code Please write. ** The part of the print function is different from usual **, so please enter it carefully.

.05-04-01.py


for i in range(10):
    print(i, end=' ')


05-04-01.py Execution result

[Execution result] </ font> 0 1 2 3 4 5 6 7 8 9

When executed, a number from 0 to 9 will be displayed. In addition, although ** end = 10 **, the end is not 10 but 9 before that, so please be careful.

Also, ** end ='''** is described in the ** print function . If only ** print function ** is used, the output result will be broken and displayed vertically, but by setting ** end =''', a half-width space'' will be inserted instead of the line feed code. Become. (The default is a line feed code.)

Find the sum using the range function and the for statement

Now, let's consider a program that finds the total value of even numbers among the values between 1 and 100. Create a file with the file name 05-04-02.py </ font> in font color = "# 00cccc"> chap05 </ font>, and enter the following code Please write.

.05-04-01.py


sum = 0

for i in range(1, 101): #Execution in a for statement between 1 and less than 101 (100 or less)
    if i % 2 == 0:  #If i is even, add
        sum += i

print(sum)


05-04-02.py Execution result

[Execution result] </ font> 2550

If you follow the program, the range function assigns a value between 1 and 101 (100 or less) to i, and if it is divisible by 2, it is added to sum.

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-04-p1.py] [1] Create a program that calculates the sum of integer values from 1 to 100 using the range function. The result is 5050. [05-04-p2.py] [2] Create a program that calculates the sum of odd-numbered integer values from 1 to 100 using the range function. The result is 2500. [05-04-p3.py] [3] Enter an integer value n, and create a program to find out how many numbers are left over by dividing an integer less than n by 3. (Hint: set start to 1 when using the range function, and repeat n times.)
05-04-p3.py Execution result

[Execution result] </ font> Enter an integer greater than or equal to 1:10 An integer from 1 to less than 10, and when divided by 3, the remaining number is 3.

Return to [Table of Contents Link]

Recommended Posts