5. Loops#
Loops are a way of repeating commands over and over again.
A while loop is an indefinite loop. It runs indefinitely until a certain condition is met. Be careful with while loops. If the condition is never met, the while loop will try to run forever.
import numpy as np
i = 0
while i<5:
i = i + 1
print(i)
print('done with while loop')
1
2
3
4
5
done with while loop
A for loop is a definite loop. It iterates over a list, array or other variable types that are groups of values or other variables. It definitely stops when it gets to the end of the list.
name_list = ['Bob','Jane','Mary']
for name in name_list:
print("Hello " + name)
print('done')
Hello Bob
Hello Jane
Hello Mary
done
An example of looping through a sequence of numbers created with the np.arange()
function.
import numpy as np
print(np.arange(5))
[0 1 2 3 4]
print(np.arange(2,5))
[2 3 4]
print(np.arange(2,5,0.5))
[2. 2.5 3. 3.5 4. 4.5]
for i in np.arange(5):
print(i*2)
0
2
4
6
8
Exercise:
Write a for loop that prints out the cumulative sum of an array.
For example, the cumulative sum of the array
[1,3,6,4,7]
would be:
[1,4,10,14,21]
Write out your algorithm first. An algorithm is a process or set of rules followed in calculations or problem solving. It helps to write or sketch these instructions first, then translate the instructions to Python. This strategy helps break a coding problem up into smaller, more manageable parts.
Use the following code as a template (replace the dashes —– with code).
x = [1,3,6,4,7]
cumsum = 0
for val in x:
cumsum = -------
print(cumsum)
This should produce the output:
1
4
10
14
21
done
5.1. For loop - cumulative sum 2#
Write a for loop that stores the cumulative sum of an array in another array. Use the np.append()
function - see help(np.append)
- and make sure that you have run import numpy as np
at the top of your program in order to use this function.
x = [1,3,6,4,7]
cumsum = 0
cumsum_array = []
for val in x:
cumsum = -------
cumsum_array = np.append(------,--------)
print(cumsum_array)
This should produce the output:
[ 1. 4. 10. 14. 21.]
5.2. Fibonacci sequence (if time)#
Write a for loop that prints the first n numbers of the Fibonacci sequence: 1,1,2,3,5,8,13… Write out your algorithm first, and start with the following minimal code.
n = 10
# insert code here