For loop exercises¶
import numpy as np
You may remember you can print a number (or anything else) with the print
function, like this:
print(5)
5
Use a for
loop to print out all the numbers from 3 through 7, one number on each line.
# Your code here.
Make an empty array called my_array
. Use a for
loop to append all the numbers between 0 and 10 (inclusive) to my_array
. Show the array at the end of the loop.
# Your code here.
my_array = np.array([])
Make a new variable total
equal to 0. Use a for
loop to add all the
numbers from 15 through 32 to total
. Print the value at the end of the loop.
Hint - here is a statement where I add 10 to the variable
my_variable
: my_variable = my_variable + 10
.
# Your code here.
total = 0
Use a for
loop to add up all the even numbers from -102 through 98.
Hint: you may like to use the step
argument.
# Sum of all even numbers from -102 through 98.
total = 0
Have a look at the definition of the factorial.
For example, the factorial of 5, written \(5!\) is 1 * 2 * 3 * 4 * 5
.
Use a for
loop to calculate \(15!\). Print out the result.
Note: those of you on Windows will have to start with a floating point
value - as in factorial = 1.0
- in order to avoid a nasty
interaction between Numpy, Windows, and integers.
# Calculate 15!
# Note the floating point number to start.
factorial = 1.0
These are getting a bit harder.
You can break out of a for
loop using the break
statement. Here
I break out of the for
loop, when I get to 6. Notice I never get to
7 or any number higher than 6. The break
statement says, “stop the
for
loop now, and go directly to the first statement after the for
loop”.
for i in np.arange(1000):
print(i)
if i == 6:
print("Stopping here, where i == 6")
break
print("I have finished the for loop now.")
0
1
2
3
4
5
6
Stopping here, where i == 6
I have finished the for loop now.
Make sure you understand what is going on in the cell above. When you
do, try using this technique to find the largest integer \(n\) where \(n!
< 10^6\). Print out \(n\) and \(n!\). Hint for your for
loop: \(n\) is
less than 100.
factorial = 1
last_factorial = 1
threshold = 1000000
# Your code here
Here is an array of 50 numbers:
# Run this cell to define the "values" array
values = np.array([ 3, 32, 39, -3, 34, 28, 9, 36,
-4, 20, -4, 13, 32, 9, 14, 999, 2, 20, 18,
12, 13, 25, 25, 2, 17, 39, 39, 4, 26, 7,
1, 36, 31, 15, 25, 19, 999, -4, -3, 24, 7,
14, -2, 35, 18, 23, 34, 14, 11, 25])
Add up all the numbers in this array, until you get to the first value
of 999. Print the sum of all the values up to, but not including, the
first 999. For example, if the array was np.array([2, 6, 4, 999, 11])
, then the result would be: 2 + 6 + 4 == 12
.
What is the equivalent result for the values
array above?
# Your code here.
The next cell is a slight variation. Add all the numbers in this
array, up to, but not including, the first value of 999. This time,
discard any negative values you find. For example, if the array was
np.array([1, 7, -3, 4, 999, 13])
, then the result would be:
1 + 7 + 4 == 12
.
What is the equivalent result for the values
array above?
# Your code here.