Saturday, January 26, 2013

First python program

I am new to python, so I first want to create the traditional "hello world" program in python and see if it works.
On my raspberry pi, python was already installed and added to the path.
There are different Version installed (standard 2.7 and the newer 3.2). Check your version:
python --version
Open an editor and type the following program:
#!/usr/bin/python
print "hello world"
Save as "test.py". It can be executed either by calling the python binary or making the file executable (asuming you are in the same directory as the python script file):
python test.py
chmod +x test.py   //sets the executable flag
./test.py
Or if you want to run it as phyton 3.2:
python3.2 test.py
This will output an error, because in the 3.x version of python the "print" command is a function and so we have to use it with parenthesis like this:
print("Hello world")
 Now it should work and print "Hello world" to stdout. Nice.

1 comment: