{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": { "cell_tags": [] }, "source": [ "Making Choices" ] }, { "cell_type": "markdown", "metadata": { "cell_tags": [] }, "source": [ "Our previous lessons have shown us how to manipulate data,\n", "define our own functions,\n", "and repeat things.\n", "However,\n", "the programs we have written so far always do the same things,\n", "regardless of what data they're given.\n", "We want programs to make choices based on the values they are manipulating.\n", "To help us see what decisions they're making,\n", "we'll start by looking at how computers manipulate images." ] }, { "cell_type": "markdown", "metadata": { "cell_tags": [ "objectives" ] }, "source": [ "#### Objectives\n", "\n", "* Create a simple \"image\" made out of colored blocks.\n", "* Explain how the RGB model represents colors.\n", "* Explain the similarities and differences between tuples and lists.\n", "* Write conditional statements including `if`, `elif`, and `else` branches.\n", "* Correctly evaluate expressions containing `and` and `or`.\n", "* Correctly write and interpret code containing nested loops and conditionals.\n", "* Explain the advantages of putting frequently-modified code in a function." ] }, { "cell_type": "heading", "level": 3, "metadata": { "cell_tags": [] }, "source": [ "Image Grids" ] }, { "cell_type": "markdown", "metadata": { "cell_tags": [] }, "source": [ "Let's start by creating some simple heat maps of our own\n", "using a library called `ipythonblocks`.\n", "The first step is to create our own \"image\":" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ipythonblocks import ImageGrid" ], "language": "python", "metadata": { "cell_tags": [] }, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unlike the `import` statements we have seen earlier,\n", "this one doesn't load the entire `ipythonblocks` library.\n", "Instead,\n", "it just loads `ImageGrid` from that library,\n", "since that's the only thing we need (for now).\n", "\n", "Once we have `ImageGrid` loaded,\n", "we can use it to create a very simple grid of colored cells:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "grid = ImageGrid(5, 3)\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "