Okay, now let's get back to some programming. We will write the traditional "Hello World" program - whenever you learn a new programming language, the first program that you write and run is usually a Hello World program. As Simon Cozens (one of the leading Perl6/Parrot hackers) puts it, it is the traditional incantation to the programming gods to help you learn the language better.
Open your editor of choice, enter the following program and save it as hello_world.py. All Python programs usually have the file extension .py .
Run this program by opening a shell (Linux terminal or DOS prompt) and entering the command python hello_world.py. If you are using IDLE, use the menu
-> or the keyboard shortcut Ctrl-F5. The output is as shown below.If you got the above output, congratulations! You have successfully run your first Python program. If you got an error, please type the program exactly as above and run the program again. Note that Python is case-sensitive i.e. print is not the same as Print - note the lowercase p in the former and the uppercase P in the latter. Also, ensure there are no spaces or tabs before the first character in each line - we will see later why this is important.
Consider the first two lines:
#!/usr/bin/python # Filename : hello_world.py
These are called comments. Anything to the right of the # character (which is not inside a string) is a comment and is mainly useful as notes for the reader of the program. Python does not use comments in any way.
However, the first line in this case is special. It is called the shebang line. Whenever the first two characters of the source file are #! - followed by the location of the interpreter it tells your Linux/Unix system that this program should be run with this interpreter, when you execute the program. This is explained in detail in the next section. Note that you can always run the program on any platform using the interpreter directly by running the command python program.py .
Please use comments sensibly in your program so that readers of the program can easily understand the program when they read it. Remember, that person could be yourself after six months!