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