To get started we will open up IDLE.
For Windows(XP) machines:
Start -> All Programs -> Python 2.6 -> IDLE (Python GUI)
For Linux(Ubuntu) machines:
Applications -> Programming -> IDLE (Using Python 2.6)
Once IDLE is open, we want to open a new window. This is where we will type all the code for our program. For now you can close Python Shell if you would like. When we run the program the Python Shell will launch automatically.
Note: Be sure to save your Python files with the .py extension!
File -> New Window
To get started typically you want to include a few comments. This can include the program name, possible a few details about the program, and the author name.
#This is a comment. It starts with the pound sign. #Python will ignore this when executing the code. #It will only be visible to someone looking at the code.
So, for example, your program may start like this:
#Hello World Program #This program prints the text 'Hello World' on the users screen #Created by Author on 01/01/2010
The next step is the program itself. For this we use the print statement.
#Hello World Program #This program prints the text 'Hello World' on the users screen #Created by Author on 01/01/2010 print "Hello World"Now to test this out simply press F5 on your keyboard, or choose Run -> Run Module. You'll be prompted to save your file first, don't forget your .py extension!
If your code is correct the Python Shell should open and display the text "Hello World." It should look like this:
IDLE 2.6.4 >>> ================================ RESTART ================================ >>> Hello World >>>
At this point you could be finished, but if you tried running the program in the Command Prompt(for Windows users) you'll notice the program opens and closes in the blink of an eye. To prevent this we will request input from the user. We will use the raw_input statement for this. For now we won't go over the raw_input statement, but you will see one of its uses in your program for now.
#Hello World Program #This program prints the text 'Hello World' on the users screen #Created by Author on 01/01/2010 print "Hello World" raw_input("Press the enter key to exit.")
To see what this does we will press F5 to run this program in the Python Shell. It should look like this:
>>> ================================ RESTART ================================ >>> Hello World Press the enter key to exit.The program will sit at this point until the user presses the enter key. Once that happens the program ends.
One thing you should know is that the text you were printing above is known as a string. When printing a string you must enclose it with quotation marks. You can use either single or double quotation marks, but be consistent. If you start with single, close with single. If you start with double, end with double. As you can see, we used double quotations in our program.
Hope this helps get some of you started!
Edited by Root23, 19 February 2010 - 06:34 PM.