Wednesday, December 17, 2014

Computer Science For Kids Using Python - Or I Like Turtles

Hello!

I know I'm a little late to Computer Science week, but better late than never. I feel that computer science is way more valuable than just a week or an hour of code.  This post will be a quick introduction to teaching children programming through the Python programming language. We will focus on using the turtle graphics module within Python.


 First, you will want to download Python. Python can be downloaded here.  I'm using Python 2.7 for all of the samples, so I would suggest using that. The Python download includes a program called IDLE which is a shell for interpreting or running our programs. To complete this guide entirely, I would set aside a solid hour or so - more if the program is not installed on your computers yet.


This download will include everything you need to run python programs, however, I would strongly advise on also getting a quality text editor and possibly a Python IDE, or integrated development environment for later use.

My suggestions for these would be:


Text editor: Sublime Text 3

Python IDE - Pycharm community edition


Take some time and install these if you want. When you're ready, open up IDLE.


Note: Python files end in .py   If you write your code in a text editor be sure to save them accordingly.

Python is an interpreted language. That means that it does not need to be compiled before it runs like some other languages. You can type in Python statements and see the results immediately like below:



As you can see, Python can be used as a fancy calculator! Spend the next few minutes having the kids type in some calculations of their own. Write out some more advanced mathematical equations on the board and see if they can use Python to solve.

When done, open a fresh IDLE window and now type: print "Hello World from Python!" and hit enter:



We have now made the infamous first program for any language - Hello World! Spend a minute or two having the kids print text on the screen via the print command.

It follows this skeleton: print ("YOUR TEXT GOES HERE")


Now we move into the turtle graphics module portion. This is where I think children will have the most fun. The turtle graphics module is an included library. A library is code that extends beyond the normal functions of a language. You can find various Python libraries on the web. We can start working with turtle graphics right away because it is included in the regular Python installer.

The basic concept behind turtle graphics is drawing on the screen. Our "turtle" acts as a pen and moves on the canvas where we want drawing along the way in a given direction.

To use the turtle graphics mode, we need to tell Python that we want to use it. This is done via the import statement:

import turtle

Next, we need to create an object to work with. Objects carry out actions just like in the real world. Our object will be the turtle, so let's give him a name:

larry = turtle.Turtle()

 Now we have defined our turtle as larry. Larry will now be able to move around the screen and draw for us:

larry.forward(100)

The above line will move larry in the direction he faces 100 pixels on the screen

The last step is to tell the program that we are done:

larry.done()

Those 4 lines would give us:


The complete program listing for the above program is:

import turtle

larry = turtle.Turtle()

larry.forward(100)

larry.done()

It's really quick and easy to get something going on the screen. Now let's try something a little more advanced. Math will start to come into play here.

We will change the pen color of larry to orange by adding in this line:

larry.pencolor("orange")

Now let's make a square using the above code snippet.

import turtle

larry = turtle.Turtle()
larry.pencolor("orange")

larry.forward(50)
larry.right(90)

larry.forward(50)
larry.right(90)

larry.forward(50)
larry.right(90)

larry.forward(50)
larry.right(90)

turtle.exitonclick()


The above program should yield these results:



We successfully drew an orange square on the screen using those 90 degree angles! At this point, I would suggest talking about games like Minecraft and how many objects come together to form the game world. Pygame is a Python library intended for games creation. Have kids check it out when they get some time.

You could have the kids try to draw a more complex object at this point such as a house by dividing parts like the house structure, roof, windows, door, etc.

You could also set a timer for say 10-15 minutes and see who can draw the most shapes in the allotted amount of time.

There are many ways to go from here. See what the kids can come up with next. Here is a fun picture of something I whipped up next:


You can start to do all kinds of crazy shape/color combinations with very little code. The above code sample is:

import turtle

larry = turtle.Turtle()
larry.pencolor("purple")

for i in range(125):
    larry.forward(60)
    larry.left(125)

    larry.pencolor("red")

    for i in range(125):
        larry.forward(100)
        larry.left(125)

turtle.exitonclick()

The program uses conditional statements (for i in range(125):  to achieve the drawing. I won't get into conditionals for the scope of this tutorial. It also features two pen colors: purple and red.

That wraps up your first steps into Python programming using turtle graphics. I'll include a few links so that you can continue research into Pythong/turtle graphics if interested. They are:


Python 2.7 Docs

Turtle graphics for Python 2.7

Python Beginner's Guide Wiki

Python for Beginners 

Intro to Python Graphics 



I hope you found this tutorial useful. Please feel free to share/modify parts of it as you wish.

Happy coding!


~Michael

No comments:

Post a Comment