Python : A tutorial for beginners


20 Feb  

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.

 

python - ubuntu command line

 

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.

 

install plugins in netbeans

 

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.

 

install python in netbeans

 

You will now see a Window as shown below:

 

configuring python in netbeans

 

Click on the ‘manage’ button and then choose ‘New’ from the new window that has popped up.

 

configuring python platform in netbeans

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:

 

python location in ubuntu

 

And if you are using a Windows machine then go to your Python installation directory and select the python.exe file. Then, click open.

 

python location in Vista

 

Now change the default platform to Python 2.6.4 (or the version you have installed)

 

python platforms

 

Similarly change the ‘Python platform’ in the projects details (see the picture):

 

python netbeans project

 

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.
 

netbeans python

 

If you wish to have a variable, you can use:

 

var = 1print "variable=", var
 
This also is self explanatory. When you execute the program, it will display “variable=1”.
 
Here is another piece of code that shows the use of loops in Python. (We have employed conditional statements – ‘if — else’ inside the ‘while’ loop, in this code)

 

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
 
 
You may note that  indentation is very important in in Python. But the IDE will help you to format your code in the right way. Another thing that you need to note here is the absence of parenthesis that we use normally in the codes. Instead you can find the symbol ‘:’

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:

 

executing python code in netbeans 

 

Another feature is that Python allows you to define your own functions in peculiar way. The syntax for it is:
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()
 
When you issue ‘hello()’, the function will get executed and will display the text.
 
Here is a slightly modified code that allows you to pass a parameter to the function :

 

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.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Tags: , , ,


TechBlog on Facebook

Comments (1)

 

  1. glassfish says:

    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)

Leave a Reply