First steps with Python programming
Creating Programs
When you use the shell, your instructions are carried out (or "run") immediately. Most of the time, you'll want to store your instructions as a program so you can run them later; that is, you can repeat the instructions without having to type them in again, which also allows you to create more sophisticated sequences of instructions that respond to what's happening at the time the program is run.
To create a program, click File | New Window in the menu at the top of your IDLE window, or use the keyboard shortcut Ctrl+N, which opens a blank window, ready for you to write your program.
For starters, type:
print("Snakes alive!") print("I'm a Python programmer!")
This time, when you finish typing a line and press Return, the cursor just goes to the start of the next line, ready for you to type in the next line. None of the commands are run until you're ready. You can add more lines of text if you want to or write a program that displays your favorite poem or the lyrics to your favorite song.
Although you can save your program from the File menu, IDLE will also save your program when you try to run it. The first time, you'll be shown the file browser so you can give your file a name and choose where to save it.
When you're ready to run your program, click Run on the menu at the top of the window and choose Run Module (Figure 4). The F5 key is a handy shortcut with which you'll become very familiar. When your program runs, the Python shell comes to the front (if it's hidden), and your text is printed on the screen. You can click back to the script window where you wrote your program to edit it and develop it further.
An important difference between the shell (where commands are carried out right away) and the programs you write is what happens to the output. If you include a sum in a program, the result doesn't appear on screen because you need to tell Python what to do with the result. Using the print
command like this
print(5 * 5)
tells it that you want it to appear on screen.
Turtley Fantastic
To give Python new capabilities, you can bring in modules other people have written. Modules are just programs that are designed to be reusable and customizable, and you'll regularly use modules for things like generating random numbers or timing events in games.
The turtle
module puts an arrow on screen that you can move around with directional commands, similar to the way you move sprites in Scratch, and it provides a handy visual way to familiarize yourself with Python and see the result of what you're doing. To bring a module into your program, use the import
command, together with the name of the module you want to use.
To begin, start a new program from the File menu, and then enter the following:
import turtle turtle.forward(150)
When you run the program, you'll see a new window open called Python Turtle Graphics. In the middle of the screen is an arrow (the turtle) facing right. As you might expect, the arrow moves 150 pixels in the direction it's facing (to the right). If you've used Scratch before, this will feel familiar.
To change the direction the turtle is facing, use turtle.right()
and turtle.left()
, with a number of degrees in parentheses. For example, to make the turtle turn left by 45 degrees, use:
turtle.left(45)
You could add this command to your program and run it, or type it into the shell. In either case, you'll see the turtle turn to face a different direction.
Try the following program to draw a square (Figure 5):
import turtle turtle.forward(150) turtle.right(90) turtle.forward(150) turtle.right(90) turtle.forward(150) turtle.right(90) turtle.forward(150)
This program has several problems: First, it's a pain to write. Although you can copy and paste in the editor, you're doing the manual work of adding the same instruction several times, even though computers are great at repetition. Second, it's a pain to read because you can't easily see what the program does. Finally, it's a pain to modify. If you want to make the program draw a triangle, you'll have to do a lot of editing.
Buy this article as PDF
Pages: 5
(incl. VAT)