{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Loops and Conditionals \n", "\n", "### Other resources\n", "\n", "Python course at https://codecademy.com\n", "\n", "Python flow control tutorial https://docs.python.org/3/tutorial/controlflow.html " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `range()` command offers an easy way to loop over a sequence of numbers. Remember that Python is 0 indexed." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for i in range(10):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also loop over anything that is iterable, this includes lists, tuples, and dictonaries." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "alist = ['a', 'b', 'c']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the loop below `aletter` takes on each value of `alist` every cycle through the loop." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n" ] } ], "source": [ "for aletter in alist:\n", " print(aletter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are also `while`-loops, but the syntax is not as clear as `for`-loops and there is always a danger of entering an infinite loop." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "i=0\n", "mybool = False\n", "while mybool is not True:\n", " print(i)\n", " i += 1\n", " if i > 10:\n", " mybool = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I prefer `for`-loops with a fixed number of iterations and a `break` statement to replicate the logic of `while`-loops, but protecting from infinite cycles." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n", "11\n" ] } ], "source": [ "for i in range(100):\n", " print(i)\n", " \n", " if i > 10:\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is an example of a conditional statement with `and` or `or` logic." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "21\n", "22\n", "23\n", "24\n", "25\n", "26\n", "27\n", "28\n", "29\n", "30\n", "31\n", "32\n", "33\n", "34\n", "35\n", "36\n", "37\n", "38\n", "39\n", "40\n", "41\n", "42\n", "43\n", "44\n", "45\n", "46\n", "47\n", "48\n", "49\n", "50\n", "51\n", "52\n", "53\n", "54\n", "55\n", "56\n", "57\n", "58\n", "59\n", "60\n", "61\n", "62\n", "63\n", "64\n", "65\n", "66\n", "67\n", "68\n", "69\n", "70\n", "71\n", "72\n", "73\n", "74\n", "75\n", "76\n", "77\n", "78\n", "79\n", "80\n", "81\n", "82\n", "83\n", "84\n", "85\n", "86\n", "87\n", "88\n", "89\n", "90\n", "91\n", "92\n", "93\n", "94\n", "95\n", "96\n", "97\n", "98\n", "99\n" ] } ], "source": [ "for i in range(100):\n", " \n", " if i < 10 or i > 20:\n", " print(i)\n", " else:\n", " print(\"Hello!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python offers a clean syntax for testing if an item is stored in a list, tuple, or dictionary." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True!!!!\n" ] } ], "source": [ "alist = ['a', 'b', 'c']\n", "\n", "if 'd' not in alist:\n", " print(\"True!!!!\")\n", "else:\n", " print(\"False!!!!\")" ] } ], "metadata": { "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.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }