What is Python?

Python is a progrmming language, which means it’s a language both people and computers can understand. A computer language is a formal subset of an informal language, like English. A computer language lets people express what they want a computer to do, and tells a computer how to do it. A computer program is a set of instructions written in a particular computer language. There are lots of different computer languages in the world, most exist to solve certain kinds of problems, and most over lap in the kinds of things they can do. Python was developed by a Dutch man named Guido van Rossum, a software engineer who created the language to solve some problems he saw in computer languages of the time. Python draws from a lot of good ideas in other languages and pulls them together in one place. Python is a powerful programming language that is fairly easy to learn. The name Python comes from Guido’s favorite comedy group, Monty Python’s Flying Circus.

You can download Python for your computer from here, the version you should get is Python 2.7.9.

Let’s Do Something Fun With Python

Rather than go through a lot of “how to program” stuff about Python up front, let’s just dive in and do something fun and interesting instead! We’re going to create a Python program that will let you create a colorful flower on screen. Here’s an picture of what we’re going to create:

_images/flower.png

First Things First

Let’s get the Python system going so we can write a program. There are two ways to work with Python; working with it directly and creating Python program files. This is where we can use the tool called Idle. Idle is a program tool that lets you work with Python directly and create progam files. Starting Idle should give you a window that looks something like this:

_images/idle_window.png

This window provides a Python command prompt (hit return a couple of times if you don’t see it) that allows you to run Python statements line by line, what’s called Python’s interactive mode. The command prompt looks like this:

>>>

This is where you enter Python statements to try things out. For example, try this at the command prompt:

>>> print "Hello there"
Hello there
>>> print 12
12
>>> x = 12
>>> x * 3
36
>>> x / 3
4

He’re we’re just printing a welcome message, doing some basic math and Python is responding. What we’ve done is enter some Python statments at our command prompt, and Python ran them. statements are the lines in a program that tell Python what you want it to do.

Creating Python Program Files

Working with interactive mode, is great for trying things out with Python. However, we want to create a Python program we can run over and over again without having to re-type it every time. This is where creating a Python program and saving it as a file is very handy. Python program files are just like any other text file, but usually have the file extension ”.py”. We can create a Python program file in Idle by clicking the File -> New Window menu item. This opens up a new, empty window that is a simple text editor, like Windows notepad or wordpad. You’ll notice there is no >>> Python command prompt in the window. This is because in the file window we’re not interacting with Python directly, we’re creating a Python program file. Let’s use our new Python program file to create our flower drawing program!

Let’s Create Our Flower

Python comes with a library of modules that let us do some interesting things, and one of those modules is called ‘turtle’. The turtle module is a nice tool for drawing graphics on the screen. You can find the turtle graphics documenation here. The turtle module is based on the idea of a “turtle” on the screen that draws a line as it moves around.

In order to use the turtle module we have to “import” it into our Python program. To import the turtle module add this line to our Python program file:

import turtle

Drawing With Turtle

Once the turtle module is available to us we can use it to draw things with a turtle. Enter these lines into our program file:

t = turtle.Turtle()
t.shape("turtle")
t.forward(100)

Saving And Running A Python Program

Let’s run the program. We have to save the file first, which we can do from the File -> Save menu selection. Give our program a name and save it to a directory on the hard disk where you can find it again. To run the program select Run -> Run Module. If your program runs without any errors, a window will open up with a turtle shape at the end of a short line. That window should look this:

_images/turtle_one.png

This is what our program told Python to do, use the turtle module to create a turtle that we’re calling t, change its shape to look like a ‘turtle’ and move it forward 100 pixels.

Let’s Get Back to Drawing!

Let’s add more statements to our program to make it draw some more. Let’s make our Python program look like this:

import turtle

t = turtle.Turtle()
t.shape("turtle")
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.color("red")
t.forward(100)

When you save and run our program the screen your turtle is drawing on should look like this:

_images/turtle_two.png

These statements made the turtle draw four lines, turning right 90 degrees (a right angle) after each line. The last side of the box is drawn in red. You can see something interesting about drawing with our turtle; what it draws is based on where it is on the screen and which way it’s headed.

More Drawing With Turtles

Let’s learn some more Python turtle module statements to use to with our turtle.

Turtle Speed

To make our turtle draw faster we use the turtle speed() method. To use this we’ll add this statement to our program:

t.speed(0)

A number 0 makes our turtle draw as fast as it can.

Turtle Line Width

We can make our turtle draw with a thicker line, making it easier to see on screen. We do this with the turtle width() method. We can pass a width in pixels, so for example adding this:

t.width(3)

to our program makes our turtle draw with a line 3 pixels wide.

Filling In Shapes

We can also fill a shape (like our box) with color using two other turtle methods, begin_fill() and end_fill(), and by modifying our t.color() method.

Putting It All together

Taking the things we’ve learned from our box drawing, let’s draw our flower. We’ll do this by doing two things; draw multiple boxes, filling the box with color and turning slightly between each box using the new turtle methods we just learned about. One way we could draw our flower would be to just repeat our box code over and over like this:

import turtle

t = turtle.Turtle()
t.speed(0)
t.width(3)
t.color("yellow", "red")

# draw our first filled in box
t.begin_fill()
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.end_fill()
t.right(10)

# draw our second filled in box
t.begin_fill()
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.end_fill()
t.right(10)

# keep going till you've drawn your flower

This would work fine, but we would have to repeat these statements for as many petals as we want to give our flower. Since we’re drawing a flower ‘petal’ every 10 degrees, and there are 360 degrees in a full circle, how many times would we have to repeat the above code? Can Python help us not repeat ourselves? Yes, it can help us by providing a way to repeat drawing the box multiple times.

Loops

Loops are statements in a programming language that allows us to repeat a set of program statements over and over. In our program we’d like to repeat the statements:

t.begin_fill()
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.end_fill()
t.right(10)

These statements create our outlined and filled in box. We’d like to repeat that, with a slight turn of 10 degrees each time, in order to create a flower. Creating a loop lets us do this. One of the looping statements in Python is called a ‘for loop’, and it’s used to create a loop that repeats for a fixed number of times.

The For Loop

We already know we want to repeat our filled in box 36 times (360 degrees divided by 10 degrees). This means we want to loop 36 times. This is where the Python for loop comes in handy. It is made for repeating things a known number of times. For our flower program we’ll use a for loop that looks like this:

for petal in range(36):
        t.begin_fill()
        t.forward(100)
        t.right(90)
        t.forward(100)
        t.right(90)
        t.forward(100)
        t.right(90)
        t.forward(100)
        t.right(90)
        t.end_fill()
        t.right(10)

Our box code is in there, under the for loop, but what’s that strange looking range(36) thing at a the end of our for loop? The range(36) thing is what tells the for loop to repeat 36 times. Notice that our box drawing Python statements are also indented under the for loop. This is how Python knows to include all of those statements in the loop.

Here’s our complete flower drawing program:

import turtle

t = turtle.Turtle()
t.speed(0)
t.width(3)
t.color("yellow", "red")

for petal in range(36):
        t.begin_fill()
        t.forward(100)
        t.right(90)
        t.forward(100)
        t.right(90)
        t.forward(100)
        t.right(90)
        t.forward(100)
        t.right(90)
        t.end_fill()
        t.right(10)

Once you’ve got this entered, save our program and then run it. If your program runs without any errors, you should get a window like this:

_images/flower.png

Congratulations, you’ve written your first colorful, interesting Python program! At this point we can play with the colors of our box, the line width, the number of petals, anything we want to draw things differently!