{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# An Introduction to Python\n", "## Loops\n", "\n", "### This notebook contains the programming exercises for [An Introduction to Python: Loops.](https://www.youtube.com/watch?v=NoVoGis_v2M)\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Exercises\n", "## Do the following problems." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Jupyter Notebook Shortcuts:\n", "\n", "There are two modes when a cell is highlighted. \n", "\n", "#### Command Mode: Press ESC to activate. The cell has a blue border if this mode is active. In this mode, you can add, delete, create, copy and paste cells.\n", "\n", "- create a new cell above the current cell: a\n", "- create a new cell below the current cell: b\n", "- delete the current cell: dd\n", "- change the current cell’s type to “Code”: y\n", "- change the current cell’s type to “Markdown”: m\n", "\n", "#### Edit Mode: Press ENTER to activate. The cell has a green border if this mode is active. In this mode, you can edit and type text into the cell.\n", "\n", "- execute the current cell and create a new cell: SHIFT + ENTER" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Provide an equivalent Python range() expression for each of the following integer sequences.\n", "(a) 1,2,3,4,5\n", "\n", "(b) 5,4,3,2,1\n", "\n", "(c) 5,10,15,20,25,30 \n", "\n", "(d) 30,25,20,15,10,5 \n", "\n", "(e) Empty sequence" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Print out the message \"Hello!\" 10 times separated by spaces. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Write a for loop that prints out all even numbers between 1 and 24 separated by spaces. Use python's modulo (%) operator" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Write a for loop that prints out the numbers in descending order from 20 to 1 separated by spaces. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Write a while loop to print out all multiples of 3 between 3 and 30 separated by spaces. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Rewrite the following code fragment so it eliminates the continue statement. Your new code’s logic should be simpler than the logic of this fragment. \n", "\n", "```python\n", "x = 5\n", "while x > 0:\n", " y = int(input())\n", " if y == 25:\n", " continue\n", " x -= 1\n", " print('x =', x)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Suppose we like to generate the following sequence of numbers: Given a positive starting number $n$. \n", "\n", "#### - If the current term is even, the next term is the current term divided by 2.\n", "#### - If the current term is odd, the next term is the $3$ times the current term plus 1.\n", "\n", "#### Generate the sequence until the sequence reaches a $1$. The famous **Collatz Conjecture** says that this sequence will always reach 1 regardless of the initial starting value.\n", "\n", "Example: \n", "n = 12, 6, 3, 10, 5, 16, 8, 4, 2, 1.\n", "\n", "#### Write a segment of code that ask the user to input a positive integer `n` and use a while loop that generates the Collatz sequence." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Write nested loops that generate the following:\n", "\n", "a) \n", "\n", "2 2 2 2\n", "\n", "4 4 4 4\n", "\n", "6 6 6 6\n", "\n", "8 8 8 8\n", "\n", "b)\n", "\n", "1 2 3 4 5\n", "\n", "1 2 3 4\n", "\n", "1 2 3\n", "\n", "1 2 \n", "\n", "1\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use the code above for generating the Collatz sequence and write a segment of code that prints out the Collatz sequence for each number from 1-10." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "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.6.4" } }, "nbformat": 4, "nbformat_minor": 1 }