You are viewing Chapter 1.2: Your First Python Program: "Hello World" (and variations) of the Guide to Learning Python for OpenRPG. You can also go to the table of contents to see the full list of available chapters.
Now, we're ready to start programming. Open up IDLE (Windows users, it's in Start > Programs > Python 2.2 > IDLE (Python GUI) ), or your favorite text editor, such as notepad, and create a new document. In IDLE, this is done by going to File > New Window. If the first window that pops up when you open IDLE looks like the Python interpreter, with the Python command prompt, then you've gotthe Python Shell, not the program editing window. The Python Shell is useful when you want to just experiment without leaving IDLE, but it's no good for actually writing programs. If you want the editor window, click File > New WindowAnyway, once you've got your new editor window open, you should have a blank page. Start by typing:
print "Hello World!"
That is the entire program. Really. There is no need to declare any variables, define any functions, introduce the program, import any libraries, or anything else. That's it. There aren't even any curly brackets. Now, this is a very simple program, but it still beats the 10+ lines to do the same thing in C. As a nice side-note, IDLE turns the word print orange, because it is a special Python word, and the words "Hello World!" green, because they are a string (more on that in the next section).
Anyway, save the file as "helloworld.py" in the folder of your choice (IDLE users, be sure you add the .py, because IDLE doesn't add it by itself). Then run it. If you're in IDLE, you can test it out just by going back to your editor window and pressing CTRL+F5. If you're using some other editor, save the file and then find it in windows and run it like a program. Surprise, surprise. A little console window saying "Hello world!" pops up for a second and then disappears. The reason it disappears is that once the program is over, it closes automatically. If you wanted it to stay, you could run it in a console window; go to the directory you saved it in, and type the following line (changing the path to python.exe appropriately):
(Non-Windows users will have to type something slightly different, but I figure people on Linux platforms know what they're doing enough to figure it out. As for you Mac people, good luck. At least it's similar to Linux.) For those of you who don't know how to work DOS, I'm not really going to explain, because this is a Python tutorial, not a DOS one, but I can give you a couple hints. First, DOS uses 8-letter file and folder names, with no spaces. Thus, if the folder you put it in has a space in its name, or somewhere in its path, you can use its "DOS name", which is the first six letters, skipping any spaces, followed by "~1", or "~2" if there are multiple files with the same first six letters in the directory. You change directory by typing cd dir, where dir is the directory you want to go to - it can be an absolute path (starting with a drive letter and continuing all the way through), or a relative path, which is simply a folder inside the current one. It can also be "cd .." (without the quotes, of course) if you want to go up a directory instead of in. But that's all for the DOS tutorial - it's not really necessary to run the program in DOS anyway.
Now, getting back to the program. I will explain how it works: Essentially, Python sees the word print and knows that it should print out whatever comes after it. The words "Hello World!" are in quotes, so they are recognized as a string, or a phrase that it should print verbatim, so it prints exactly that. So, let's add more lines, using the same print statement as before. For example:
print "Hello World!" print "I can do math!" print "Two plus two is", 2+2 print "Catch you next time!"
Now, save the program and run it again. This time, the window should read:
Hello World! I can do math! Two plus two is 4 Catch you next time!
Now, I'll explain it again. This time, it saw multiple lines, so it ran them all, one after another. The first line, like in the last program, simply printed "Hello World!" to the screen. The next print statement printed its text out on a new line - programmers will notice that no "\n" is needed to create a line break at the end of the line, as it's done automatically by print. The third line is something new - the print statement had two parts to it: a string, "Two plus two is" and a math phrase, 2+2. Since 2+2 wasn't in quotes, Python simplified it and then printed it out, with a space after the previous part. You can have as many parts, called arguments, in a print statement as you want.