2.3. for loops#
2.3.1. Learning goals#
2.3.1.1. For loops in python#
Loops are a common structure in coding/scripting.
for i in range(10):
print(i)
0
1
2
3
4
5
6
7
8
9
Loops are often used to compute sums. For example if we want to know \(\sum_{i=0}^{10} i\)
partialSum = 0
for i in range(11):
partialSum += i
print(i, partialSum)
print("Final sum is:", partialSum)
0 0
1 1
2 3
3 6
4 10
5 15
6 21
7 28
8 36
9 45
10 55
Final sum is: 55
Loops are often used to compute sums. For example if we want to know \(\sum_{i=1}^{10} i\)
partialSum = 0
for i in range(1,11):
partialSum += i
print(i, partialSum)
print("Final sum is:", partialSum)
1 1
2 3
3 6
4 10
5 15
6 21
7 28
8 36
9 45
10 55
Final sum is: 55