First some basic notions
Interactive mode and script mode programming
Interactive mode and script mode programming gives the developers 2 very important tools. The first is all about executing sentences directly in the interpreter, this is very useful when you are developing, since you can test code directly in the interpreter without executing your application or simply execute tests to your own code.
Script mode programming is creating your own modules and tell the interpreter to execute your application. This is what you do if you want to create a package with the application you’ve created. These modules are created across 1 or more files and executed calling the interpreter with the file name as a parameter.
$ python shrubbery.py
This will execute the shrubbery.py file. You can also execute an application in interactive mode, for that you just need to pass the -i parameter to the interpreter and instead of Python exiting when the program finishes, it starts an interactive session. This is useful when it comes to debug.
$ python -i shrubbery.py
A debugger is a must on every language and python is no less. Executing python with -m pdb parameter makes the interpreter enter debugging when executing an application. After this you can set breakpoints, execute line by line statements or view the value of the variable in your application. We will come back to this in a later post.
$ python -m pdb shruberry.py
The debugger can also be invoked using the import pdb statement in the module, but we will go into more depth in the debugger in a later article.
Executing an application without calling the interpreter
Applications can be executed by at least 2 ways, calling the python interpreter or by adding the #!/usr/bin/python statement in the beginning of the script file (this only works in Unix or Linux and to use it you need to set the environment variables, to point at the python interpreter location, and set the permission of the file to executable).
To find out where your python interpreter is installed:
$ sudo find / -name "python"
After executing the previous, you should get the location of your python interpreter, then you need to set the environment variable, in my case python executable is located in /usr/bin/python
$ export PATH="$PATH:/usr/bin/python"
Then you need to set the file attributes to executable, you can look more into file attributes in Unix/Linux here http://en.wikipedia.org/wiki/Chmod
$ chmod +x shrubbery.py
And finally to execute your application
How to say Ni
With your favorite editor, create a file named shrubbery.py and add the following code
#!/usr/bin/python print ("We are the knights who say NI")
So, for in the first line we have the location of our python interpreter, after that, we say the interpreter to print the “We are the knights who say NI” text to the screen. If we run this script we should get the text printed on our console.
This is where to start when you are learning a new programming language, you can find the Hello World program in 87 diferent languages here http://en.wikipedia.org/wiki/Hello_world_program_examples