We can use the Python interpreter to run our programs as and when we write the code.
First, we have to open a command line. Linux/BSD/Mac users can open up a shell emulator of their choice such as konsole or gnome-terminal. Windows users can open up the DOS prompt by running -> , enter the word cmd (in Windows XP) or command (in older versions) and press the enter key.
Now, we need to run the Python interpreter. In the command line, enter the word python and press the enter key. You should see something like this:
C:\Documents and Settings\Admin>python ActivePython 2.4 Build 244 (ActiveState Corp.) based on Python 2.4 (#60, Feb 9 2005, 19:03:27) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
Notice that >>>
is the prompt
waiting for you to enter your Python code.
Now enter print 'Hello World'
followed by enter key. You should now see the
words Hello World
printed on your
screen by Python just like you asked it to.
C:\Documents and Settings\Admin>python ActivePython 2.4 Build 244 (ActiveState Corp.) based on Python 2.4 (#60, Feb 9 2005, 19:03:27) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print 'Hello World' Hello World
Now wasn't that easy? :-)
If you get an error like this:
C:\Documents and Settings\Admin>python ActivePython 2.4 Build 244 (ActiveState Corp.) based on Python 2.4 (#60, Feb 9 2005, 19:03:27) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print 'Hello World' File "<stdin>", line 1 print 'Hello World' ^ SyntaxError: invalid syntax >>>
it means that you entered some spaces before the word
print
. You will soon learn why Python behaves
this way. For now, type the same line again without having any
spaces or tabs before the print
word and it
should work.
Notice that Python gives you the 'output' of the program you entered almost immediately. Yes, what you entered is a real and complete Python program.
print
is a statement used to,
unsurprisingly, print anything that is supplied to it. In this
case, we are supplying the text 'Hello
World'
and the print
statement
promptly prints it to the screen.
If you are using Linux/BSD/Mac, press Ctrl-d.
If you are using Windows, press Ctrl-z followed by enter key.