diff --git a/sum23/lecture_materials/06_Iteration1/06_Iteration-1.pdf b/sum23/lecture_materials/06_Iteration1/06_Iteration-1.pdf new file mode 100644 index 0000000000000000000000000000000000000000..5174dfb2a1d1cb1a6b9d7c1bb05ac74b57a8debd Binary files /dev/null and b/sum23/lecture_materials/06_Iteration1/06_Iteration-1.pdf differ diff --git a/sum23/lecture_materials/06_Iteration1/lec_06_Iteration_1_template.ipynb b/sum23/lecture_materials/06_Iteration1/lec_06_Iteration_1_template.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..154d227869a5f7516c0dbabcbd0ef4f8a191a27b --- /dev/null +++ b/sum23/lecture_materials/06_Iteration1/lec_06_Iteration_1_template.ipynb @@ -0,0 +1,425 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "id": "dd1c3a1b", + "metadata": {}, + "source": [ + "# Iteration 1\n", + "\n", + "## Readings:\n", + "\n", + "- Chapter 7 of Think Python\n", + "- Chapter 6.1 to 6.3 of Python for Everybody" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "595d2e5b", + "metadata": {}, + "source": [ + "## Learning Objectives:\n", + "\n", + "- Implement an iterative algorithm using a `while` loop, for\n", + " - printing / counting\n", + " - validating user input\n", + " - performing an iterative calculation\n", + " - printing character art\n", + "\n", + "- Trace iterative algorithms and determine their output\n", + "\n", + "- Recognize common `while` loop errors\n", + " - Infinite loops (when unintentional)\n", + " - Off-by-one mistakes in the loop control variable" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c30a8ea2", + "metadata": {}, + "outputs": [], + "source": [ + "# import statements\n", + "\n", + "import time\n", + "import math" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "f113fc7b", + "metadata": {}, + "source": [ + "### Example 0: Simple countdowns" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "73959e77", + "metadata": {}, + "source": [ + "**How to termination infinite loop in:**\n", + "- jupyter: Kernel > Interrupt (fix and then re-run)\n", + "- script mode / interactive mode: Ctrl + C (Kill signal)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cb8de263", + "metadata": {}, + "outputs": [], + "source": [ + "#TODO: Copy/paste this example into PythonTutor\n", + "#Count from 0 to 3, printing each number\n", + "count = 0\n", + "\n", + "while count <= 3:\n", + " print(count)\n", + " count += 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "23dbc9da", + "metadata": {}, + "outputs": [], + "source": [ + "#TODO: Copy/paste this example into PythonTutor\n", + "#Count from 3 to -3, printing each number\n", + "count = 3\n", + "\n", + "while count >= -3:\n", + " print(count)\n", + " count -= 1" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "115e8742", + "metadata": {}, + "source": [ + "### Example 1: Countdown timer alarm" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "42f4a48f", + "metadata": {}, + "outputs": [], + "source": [ + "# TODO: use input function to get user input for number of seconds\n", + "start = ???\n", + "\n", + "# TODO: copy start into another variable\n", + "remaining = ???\n", + "while ???: # TODO: iterate from start to 1\n", + " print(remaining, \"seconds left\")\n", + " # TODO: update loop control variable's value to make progress towards terminating \n", + " # the loop, that is turning loop condition to False\n", + " remaining -= ???\n", + " # TODO: now run the cell to see the output. Didn't it go too fast?\n", + " # TODO: call time module sleep function, by passing 1 as argument\n", + "\n", + "# TODO: print \"BEEP BEEP BEEP ...\" (10 BEEPS) without typing BEEP 10 times\n", + "# What string operator can you use here?\n", + "\n", + "\n", + "# wake up call" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "d54200ad", + "metadata": {}, + "source": [ + "## `for` loop\n", + "\n", + "- another kind of loop\n", + "- does not require initialization of loop control variable outside the loop\n", + "- loop statement itself creates the loop control variable\n", + "- keywords `for` and `in`\n", + "\n", + "### range built-in function\n", + "- accepts a single integer argument and produces a sequence of numbers from 0 to argument - 1, that is argument is exclusive\n", + "- accepts two integer arguments and produces a sequence of numbers from start (argument1) to end (argument2) - 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "43776615", + "metadata": {}, + "outputs": [], + "source": [ + "for i in range(5): # single arugment -> produces 0, 1, 2, 3, and 4\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e3f01e6f", + "metadata": {}, + "outputs": [], + "source": [ + "for i in range(5, 10): # two arguments -> produces 5, 6, 7, 8, and 9\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b37a6842", + "metadata": {}, + "outputs": [], + "source": [ + "# TODO: write a for loop to iterate over the numbers 2 to 8\n" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "558e4bed", + "metadata": {}, + "source": [ + "### Example 2: Print the square of all positive numbers <= 5\n", + "\n", + "First, we show the code for how to do this with a while loop. Then, we'll work together to do the same thing with a for loop." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5ec1ba4f", + "metadata": {}, + "outputs": [], + "source": [ + "x = 1\n", + "while x <= 5:\n", + " print(str(x) + \" squared is:\")\n", + " print(str (x ** 2))\n", + " x += 1\n", + "print(\"all done!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1de3a188", + "metadata": {}, + "outputs": [], + "source": [ + "# TODO write a function using a for loop that prints the square of all positive numbers <= 5\n", + "# the output should be identical to the output of the cell above\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9df8c834", + "metadata": {}, + "outputs": [], + "source": [ + "# TODO what value does x have after the for loop finishes? What about after the while loop finishes?\n" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "cdc66ffa", + "metadata": {}, + "source": [ + "### Example 3: Find the max value of a function on an interval\n", + "\n", + "<div>\n", + "<img src=\"attachment:Curve_peak.png\" width=\"600\"/>\n", + "</div>" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2ec27141", + "metadata": {}, + "outputs": [], + "source": [ + "def f(x):\n", + " return 5 - (x - 2) ** 2\n", + " \n", + "print(f(1))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0098b6aa", + "metadata": {}, + "outputs": [], + "source": [ + "# TODO: for what value of x will f(x) produce the maximum y value?\n", + "print(f(???))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27298992", + "metadata": {}, + "outputs": [], + "source": [ + "# Goal: find the x that maximizes the y = f(x)\n", + "\n", + "# Let's try the values from -5 to 5\n", + "\n", + "# Goal: after the loop, best_x and best_y should contain just that\n", + "\n", + "# Try out increasing increments, make sure to comment the other increment\n", + "# delta_x = 1\n", + "# delta_x = 0.1\n", + "# delta_x = 0.01\n", + "# delta_x = 0.001\n", + "\n", + "\n", + "\n", + "print(\"Best x:\", best_x)\n", + "print(\"Best y:\", best_y)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "249f2aa7", + "metadata": {}, + "source": [ + "### Example 4: Integration (Riemann Sum)\n", + "\n", + "<div>\n", + "<img src=\"attachment:ReimannSum.png\" width=\"600\"/>\n", + "</div>" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "96b98336", + "metadata": {}, + "outputs": [], + "source": [ + "# Let's try the values from 1 to 5\n", + "start_x = 1\n", + "end_x = 5\n", + "total_area = 0\n", + "current_x = start_x\n", + "# Try out increasing values of width, make sure to comment the other width values\n", + "# delta_x = 1\n", + "delta_x = 0.1\n", + "# delta_x = 0.01\n", + "# delta_x = 0.001\n", + "\n", + "while current_x <= end_x:\n", + " y = ??? # TODO: use f(x) defined previously\n", + " rect_area = ???\n", + " total_area += ???\n", + " current_x += delta_x\n", + " \n", + "print(\"Area found using approximation is:\", total_area)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "3e8d609b", + "metadata": {}, + "source": [ + "### Example 5: Find primes" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f2d0c381", + "metadata": {}, + "outputs": [], + "source": [ + "def is_prime(num):\n", + " \"\"\" returns True if x is prime, false otherwise. Assumes x is positive\"\"\"\n", + " \n", + " # try all divisors from 2 to sqrt(num) to check if num is prime\n", + " divisor = 2\n", + " while ???:\n", + " # check if num is divisible by divisor\n", + " if num % divisor == 0:\n", + " return False\n", + " divisor ???\n", + " \n", + " return True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e7aea11c", + "metadata": {}, + "outputs": [], + "source": [ + "print(is_prime(1))\n", + "print(is_prime(2))\n", + "print(is_prime(3))\n", + "print(is_prime(7))\n", + "print(is_prime(16))\n", + "print(is_prime(23))\n", + "print(is_prime(1000000))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d26d790c", + "metadata": {}, + "outputs": [], + "source": [ + "print(\"Prime numbers:\")\n", + "number = 2\n", + "# TODO: comment out this while loop and write equivalent for loop using range\n", + "while number <= 50: \n", + " if is_prime(number):\n", + " print(number, \"is prime\")\n", + " else:\n", + " print(number, \"is not prime\")\n", + " number += 1" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}