{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A short Besides Blocks Snap! to Python Module" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Previously in Snap! we have been used to programming with sprites. We will do something similar to help bridge your understanding of how computational thinking can be realized either in Snap! or python.\n", "\n", "Now let's start with some basics of python using something we are already familiar with turtle graphics. Remember in Snap!, we had our sprite, and the sprite could move around the stage. We can do the same thing with python. To do this, lets start by importing the turtle package. But before we do that, we need to make sure that we are using the right version of python. For this class, the good folks at Beauty and Joy of Computing have decided that we should stick with python3.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import turtle\n", "wn = turtle.Screen() # creates a graphics window" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The python code brings in the turtle graphics package by using the keyword \"import.\" The turtle package is made up of a series of functions that allows the user to move a cybernetic turtle around in space. Very similar to alonzo in our Snap! environment." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "alonzo = turtle.Turtle() # create a turtle named alonzo\n", "alonzo.forward(150) # tell alex to move forward by 150 units\n", "alonzo.left(90) # turn by 90 degrees\n", "alonzo.forward(75)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should see something like this in your window. \n", "Now lets see if you can make a square ....\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "alonzo.forward(150) # tell alonzo to move forward by 150 units\n", "alonzo.left(90) # turn by 90 degrees\n", "alonzo.forward(150) \n", "alonzo.left(90)\n", "alonzo.forward(90)\n", "alonzo.shape(\"turtle\") # throwing this piece of code in there, changes the shape of the sprite" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Oops, I realize I need to clear my screen, but I don't know how to do it. Not to worry, we can learn all about all the functions that are associated with the python turtle package by typing \"Python Turtle Graphics.\" It should lead us to the python documentation found here https://docs.python.org/2/library/turtle.html. You should see something like the following below." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import HTML\n", "HTML('')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ah, once I scroll down the page, I see there is a clear() command. Now I can clear my screen, and start all over again with making the square.\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "alonzo.clear() # clear my screen\n", "alonzo.shape(\"turtle\") # make the shape of a turtle\n", "alonzo.forward(150) # tell alonzo to move forward by 150 units\n", "alonzo.left(90) # turn by 90 degrees\n", "alonzo.forward(150) \n", "alonzo.left(90)\n", "alonzo.forward(150)\n", "alonzo.left(90)\n", "alonzo.forward(150)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "alonzo.clear() # clear my screen\n", "alonzo.shape(\"turtle\") # make the shape of a turtle\n", "for x in range (0, 4):\n", " alonzo.forward(150) # tell alonzo to move forward by 150 units\n", " alonzo.left(90) # turn by 90 degrees" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "There we go. Now lets optimize the code by putting it into a for loop. If you have forgotten what that statement is, just google \"python for loops\" and you should be ready to go.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have done that, lets make a more interesting visualization before we move on to more complex stuff. Lets make a fun starburst using turtle graphics. The code is included below. You should get an image like the one below. \n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#clear()\n", "#penup()\n", "from turtle import *\n", "shape(\"turtle\")\n", "color('blue', 'yellow')\n", "begin_fill()\n", "while True:\n", " forward(200)\n", " left(170)\n", " if abs(position()) < 1:\n", " break\n", "end_fill()\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Challenge time!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1) A more advanced block we made in Snap! was this block. Can you write the same block in Python?\n", " \n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Recursion \n", "Ready for an even greater challenge? Surprise - you can do recursion in Python too! Use the same ideas we used to make fractals in Snap! to build one in Python!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By the way, did you know that the guy who help invent the field of Artificial Intelligence created Turtle Graphics? His name is Seymour Papert and he has the most delighful book on computational thinking called Mindstorms, Children, Computers and Powerful Ideas. You can read it free on the web.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }