{"nbformat_minor": 0, "metadata": {"name": "", "signature": "sha256:21f53606fe7bec33745cfb971d168e8bd9b7a8d3f71872659c2b4db9f27d0a0a"}, "worksheets": [{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["`for` loops in Python always iterate over something list-like:"]}, {"language": "python", "cell_type": "code", "input": ["for i in range(10):\n", " print(i)"], "metadata": {}, "outputs": [{"text": ["0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n"], "output_type": "stream", "stream": "stdout"}], "collapsed": false, "prompt_number": 2}, {"cell_type": "markdown", "metadata": {}, "source": ["**Note** that Python does block-structuring by leading spaces.\n", "\n", "Also note the trailing \"`:`\"."]}, {"cell_type": "markdown", "metadata": {}, "source": ["---\n", "`if`/`else` are as you would expect them to be:"]}, {"language": "python", "cell_type": "code", "input": ["for i in range(10):\n", " if i % 3 == 0:\n", " print(\"{0} is divisible by 3\".format(i))\n", " else:\n", " print(\"{0} is not divisible by 3\".format(i))"], "metadata": {}, "outputs": [{"text": ["0 is divisible by 3\n", "1 is not divisible by 3\n", "2 is not divisible by 3\n", "3 is divisible by 3\n", "4 is not divisible by 3\n", "5 is not divisible by 3\n", "6 is divisible by 3\n", "7 is not divisible by 3\n", "8 is not divisible by 3\n", "9 is divisible by 3\n"], "output_type": "stream", "stream": "stdout"}], "collapsed": false, "prompt_number": 3}, {"cell_type": "markdown", "metadata": {}, "source": ["`while` loops exist too:"]}, {"language": "python", "cell_type": "code", "input": ["i = 0\n", "while True:\n", " i += 1\n", " if i**3 + i**2 + i + 1 == 3616:\n", " break\n", "\n", "print(\"SOLUTION:\", i)"], "metadata": {}, "outputs": [{"text": ["SOLUTION: 15\n"], "output_type": "stream", "stream": "stdout"}], "collapsed": false, "prompt_number": 5}, {"cell_type": "markdown", "metadata": {}, "source": ["----\n", "Building lists by hand can be a little long. For example, build a list of the squares of integers below 50 divisible by 7:"]}, {"language": "python", "cell_type": "code", "input": ["mylist = []\n", "for i in range(50):\n", " if i % 7 == 0:\n", " mylist.append(i**2)"], "metadata": {}, "outputs": [], "collapsed": false, "prompt_number": 6}, {"language": "python", "cell_type": "code", "input": ["mylist"], "metadata": {}, "outputs": [{"metadata": {}, "output_type": "pyout", "text": ["[0, 49, 196, 441, 784, 1225, 1764, 2401]"], "prompt_number": 7}], "collapsed": false, "prompt_number": 7}, {"cell_type": "markdown", "metadata": {}, "source": ["Python has a something called *list comprehension*:"]}, {"language": "python", "cell_type": "code", "input": ["mylist = [i**2 for i in range(50) if i % 7 == 0]"], "metadata": {}, "outputs": [], "collapsed": false, "prompt_number": 8}, {"language": "python", "cell_type": "code", "input": ["mylist"], "metadata": {}, "outputs": [{"metadata": {}, "output_type": "pyout", "text": ["[0, 49, 196, 441, 784, 1225, 1764, 2401]"], "prompt_number": 9}], "collapsed": false, "prompt_number": 9}], "metadata": {}}], "nbformat": 3}