What is Python?
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.
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.
Go to the directory where you saved the file and type “python hello_world.py”.
The command is “python your_filename.py”.
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‘.
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).
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.
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‘.
To run the script, just add ‘./‘ before the filename.