Home > Articles > Open Source > Python

Learn Python the Hard Way: A Good First Program

  • Print
  • + Share This
Just getting started with Python? Zed Shaw walks you through your first program.
This chapter is from the book

Remember, you should have spent a good amount of time in Exercise 0, learning how to install a text editor, run the text editor, run the Terminal, and work with both of them. If you haven’t done that, then do not go on. You will not have a good time. This is the only time I’ll start an exercise with a warning that you should not skip or get ahead of yourself.

Type the following text into a single file named ex1.py. This is important, as Python works best with files ending in .py.


1    print "Hello World!"
2    print "Hello Again"
3    print "I like typing this."
4    print "This is fun."
5    print 'Yay! Printing.'
6    print "I'd much rather you 'not'."
7    print 'I "said" do not touch this.'

If you are on Mac OSX, then this is what your text editor might look like if you use TextWrangler:

If you are on Windows using Notepad++, then this is what it would look like:

Don’t worry if your editor doesn’t look exactly the same; the key points are as follows:

  1. Notice I did not type the line numbers on the left. Those are printed in the book so I can talk about specific lines by saying, “See line 5 . . .” You do not type those into Python scripts.
  2. Notice I have the print at the beginning of the line and how it looks exactly the same as what I have above. Exactly means exactly, not kind of sort of the same. Every single character has to match for it to work. But the colors are all different. Color doesn’t matter; only the characters you type.

Then in Terminal, run the file by typing:

    python ex1.py

If you did it right, then you should see the same output I have below. If not, you have done something wrong. No, the computer is not wrong.

What You Should See

On Max OSX in the Terminal, you should see this:

On Windows in PowerShell, you should see this:

You may see different names, the name of your computer or other things, before the python ex1.py, but the important part is that you type the command and see the output is the same as mine.

If you have an error, it will look like this:

     $ python ex/ex1.py
       File "ex/ex1.py", line 3
         print "I like typing this.
     SyntaxError: EOL while scanning string literal

It’s important that you can read these, since you will be making many of these mistakes. Even I make many of these mistakes. Let’s look at this line by line.

  1. Here we ran our command in the Terminal to run the ex1.py script.
  2. Python then tells us that the file ex1.py has an error on line 3.
  3. It then prints this line for us.
  4. Then it puts a ^ (caret) character to point at where the problem is. Notice the missing " (double-quote) character?
  5. Finally, it prints out a SyntaxError and tells us something about what might be the error. Usually these are very cryptic, but if you copy that text into a search engine, you will find someone else who’s had that error and you can probably figure out how to fix it.
  • + Share This
  • 🔖 Save To Your Account