Linux Programming (Python)-Part 1-Introduction

Python is a dynamic programming language that combine great power with very clear syntax. Guido Van Rossum began to work with python in late 1980 at National Research Institute for Mathematics and Computer science in Netherland (CWI). Before I read the history, I though it came from name of snake. But I was wrong. Guido named it when he saw ‘Monthy Python’s Flying Circus‘ comedy TV show in BBC.

Write your own first syntax
Well, no need to explain more details about python, you can find it in the Internet. Just let’s start.
Open Linux Terminal (Applications > Accessories > Terminal) then type: python.
You will get ‘>>>>’ prompt. It’s mean that python is ready to get any commands.
Try to check with few commands, such as ‘help‘, ‘copyright‘, ‘credits‘ and ‘license‘. To quit, just type ‘quit()‘.

Now, go to python again, type:

print “Hello World!!! I’m python”, then press Enter.

Congratulation, you did first your syntax programming in python.

There is another method to to run the python program. It’s called script mode. The first method that you did above is called Interactive mode. To use python script mode you will need a text editor. Don’t forget to put ‘.py‘ for the file extension otherwise python will not run your program.

In this sample, I use vim. You can use any text editor (vi, nano, emacs, etc) that you are familiar with.

We will do the same syntax, print “Hello World!!! I’m python” and for the filename I use ‘hello_wolrd.py‘.

Type the syntax and save it.

Go to the directory where you saved the file and type “python hello_world.py”.

The command is “python your_filename.py”.

Congratulation, you did your first python script mode programming.

Actually, python has a ‘text editor‘ and ‘shell‘ that will make you easier to do both, interactive and script mode programming. But, I have to explain the optional methods above, so you will have more flexibility.
The application is called, idle.
You can download it directly from the Internet using command ‘$sudo apt-get install idle‘.

Installation is done.

Open your ‘Running Application’ dialog box and type ‘idle‘.
In my Ubuntu, I just type ‘ALT-F2′. You may set any shortcut that you like.
Just set it through (System > Preferences > Keyboard shortcuts).

Welcome to Idle.
It’s almost the same when you did it with Linux Terminal but you can save, edit and run the file directly which make your python programming easier.

As you see, I type the same syntax, run it and got the same output.

To use text editor in Idle, select ‘File > New window‘ and type the syntax program in it.

Select ‘File > Save‘ from the idle menu, go to directory that you want to save the file, type the filename and press ‘save‘ button. You don’t have to give the extension. Idle will automatically add it for you.

With Idle, you can test your python scripts directly.
Just select ‘Run > Run Module‘, you will see the result in the python shell screen.

Now, you know how to use python in ‘interactive‘ and ‘script‘ mode. There is another method that you have to know also. It’s ‘executable‘ mode.
From Idle, select new window, type the scripts below, and save it as ‘hello_world3.py‘

Now open your Linux Terminal again, go to your python directory.

Change the ‘access right‘ to ‘executable‘ for both, ‘hello_world.py‘ and ‘hello_world3.py‘ using ‘$chmod +x hello_world.py‘ and ‘$chmod +x hello_world3.py‘.