A few weeks ago, I spent a few days learning Python using LPTHW.
Starting this week, I began my deep dive into Python exploring each feature of the language in depth.
This post is the first of many that attempts to document everything I have been learning about the language.
Why learn Python?
Python is a simple, coherent language that is easy to learn. It has a small core and focuses on readability. Python programs, even the ones you did not write, are easy to read and understand. This leads to re usability and ease of maintenance.
Python has support for object oriented programming. You do not have to use it, but it is there if you need it.
Programs written using Python are typically smaller than similar programs written in static compiled languages like C++, Java and C#. This means that there is less code that needs to be written and maintained.
It comes with a standard library that has support for a lot of tasks that are part of application development. It has modules for network programming, regular expressions, parsing different forms of markup and many more.
Python programs are portable, in most cases, all you have to do is copy the source files from one OS to another. Programs can be called from and can call programs written using C, C++, Java and .NET. This makes it a good choice for extending programs or as a integration language.
Because Python builds out on a small core, the language is remarkably consistent. This makes it easy to remember. I experienced this first hand when writing a simple program to parse an xml file. I never found myself reaching for a reference book to look up the usage for syntax.
A lot of libraries useful in various application domains that are written using languages like C, C++, Java and C# have a front end Python API that make it easy to integrate them in Python programs. NumPy and SciPy are some of the popular ones.
Python is often referred to as a scripting language. I have found that this often leads to confusion. To some, a scripting language is something that is used to code operating system level tasks, to others, it signifies the ability of a language which can be used to integrate various application components written using C++ and Java.
Python is in fact used in these roles, but is not limited to them.
You can use Python to build line of business (industry jargon for boring & un-exciting) applications. You can use Python to build any application that you would build using languages like Java and C#.
Speed of Execution vs Speed of Development
The question – ’..but is it fast’ always pops up when discussing the viability of any programming language. Let’s not forget that speed of development is often preferred over speed of execution for applications in most domains.
As per the standard Python implementation, Python code is first compiled to byte code. You can think of this as low level portable code. The Python interpreter then interprets and executes the byte code. Because the Python source code is first compiled to byte code and not binary machine executable code it results in a decrease in performance when compared to code written in languages like C which are always compiled down to machine level code.
Certain tasks such as file processing in Python are dispatched to compiled C code inside the Python interpreter and run at native speeds.
If you still want the programs to run at native execution speeds, you can code those parts in languages like C or C++ and call those compiled extensions using Python.
Some technical strengths
Python is dynamically typed, which means that you, the developer, do not have to write mountains of code that do nothing but declare types. In Python, there is no concept of declaring variable or assigning types ahead of time. Types are created when expressions are evaluated at runtime.
Python has a garbage collector that reclaims objects that are no longer being used. You do not have to write code to manage memory.
Inheritance, Polymorphism, Operator Overloading are all supported in Python. You don’t have to use OOP for programs, but it has support for OOP if you need it.
Built in objects
The language and its standard library has built in objects that are used in everyday programming tasks. Objects like lists, dictionaries, tuples, strings are part of the standard language. Python also has built in tools that are used to manipulate these objects.
The standard Python implementation is written using ANSI C. This can be compiled and run on virtually every hardware platform. Most of the standard library that comes with Python is also portable.
Most of Python’s power comes from the fact that polymorphism is one of the key parts of it’s design.