Wednesday, October 12, 2011

Python Tutorial 1:Running your first program....


So we begin our journey into the exciting world of python.You are going to learn a very beautiful language and a powerful one too.So first we begin with very basic i.e. how to run python programs and how to save your python files.

The very common python files have extensions like .py and .pyw just like C or C++ where the extensions are .c and .cpp respectively.
So if you encounter any such files you know which language's code is in that file.

To run your python programs or scripts you need to save them with .py extension and they will run in a console window.The .pyw extension is for the programs that don't need a console i.e. your programs that have a GUI.

So first of all you should have python installed.For installation refer to this post.

Now,
After installing you are ready to run python programs.If you are on windows and installed python using a .exe file you'll get a start menu entry of Python "version".From there you can launch the command line python interpreter or an IDE that comes with it known as IDLE.


The python interpreter is an awesome tool to try out some stuff you just learned without having to compile just a line or two.So whatever you learn just fire up the interpreter and try it out.

Now we begin...

1. Python is an interpreted language i.e. the interpreter interprets every line and halts if it finds
    an error in that line whereas in C or C++ which are compiled languages the whole program is
    checked for errors.

2. Python is a dynamically typed language that means you don't have to declare variables like
    you did in C,C++ or Java.
    So myvar=5 is valid in python but not in C,C++ or Java.

3. Python is case sensitive just like C or C++.
    So myvar is different from myVar or Myvar.

4. You dont have to put a semicolon to end a statement.In python a new line tells the
    interpreter that this is a new statement.

5. To use comments use # .
     myvar=5  #This is a comment.
     comments are ignored by the interpreter and are not executed.

6. To print something use print

     print "Anything that goes here is printed as it is!! "

So thats it...with these points in mind we can write a simple hello world program in python

The code is:
 
    print "Hello world!!" #This is a print statement!

and save it as hello.py.

Now you can run this in windows or linux using command python hello.py and you'll be greeted with a hello world message.You can also execute this program by double clicking the python file hello.py but the console will just exit quickly and you won't be able to see the output.We'll see how to remove this in upcoming posts...

Thats it for this post.Next post will be about data types and some python magic!! :)
So stay tuned ;)

If you liked this post don't forget to like and share it with your friends.You can also comment below if you have anythong to ask or share....



1 Comments
Comments
Unknown said...
This comment has been removed by a blog administrator.