---
jupyter:
  jupytext:
    text_representation:
      extension: .Rmd
      format_name: rmarkdown
      format_version: '1.0'
      jupytext_version: 0.8.5
  kernelspec:
    display_name: Python 3
    language: python
    name: python3
  language_info:
    codemirror_mode:
      name: ipython
      version: 3
    file_extension: .py
    mimetype: text/x-python
    name: python
    nbconvert_exporter: python
    pygments_lexer: ipython3
    version: 3.7.1
---

# For loop exercises

```{python}
import numpy as np
```

You may remember you can print a number (or anything else) with the `print` function, like this:

```{python}
print(5)
```

Use a `for` loop to print out all the numbers from 3 through 7, one number on each line.

```{python}
# Your code here.
```

<!---
Do not use append 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.

```{python}
# 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`.

```{python}
# 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.

```{python}
# Sum of all even numbers from -102 through 98.
total = 0
```

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 point
value \- as in `factorial = 1.0` \- in order to avoid a nasty
interaction between Numpy, Windows, and integers.

```{python}
# 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".

```{python}
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.")
```

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.

```{python}
factorial = 1
last_factorial = 1
threshold = 1000000
# Your code here
```

Here is an array of 50 numbers:

```{python}
# 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?

```{python}
# 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?

```{python}
# Your code here.
```
