Most Recent Blog

King's View of the Battle

 Open GL model of a chess board.

March 9, 2011

Python - Hello World

Writing hello world in python is almost to simple to justify a post, however there are some caveats and for completeness of this group of posts, it will be included.

Step 1 Install Python 3.2 
Once installed, make sure that you can access the python executable.  If python is not in the path, you will need to add it.


Step 2 Create a Python Source file
______________________Source File____________________________
#!/opt/python
print ("Hello World in Python from ecocrypt.blogspot.com")
______________________End Source____________________________
save the source to a file called helloworld.py  (or any other name of your choice)


Step 3 Tell the Python Interrupter to execute the instructions in the file
prompt % python helloworld.py
Hello World in Python from ecocrypt.blogspot.com


This entire exercise could have been done from within the python interrupter.  To start the interpretor simply type 'python' at the command prompt this will give you a python interpretor prompt:


>>>
>>>print ("hello world in python from ecocyrpt.blogspot.com")
hello world in python from ecocyrpt.blogspot.com
>>>
type quit() to leave the interpretor


SOME COMMON ERRORS:
Using the wrong Python Syntax for version 3.2.

>>> print "Hello World"
  File "<stdin>", line 1
    print "Hello World"
                      ^
SyntaxError: invalid syntax
>>>

There were some syntax changes between python 2.7 and 3.2.
This fails because the print method is not enclosed in braces().
Try >>> print ("Hello World")



No comments:

Post a Comment

All comments are greatly appreciated! Comments will be moderated, and usually appear within 1 day. If you like a post, instead of saying you like it, vote for it or digg it (links provided). If there is any ERROR, PLEASE Comment I strive for good quality complete content.