{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Introduction: Control Flow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`for` loops in Python always iterate over something list-like:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "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", "\n", " print(i)" ] }, { "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:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "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" ] } ], "source": [ "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))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`while` loops exist too:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SOLUTION: 15\n" ] } ], "source": [ "i = 0\n", "while True:\n", " i += 1\n", " if i**3 + i**2 + i + 1 == 3616:\n", " break\n", "\n", "print(\"SOLUTION:\", i)" ] }, { "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:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mylist = []\n", "\n", "for i in range(50):\n", "\n", " if i % 7 == 0:\n", "\n", " mylist.append(i**2)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 49, 196, 441, 784, 1225, 1764, 2401]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python has a something called *list comprehension*:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mylist = [i**2 for i in range(50) if i % 7 == 0]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 49, 196, 441, 784, 1225, 1764, 2401]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 }