The first thing that anyone ever learns in a programming language is to make a program that spits out "Hello World". I must admit that I think that this is pretty dumb. How about, for your first program, we instead make a program that spits out:

Hello, my name is _____

Oh my god.  This actually worked!
It's a little more accurate with regard to the feelings inspired by running a program you wrote yourself, and, since that's what you'll probably say anyway, now you'll sound like you are reading your program's output instead of rejoicing.

Writing this program might be deceptively difficult. Not because it is a difficult thing to understand, but because of all of the infrastructure that goes along with this first program. You have to have Python working, you have to edit a text file to give to Python, and then you have to execute that program you wrote. I regret to inform you that this step has the potential to be the most frustrating one. There's a lot of things that people take for granted with computers, and I've been doing them for a while, and it's easy to forget what is initially unintuitive after everything becomes more second nature. Some of the below will only apply to you if you aren't working on a Macintosh. The Mac has a decidedly different setup as compared to a Windows computer or a Linux computer.

Let's start editing the file...

Start your editor, (notepad or edit on Windows, pico, gnotepad, emacs or vi on Linux) and create a new file called first.py. Then, type or paste in the following:

print "My name is Jongleur"
print
print "Oh my god.", "This actually worked!"
You might want to consider substituting your name in for mine, though. To execute your program, go to the directory you saved it in, and type python first.py. If everything worked properly, you should see the output above appear on your screen. If not, then something has gone wrong with your setup. This kind of thing can be tricky to debug in a general way, but if the window just flashes on and then disappears, then add the line raw_input() to the bottom of the file. If that's not the problem, then the easiest thing for you to do would be to /msg me and I'll try to help you.

If you did get output, then hooray! Read it aloud. You just made the computer do something that it couldn't do previously. It's not very useful, but it could be! Now try changing around the text between the quotes and rerunning your program.

If you would like to make the program execute without having to type python in front, you can do one of the following:

On Linux (or any other variant of Unix)
Add the following line to the top of your program:#!/usr/bin/env python. Then, on the command line, type chmod +x first.py
On Windows
This can get complicated, unfortunately. What I recommend is creating a file called first.bat that conatins one line: python first.py. Now, try typing first in that directory. It should run your program.
On a Mac
This is a harder question than you think. It is difficult because you have no CLI. For now, stick to the IDE that came with python. Sorry. That advice was for Mac OS9 and below. With OSX, just follow the Linux instructions, because OSX is a UNIX, just like Linux.

Alright, how could this be useful? Well, perhaps you use the command line a lot. If so, then there are probably some commands you always mistype. (I know that I often type sl instead of ls or dri instead of dir) You could create a file called dri. Then whenever you type dri, instead of seeing an error message, you could see something like "Learn to type!!!! You meant to type dir". Now all of a sudden, you have created something useful! You now have a program that will give you negative feedback every time you mistype a command.

Let's analyze what's going on here after you hit enter on the command line...

  1. First, you are calling the python interpreter on your file. This seems like cheating, but it's easier than compiling and it gives more instant feedback.
  2. Now, the interpreter looks at the first line of your prgram and runs it. It is pretty clear what it does - it prints out everything between the quotes.
  3. We tell it to print nothing. So it spits out a blank line.
  4. We tell it to print two things on the same line. We separate those two things with commas. You can actually pass an (almost) infinite amount of things to print as long as you put commas between them. Note that it automatically put a space between the first thing and the second thing.
  5. It reaches the end of the file and so quits.

Now for some terminology. The things between the quotes are referred to as strings. A string is simply a bunch of characters between two quotes. You can actually use ' instead of " as long as you use the same quote mark at the other end of the string. print is a function or command. The way python works is by you telling it "do this, then do this". "this" is almost always a function. You can define your own functions, but not for a few lessons.

Now, play around! Change the strings! Try printing numbers! Try printing numbers without using quotes! Go mad with your newfound power!

At some point in your playing around, you will probably screw up. This is natural and good. Fear of failure is something that is completely unacceptable in coding. When you screw up you should see somthing like the following:

  File "first.py", line 2
    print :googoo"
          ^
SyntaxError: invalid syntax
Notice how python tries to help you. It tells you what line it got confused at, and it points out the exact character it got confused at. Look at that line - I screwed up by putting a : instead of a ". You should also notice how painless failure was. This is good. Most programs take a while to get right, and if failure was painful, the only coders would be members of the Jim Rose Circus.

It's initially hard to see that relationship between what you just did, and, say, Microsoft Word, Linux, or Mozilla. I promise, however, that the steps you have taken to get to your current point have gotten you more than halfway to being able to understand how those giant behemoths work.

After playing around for a little while longer, proceed to the next lesson. In case you are out of ideas, here are some things you can try. Put \n, \r, \t, \\, and \g in the string you print out. Try putting them in one at a time. The \ character is called an escape character. It, combined with the character that follows it, does something special. Since there is no "make a beep" key on your keyboard, we have to use the backslash to generate one. It also allows you to put quote marks in your string without prematurely ending it by using \". Now that you know how to make newlines, tabs, quotes, and beeps, your negative-feedback program can get even more annoying!

Learn to Program | >>

Log in or register to write something here or to contact authors.