Python Introduction | Statements | Repeating Values in Order with for

Python has another looping statement: for. Use for to process many values in order.

Basic for Statement 1

for variable in values:
    repeated process ......

Basic for Statement 2

for variable in values:
    repeated process ......
else:
    process after the loop

Programming languages provide ways to group many values together. A for statement is designed for iterating over such collections. It retrieves each available value in order and performs a process for each one.

The following example rewrites the while loop from the previous article as a for loop.

x = 100
total = 0
for n in range(1, x + 1): 
    total = total + n 
else: 
    print(str(x) + "까지의 합계는 " + str(total)) 
print("....end.")

The range(...) function creates a collection of the numbers from 1 through x. The loop retrieves the numbers 1, 2, 3, ... 100 one at a time and adds them to total.

The important question is what a collection of many values is. It is generally called an array. The following articles explain arrays and related features.