{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic ipythonblocks" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ipythonblocks import BlockGrid\n", "grid = BlockGrid(10, 10, fill=(123, 234, 123))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "WHITE = (255, 255, 255)\n", "RED = (255, 0, 0)\n", "GREEN = (0, 255, 0)\n", "BLUE = (0, 0, 255)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 27 }, { "cell_type": "code", "collapsed": false, "input": [ "grid[5, 5] = WHITE\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 28, "text": [ "" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 1\n", "\n", "Make the square in the upper right hand corner of the grid turn red." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python control structures\n", "\n", "for and if loops!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "for i in range(10):\n", " grid[i, i] = WHITE" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 30, "text": [ "" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2\n", "\n", "Make the rightmost column of blocks turn red." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Nested for loops" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# you can nest for loops\n", "for i in range(10):\n", " for j in range(10):\n", " grid[i, j] = GREEN\n", " \n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "" ] } ], "prompt_number": 31 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Nested for loops with if statements" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# if statements\n", "for i in range(10):\n", " for j in range(10):\n", " if i == j - 1:\n", " grid[i, j] = RED" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "" ] } ], "prompt_number": 33 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3\n", "\n", "Make the center diagonal red, the diagonal above/to the right of it blue, and the diagonal below/to the left of it white." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 33 } ], "metadata": {} } ] }