Hello World or How to Say Ni

First some basic notions

Interactive mode and script mode programming

(from http://en.wikibooks.org/wiki/Python_Programming/Interactive_mode)

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

The debugger

(from http://docs.python.org/library/pdb.html)

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

(from http://docs.python.org/tutorial/interpreter.html)

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

$ ./shrubbery.py

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.

$ ./shrubbery.py

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

About these ads

About Joao Lopes

I'm a software engineer at Universal Postal Union, with an interesting consultant background. I am very enthusiastic about science and technology and in my blog, I will try to report my introduction to Python experience, starting from scratch. You can check my professional profile here: http://ch.linkedin.com/in/jnlopes View all posts by Joao Lopes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

jobhunthero

Job Search, Resume and Interview advice from the trenches...

Cocheno

Another Python Tutorial

Engineer's Journey

From Bachelor to Master

Inside Python

Another Python Tutorial

Rui Jarimba

Yet another geek blog about software development

Follow

Get every new post delivered to your Inbox.

Join 34 other followers

%d bloggers like this: