Today I am starting a new segment dedicated to programming in Python and this edition carries a tutorial for beginners. Python is a general purpose high-level programming language which is used widely in the scientific community. The prime reason for this wide acceptance is the code readability associated with the language. This means that even if you are a beginner, you can easily comprehend what exactly a program does just by looking at the code.
You need to install Python in your machine if you want to play around with it. You can download it from the official website. (If you are using a popular Linux distribution you can find this in the distribution’s repository).
After installing Python you can open your terminal or CMD (if you are in Windows) to execute Python codes. Well, this may not be a good idea if you are new to programming. You may find it hard to execute codes directly from this terminal or running a Python file (*.py) file using the command line way.
Well, the best way to get away with it is to use an IDE (Integrated development environment). You can use any good Python IDE for this, but for this tutorial I’m going to use Netbeans
Running Python in Netbeans : Setting up Netbeans for Python
In order to run Python programs within Netbeans you need to configure the IDE. First of all, you have to install the Python plugin for Netbeans. You can find this in the Tools –> Plugins menu. Then you can navigate to the ‘available plugins’ tab and search for ‘python’. Once you find the plugin, select and install it.
Netbeans, by default, is using Jython (Python implementation in Java). You can change this by selecting File—> New Project from the menu and then by clicking ‘Python’ from the options.
You will now see a Window as shown below:
Click on the ‘manage’ button and then choose ‘New’ from the new window that has popped up.
If you are in Linux go to the /bin/ (binary) directory and locate your Python binary (Sometimes, this will be in /usr/bin). You can try ‘which python’ command for finding this out:
And if you are using a Windows machine then go to your Python installation directory and select the python.exe file. Then, click open.
Now change the default platform to Python 2.6.4 (or the version you have installed)
Similarly change the ‘Python platform’ in the projects details (see the picture):
Done! You can now create a new project and start playing with Python
Python Introductory Tutorial
You may note that there are few differences when you migrate from command line to IDE (especially if you are beginner and was trying only simple commands). Say for example, when you are using the command line you can simple enter ‘1+1’ for finding out the value. But in the IDE it should be
print 1 + 1
Let’s write our first lines of code in Python:
str1 = "Our "str2 = "First "str3 = "Python Tutorial!"sentence = str1 + str2 + str3print sentence
The code illustrates the concatenation of strings. We have three strings and the programs joins all the three and prints out the resultant one.
If you wish to have a variable, you can use:
var = 1print "variable=", var
print 1 + 1x=5 while x!=0: print 'value of x is', x if x==3: print 'my if loop is ok' else: print 'x != 3' x=x-1
And here is the output of the code given above:
value of x is 5
x != 3
value of x is 4
x != 3
value of x is 3
my if loop is ok
value of x is 2
x != 3
value of x is 1
x != 3
(Beginners may note that ‘==’ is used for equating with a value and ‘!=’ means ‘is not equal to’)
If you know the basic rules of programming (in any language) you can easily understand Python. Hence in this tutorial the emphasis is given on the differences.
Python supports various inbuilt functions. For example, the function ‘raw_input’ allows you to get an input value from the user.
func = raw_input ("Enter some value and press enter\n ")print func
Here you can see the code in action:
def function_name(your_parameter_1,your_parameter_2):
You can give any name for your function and define any number (or null) of parameters.
Let me show you a simple function to illustrate this:
def hello(): print "hello world" return hello()
def hello(name): print "The name of this site is", name return hello("techblog")
We will be covering topics like using dictionaries, tuples, lists, I/O (input/output) operations etc in the upcoming tutorials.




Join Techblog
Facebook Group
Read
Digg entries
Add techblog to
Google reader
Good tutorial for beginners. It also would be great to mention that the method naming is different from JAVA
(under score usage is not couraged in java)