Saturday 21 December 2013

Python Hello World Example

We can run this program in two ways.
  1. Interpreter prompt / gnome terminal in Ubuntu.
  2. As a source file.

In Interpreter:
Open the interpreter on the command line. Then enter 'python' at the shell prompt. Then you can see in your shell as below.

$ python

Python 2.7.3 (default, Jul 5 2013, 08:17:30)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Now enter print 'Hello World' followed by the Enter key.

>>> print 'Hello world'

OUTPUT
Hello world

This is very simple and basic example. To quit the python prompt, In linux you type Ctrl+d and in windows Ctrl+z followed by Enter.

As Source file

Open an editor and type as above.
Note: When you choose editor, choose good editor which shows the Syntax coloring, indenting, autocompletion, and source-navigation tools. vim editor is one of the good choice. You can find some other python editors here https://wiki.python.org/moin/PythonEditors

print 'Hello World'

Save the file with file name followed by '.py' extension. Then open the shell and run the program by typing python file_name.py and enter.

$ python
Python 2.7.3 (default, Jul 5 2013, 08:17:30)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

$ python helloworld.py

OUTPUT
Hello World

If you get this output, then your program is running fine.

No comments:

Post a Comment