forループ(forloops)

Regular loop

e = []
for i in range(11):
  e.append(i**2)
e
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

リストによるforループ(List loop)

e2 = ['A', 'B', 'C', 'D']
for item in e2:
    print(item,end=' ')
A B C D

リスト内包ループ(List comprehension loop)

e3 = [i**5 for i in range(11)]
e3
[0, 1, 32, 243, 1024, 3125, 7776, 16807, 32768, 59049, 100000]
[i**5 for i in range(11)]
[0, 1, 32, 243, 1024, 3125, 7776, 16807, 32768, 59049, 100000]

Double forloop

import numpy as np
import time

start_time = time.perf_counter()
nn = 1001
f = np.empty((nn,nn))
for i in range(nn):
    for j in range(nn):
        f[i,j] = i*j
end_time = time.perf_counter()
print(end_time - start_time)
0.1951509800000082