Saturday, June 18, 2011

Introduction to python

Python is an easy to learn and powerful programming language.In python code is automatically compiled to byte code and executed.So python can be used as a scripting language.

Features of python
  • Simple and Easy to Learn
  • Free and Open Source
  • High-level Language
  • Built-in high level data types: strings, lists, dictionaries.
  • The usual control structures: if, if-else, if-elif-else, while.
  • Multiple levels of organizational structure: functions, classes, modules, and packages.

Python programs are executed by interpreters. There are two modes to use the interpreter.
  • Interactive mode
On a Linux platform you should be able to do this by typing python at a command prompt. On windows you should double click on the Python icon.This will launch the interactive interpreter.

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 34+45
79
>>> a="haiii"
>>> a
'haiii'
>>> len(a)
5
>>>
  • Script mode In script mode code is stored in a file.Interpreter is used to execute the contents of the file.Python scripts saved with extension .py.

Creating and running programs

Python programs are just text files that contain instructions for the Python interpreter. You can create them with any text editor .In Linux we can run the program by typing something like ./program.py.

Comment :
Any line which starts with a hash (#) is a comment. It will be skipped by the interpreter. There is no multi line comment in Python, you should start every line with a hash instead.

Indentation: In python white space indentation effect the meaning.A logical block will have the same indentation.If one of the line of a logical block has a different indentation it will result in syntax error.

Code is checked at runtime: Python does very little checking at compile time.Python only checks a line when it runs that line.

No comments:

Post a Comment