<jupyter_start><jupyter_text>For loop exercises<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>You may remember you can print a number (or anything else) with the `print` function, like this:<jupyter_code>print(5)<jupyter_output>5<jupyter_text>Use a `for` loop to print out all the numbers from 3 through 7, one number on each line.<jupyter_code># Your code here.<jupyter_output><empty_output><jupyter_text>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.<jupyter_code># Your code here.
my_array = np.array([])<jupyter_output><empty_output><jupyter_text>Make a new variable `total` equal to 0. Use a `for` loop to add all thenumbers 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`.<jupyter_code># Your code here.
total = 0<jupyter_output><empty_output><jupyter_text>Use a `for` loop to add up all the even numbers from -102 through 98.Hint: you may like to use the `step` argument.<jupyter_code># Sum of all even numbers from -102 through 98.
total = 0<jupyter_output><empty_output><jupyter_text>Have a look at the definition of the[factorial](https://en.wikipedia.org/wiki/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 pointvalue \- as in `factorial = 1.0` \- in order to avoid a nastyinteraction between Numpy, Windows, and integers.<jupyter_code># Calculate 15!
# Note the floating point number to start.
factorial = 1.0<jupyter_output><empty_output><jupyter_text>These are getting a bit harder. You can break out of a `for` loop using the `break` statement.  HereI break out of the `for` loop, when I get to 6.  Notice I never get to7 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".<jupyter_code>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.")<jupyter_output>0
1
2
3
4
5
6
Stopping here, where i == 6
I have finished the for loop now.<jupyter_text>Make sure you understand what is going on in the cell above.  When youdo, 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$ isless than 100.<jupyter_code>factorial = 1
last_factorial = 1
threshold = 1000000
# Your code here<jupyter_output><empty_output><jupyter_text>Here is an array of 50 numbers:<jupyter_code># 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])<jupyter_output><empty_output><jupyter_text>Add up all the numbers in this array, until you get to the first valueof 999.  Print the sum of all the values up to, but not including, thefirst 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?<jupyter_code># Your code here.<jupyter_output><empty_output><jupyter_text>The next cell is a slight variation.  Add all the numbers in thisarray, 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?<jupyter_code># Your code here.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10442477876106195, 'fraction_numerical': 0.05056890012642225, 'mean_word_length': 4.104516129032258, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 47, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '261737', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1333, 'n_words': 559, 'path': 'notebooks/exercises/for_loops.ipynb', 'repo_name': 'matthew-brett/cfd-uob', 'size': '3955'}