Lesson 1: Getting Started: The Game Over Program
Programming basically is getting your computer to do stuff. This is not the most technical
definition, but it’s a pretty accurate one. By learning Python, you’ll be able to create a
program, whether it’s a
simple game, a small utility, or a business product with a full
featured graphical user interface (GUI). It’ll be all yours, something you made, and it will
do just what you told it to. Programming is part science, part art, and one great adventure.
esson starts you on your Python programming journey. In it, you’ll learn:
What Python is and what’s so great about it
How to install Python on your computer
How to print text to the screen
What comments are and how to use them
How to use Python’s integrat
ed development environment (IDE) to write, edit,
run, and save your programs
EXAMINING THE GAME OVER PROGRAM
This lesson’s project, Game Over, displays the two most infamous words in computer
gaming: “Game Over,” The figure below shows the program in acti
The above figure shows what’s called a console window, a window that can display only
text. Though not as nice as windows in a Graphical User Interface (GUI), console
applications are easier to write and a good place for the beginning progra
mmer to start.
The Game Over Program is pretty simple: in fact, it’s one of the simplest Python
programs that you can write. That’s the reason it’s presented in this chapter. By
completing such a modest program, you cover all the setup work required to st
programming in Python, such as installing the language on your system. You also work
through the entire process of writing, saving, and running a program. Once you finish all
of this groundwork, you’ll be ready to tackle larger programs with some real
IN THE REAL WORLD:
The Game Over program is really just a variation of the
traditional Hello program, which displays the words “Hello World” on the screen. The
Hello World program is often the first program a beginner writes in order to dip
his or her
toe in a new language. It’s such a common first program that Hello World is an
understood term in programming.
Python is a powerful yet easy
use programming language developed by Guido van
Rossum, first released in 1991. W
ith Python, you can quickly write a small project. But
Python also scales up nicely and can be used for mission
If you check out any Python documentation, you may notice an alarming number of
references to spam, eggs, an
d the number 42. These references all pay homage to Monty
Python, the English comedy troupe that inspired Python’s name. Even though Guido von
Rossum named Python after the group, the official mascot of the language has become
the python snake (which is re
ally for the best, since it would be pretty hard to fit six
British comedians’ faces on a program icon anyway.)
There are a lot of programming languages out there. What’s so great about Python? Let ,e
Python is Easy to Use
The major goal of any
programming language is to bridge the gap between the
programmer’s brain and the computer. Most of the popular languages you’ve probably
heard of, like Visual Basic, C#, and Java, are considered high
level languages, which
means that they’re closer to hum
an language then machine language. And they are. But
Python, with its clear and simple rules, is even closer to English. Creating Python
programs is so straightforward that it’s been called “programming at the speed of
thought.” Python’s ease of use transl
ates into productivity for professional programmers.
Python programs are shorter and take less time to create than programs in many popular
Python is Powerful
Python has all the power you’d expect from a modern programming language. By the end
of this course, you’ll be able to write programs that employ a GUI, process files, and use
a variety of data structures.
Python is powerful enough to attract developers from around the world as well as
companies such as Google, IBM, Industrial Light + Mag
ic, Microsoft, NASA, Red Hat,
Verizon, Xerox, and Yahoo!. Python is also used as a tool by professional game
programmers. Electronic Arts, 2K Games, and the Disney Interactive Media Group all
publish games that incorporate Python.
Python is Object
oriented programming (OOP) is a modern approach to solving problems with
computers. It embodies an intuitive method of representing information and actions in a
program. It’s certainly not the only way to write programs, but, for large projects.,
often the best way to go.
Languages like C#, Java, and Python are all object
oriented. But Python does them one
better. In C# and Java, OOP is not optional. This makes short programs unnecessarily
complex, and it requires a lot of explanation before
a new programmer can do anything
significant. Python takes a different approach. In Python, using OOP techniques is
optional. You have all of OOP’s power at your disposal, but you can use it when you
need it. Do you have a short program that doesn’t really
require OOP? No problem. Do
you have a large project with a team of programmers that demands OOP? That’ll work
too. Python gives you power and flexibility.
Python is a “Glue” Language
Python can be integrated with other languages such as C, C++, and Java
. This means that
a programmer can take advantage of work already done in another language while using
Python. It also mean that he/she can leverage the strengths of other languages, such as the
extra speed that C or C++ might offer, while still enjoying t
he ease of development that’a
a hallmark of Python programming.
Python Runs Everywhere
Python runs on everything from a Palm Pilot to a Cray (a supercomputer). And if you
don’t happen to have a supercomputer in your house, you can still run Python on
ows, Macintosh, or Linux machines, and that’s just the top of the list.
Python programs are platform independent, which means that regardless of the operating
system you use to create your program, it’ll run on any other computer with Python. So if
ite a program on your PC, you can e
mail a copy to your friend who runs Linux or
to your aunt who has a Mac, and the program will work (as long as your friend and aunt
have Python installed on their computers).
Python Has a Strong Community
ng languages have a dedicated newsgroup, but Python also has
something called the Python Tutor mailing list, a more informal way for beginning
programmers ask those first questions. The list is at
. Even though the list is called Tutor, a
novice or an expert programmer can answer questions.
There are other Python communities focused on different areas, but the common element
they share is that they t
end to be friendly and open. That only makes sense since the
language itself is so approachable for beginners.
Python is Free and Open Source
Python is free. You can install it on your computer and never pay a penny. But Python’s
license lets you do much
more than that. You can copy or modify Python. Embracing
source ideals like this is part of what makes Python so popular and successful.
Setting up Python on Windows
e you can jump in and write your first Python program, you need to get the
uage on your computer. But, don’t worry
I’ll walk you through the process of
installing Python on a Windows machine. (Python comes built
in on an Apple computer
depending on the age of the computer you might have to upgrade it).
Python on W
To install Python under Windows, follow these steps:
Download the Python Windows installer from the Python website,
(the file is called python
Run the Python Windows Installer,
Accept the default configuration. Once you’re done, you’ll have Python 3.1 on
Python comes with an IDE (
nvironment) called IDLE. A
development environment is a set of tools that makes w
riting programs easier. You can
think of it as a word processor for your programs. But it’s even more than a place to
write, save, and edit your work. IDLE provides two modes in which to work; an
interactive and a script mode.
Programming in Interactive M
Finally, it’s time to get your hands dirty with some actual Python programming. The
quickest way is to start Python in interactive mode. In this mode, you can tell Python
what to do and it’ll respond immediately.
Writing Your F
To begin yo
ur interactive session, from the Start menu, choose All Programs , Python
3.1, IDLE (Python GUI). You should see something very similar below. Python in an
interactive session, awaiting your command.
Python in an interactive
session waiting your comma
This window is called the Python Shell. At the command prompt (
and then press the Enter key. The interpreter responds by
displaying on the screen:
Ta da! You’ve written your first Python program! You are a
real programmer (with a
little more to learn, but that goes for all of us).
Using the print Function
Take a look at the line you entered,
straightforward it is. Without knowing anything about programming, you could have
ably guessed what it does. That’s Python in a nutshell. It’s concise and clear. You’ll
appreciate this even more as you learn how to do more complex things with the language.
function can display text, surrounded by quotes, that you can put in
the pair of parentheses. If you put nothing inside the parentheses, it will print a blank line.
and by convention,
, So, c will work, but Print(“Game Over”) and PRINT(“Game Over”) wo
Learning the Jargon
Now that you’re a programmer, you have to throw around those fancy terms that only
programmers understand. A
is like a mini
program that goes off and performs
some specific task. The task of the
function is to dis
play a value (or sequence
of values). You kick off, or
, a function by using the function name, followed by a set
of parentheses. You did just this in interactive mode when you typed
times you can give, or
, a function value
s to work with. You put
these values, called
, between the parentheses. In the case of your first
program, you passed the
function the argument
function used to display the message
Functions in Pyth
or provide information back to the part of the
program that called the function. These values are called
. You’ll learn
more about return values in the next lesson.
In this particular case, you can be more specific by saying th
at the value
function is a string. This just means that it’s a series of
characters, like the ones on your keyboard. “String” may seem like an odd name
or “words” might be clearer
but the name comes from the idea t
hat text is a string, or a
series, of characters. Technically,
is a string literal because it is literally
the sequence of characters that make up the words.
The line you entered in the interpreter is also considered a statement. In English,
statement is a complete thought. In Python, a statement is a complete instruction. It does
something. Every program contains a number of statements.
Finally, now that you’re a programmer, you can tell someone that you wrote some Python
. Code means
programming statements. You can also use it as a verb to mean the act
of programming. For example, you can say that you were up all night eating Hot Cheetos,
drinking Mountain Dew, and coding like crazy.
Generating an Error
Computers take everything liter
ally. If you misspell a function name by even one letter,
the computer will have absolutely no idea what you mean. For example, at the interactive
prompt, if I type primt(
, the in
terpreter will respond with something
like the following:
ack (most recent call last):
File "<pyshell#0>", line 1, in <module>
NameError: name 'primt' is not defined
Translated to English, the interpreter is saying “Huh?!” The key line is the error message
NameError: name 'primt' is not
. The message states that the
interpreter doesn’t recognize
As a human being, you can ignore any typo and
understand what I meant. Computers are not so forgiving. Luckily, programming errors,
, like this can be easily resolved by fixin
g the offending typo.
Understanding Syntax Highlighting
You probably noticed that words on the screen are displayed in different colors. This
, helps you quickly understand what you’ve
typed by visually categorizing
it. And there is a method to this coloring madness. Special
words that are a part of the Python language (called
or reserved words), like
print, are displayed in
, like “Game Over”, are in
. And the
what the interpreter
displays as a result of that you type
. As you
write larger programs, this color scheme will come in handy by helping you take in your
code in one glance and spot errors more easily.
Programming in Script Mode
Using the interactive mode gives y
ou immediate feedback. This is great because you can
see the results. But it’s not designed to create programs you can save and run later.
Luckily, Python’s IDLE also offers a script mode, in which you can write, edit, load, and
save your programs. It’s li
ke a word processor for your code. In fact, you can perform
such familiar tasks as find and replace, and cut and paste.
Writing your First Program (Again)
You can open a script mode from the interactive window you’ve been using. Select the
File menu, then
New Window. A new window will appear that looks just like the one
Your blank canvas awaits! Python is ready
for you to write a program in script mode.
In this new script window, type
and press Enter. Nothing
That’s because you’re in script mode. What you’re doing is writing a list of
statements for the computer to execute later. Once you save your program, you can run it.
Saving and Running YourProgram
To save your program, select File, Save As. I gave my co
py the name
Save this file in your Lesson 1 folder that you have created.
Make sure to save your programs with the .py extension. This allows various
applications, including IDLE to recognize these files as Python programs.
To run my
Game Over program, I simply select Run, Run Module. Then, the interactive
window displays the results of my program.
The results of running the Game Over program through IDLE.
To run your program from IDLE, you need to first save your program.
tive mode is great for trying out a small idea quickly. Script mode is perfect for
writing programs you can run later. Using both modes together is a great way to code.
Even though I need only script mode to write a program, I always keep an interactive
window open. As I write my programs in script mode, I jump over to the interactive
window try out an idea or to be sure I have the usage of a function just right.
The script window is where I craft my final product. The interactive window is like a
h pad where I can experiment. Using them together helps me to write better
programs more quickly.
BACK TO THE GAME OVER PROGRAM
So far, you’ve run a version of the Game Over program through IDLE. While you’re in
the process of writing a program, running i
t through IDLE is a fine way to go. But, I’m
sure you want your finished products to work like any other program on your computer.
You want a user to simply double
click your program’s icon to launch your program.
If you were to try to run the version of
the Game Over program I’ve shown so far in this
way, you’d see a window appear and, just as quickly, disappear. You’d probably think
that nothing happened. But something would have happened. It just would have happened
too fast for you to notice. The progr
am would run, Game Over would be displayed, and
the program would end, all in a split second. What the program needs is a way to keep its
console window open.
This updated version of Game Over, keeps the program window open so the user can see
Press the enter key to exit
. Once a user presses the Enter key,
the program exits, and the console window disappears.
Under the Windows operating system, you can directly open a Python program
in IDLE by right
clicking in the file icon and selectin
g Edit with IDLE.
The following are the first two lines of the program:
# Game Over
# Demonstrates the print function
These lines aren’t statements for the computer to execute. In fact, the computer totally
ignores them. These notes, call
ed comments, are for the humans. Comments explain
programming code in English (or for any other language for that matter). Comments are
invaluable to other programmers and help them to understand your code. But comments
are also helpful to you. They remind
you of how you accomplished something that may
not be clear at a glance.
You create a comment with the number sign symbol, #. Anything after this symbol
except in a string
) on the rest of the line is a comment. Comments are ignored by the
ce that comments are colored red in IDLE to make them stand out.
It’s a good idea to start all your programs with a few comments, like a do. It’s helpful to
list the title of the program and its purpose. Although I don’t use it in the example on the
ous program, you should also list your name and the date the program was written.
You may be thinking: “Why have comments at all? I wrote the program, so I know what
is does.” That may be true a month after you write your code, but experienced
know that after a few months away from a program, your original
intentions many not be clear. If you want to modify an old program, a few well
comments may make your life much easier,.
IN THE REAL WORLD
Comments are even more helpful to another p
needs to modify a program you wrote. This kind of situation comes up a lot in the world
of professional programming. In fact, it’s estimated that the majority of a programmer’s
time and effort go toward maintaining code that already exists. I
t’s not uncommon for a
programmer to be charged with the task of modifying a program written by someone
and there’s a chance that the original programmer won’t be around to answer any
questions. So, GOOD comments are CRITICAL!!!
Using Blank Lines
hnically, the next line in the program is blank. The computer generally ignores blank
lines; these, too, are just for the humans reading the code. Blank lines can make programs
easier to read. Usually, I keep lines of related code together and separate sec
tions with a
blank line. In this program, I separated the comments from the call to the
function with a blank line.
Printing the String
The next line in the program should seem familiar to you:
It’s your old friend, the
nction. This line, just as it does in interactive mode,
Waiting for the User
The last line of the program
nPress the enter key to exit.")
displays the prompt
Press the enter key to exit
and waits for the user to
Enter key. Once the user presses the key, the program ends. This is a nice trick
to keep a console window open until the user is done with an application.
Normally, this is about the time that I’d explain what is going on in this line. However,
I’m going t
o keep you in suspense! Sorry. You’ll have to wait until the next lesson to
fully appreciate this one line.
You covered a lot of ground in this lesson. You learned a bit about Python and its
strengths. You installed the language on your computer a
nd gave it a little test drive. You
learned to use Python’s interactive mode to instantly execute a programming statement.
You saw how to use script mode to write, edit, save, and run longer programs. You
learned now to display text to the screen and how t
o wait for the user before closing a
program’s console window. You laid all the groundwork necessary for your adventure in