Krishnaprasad Varma bio photo

Krishnaprasad Varma

Programmer, Ruby Enthusiast, Gopher interested in Python & Javascript. Recently developed an interest in Gardening.

Email Twitter Google+

Python is a general purpose, high level programming language. Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles.

The design of Python offers some support for functional programming in the Lisp tradition. The language has map(), reduce() and filter() functions; comprehensions for lists, dictionaries, and sets; and generator expressions.[39] The standard library has two modules (itertools and functools) that implement functional tools borrowed from Haskell and Standard ML.[40]

Dynamic Typing

Python is a dynamically typed language where every variable name is bound only to an object. Hence it is possible to bind a name to objects of different types at runtime.

Python author = ‘Krishnan’ # the name ‘author’ is now a name for the string ‘Krishnan’ author = 100

The name ‘author’ stops being a name for the string ‘Krishnan’, and starts being a name for the integer 100

This is as against the statically typed language where every variable name is bound with both a type and an object. An exception will be thrown when the varialbe name is tried to bound with an object of a different type.

Java

int marks // Declares the integer varialbe named marks int marks = 89 // Assigns the value 89 to the variable marks

int marks = ‘Hello’ // Throws Error as the type doesn’t match

Automatic Memmory Management (Garbage Collection)

Most of the users are familiar with C’s malloc and calloc fo preallocating and deallocating memmory. Python does all this by itself. Python’s memory allocation and deallocation method is automatic.

Python uses two strategies for memory allocation reference counting and garbage collection. Reference counting works by counting the number of times an object is referenced by other objects in the system. When references to an object are removed, the reference count for an object is decremented. When the reference count becomes zero the object is deallocated.

Reference cycles are complex and take computational work to discover. Hence Garbage Collection must be a scheduled activity. Python schedules garbage collection based upon a threshold of object allocations and object deallocations. When the number of allocations minus the number of deallocations are greater than the threshold number, the garbage collector is run.

Large Standard Library

Python’s Standard library contains built-in modules which are written in C that provide access to system functionality such as file I/O. It also contains modules written in Python that provide standardized solutions for many problems that occur in everyday programming.

You can get a better idea of python standard library here

Compiled to bytecode and bytecode Intepreted

C is a compiled language, as you need to compile to source code before you can execute it. Compilation depends on the machine architecture.

A language like ruby is an intepreted language, as every line of code in ruby is intepreted at the run time.

So, what about Python ? Is it compiled or intepreted?

Python will fall under byte code interpreted. .py source code is first compiled to byte code as .pyc. This byte code can be interpreted (official CPython), or JIT compiled (PyPy). Python source code (.py) can be compiled to different byte code also like IronPython (.Net) or Jython (JVM). - source: google.com

The .pyc files you see are byte code for the Python virtual machine (similar to Java’s .class files). They are not the same as the machine code generated by a C compiler for a native machine architecture. Some Python implementations, however, do consist of a just-in-time compiler that will compile Python byte code into native machine code. - source: stackoverflow, user: mipadi, url: http://programmers.stackexchange.com/questions/24558/is-python-interpreted-or-compiled

References