Now let's get back to programming. There is a tradition that whenever you learn a new programming
language, the first program that you write and run is the 'Hello World' program - all it does is just say
'Hello World' when you run it. As Simon Cozens 1 puts it, it is the 'traditional incantation to the programming
gods to help you learn the language better' :) .
Start your choice of editor, enter the following program and save it as helloworld.py
Example 3.2. Using a Source File
#!/usr/bin/python
# Filename : helloworld.py
print 'Hello World'
(Source file: code/helloworld.py)
Run this program by opening a shell (Linux terminal or DOS prompt) and entering the command python
helloworld.py. If you are using IDLE, use the menu Edit -> Run Script or the keyboard shortcut
Ctrl-F5. The output is as shown below.
$ python helloworld.py
Hello World
If you got the output as shown above, congratulations! - you have successfully run your first Python program.
In case you got an error, please type the above program exactly as shown and 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 why this is important later.
No comments:
Post a Comment