What Is Python?
Python is an “open source, general-purpose programming language that is dynamic, strongly typed, object-oriented, functional, memory-managed, and fun to use.” Those are a lot of adjectives for one sentence! Let’s unpack them one at a time.
Python is distributed under an open source, BSD-style license called the Python Software Foundation License Agreement. It is a very permissive license that allows great flexibility in how Python can be used. Python’s development is done in the open by a large and diverse community of volunteers.
Python is general purpose in that you can use it to build a variety of applications running the gamut from simple scripts and command line tools to desktop and web applications, network servers,scientific
applications, and more.
We know that Java is a statically typed language; that is, the types are checked and enforced at compile time. In contrast, Python is dynamic,which means that the types are checked only at runtime. But Python is also strongly typed, just like Java. You can only execute operations that are supported by the target type.
Another way to think about this is that in Java, both variables and objects have types associated with them; whereas in Python, only objects have types, not the variables that they are bound to. In Java, when we declare
MyType obj = new MyType()
The obj variable is declared of type MyType and then the newly instantiated object of type MyType is assigned to it. In contrast, in Python, the same declaration would read
obj = MyType()
Ignoring the missing new keyword (which Python doesn’t have), obj is simply a name that is bound to the object on the right, which happens to be of type MyType. We can even reassign obj in the very next line—obj = MyOtherType()— and it wouldn’t be a problem. In Java, this reassignment would fail to compile1 while in Python, the program will run and will only fail at runtime if we try to execute an operation via obj that is incompatible with the type assigned to it at that point in time.
Python is object oriented and supports all the standard OOP features that Java has like creation of types using classes, encapsulation of state, inheritance, polymorphism, and so forth. It even goes beyond Java and supports features such as multiple inheritance, operator overloading, meta-programming, and so forth.
Python also supports a rich set of functional programming features and idioms. In Python, functions are first-class objects that can be created, manipulated, and passed around just like any other object. While its emphasis on functional programming might not be as focused as say Clojure, Python certainly offers much more to the functional programmer than Java.
Another similarity between the languages is in terms of manual memory management, in that there is none. The language runtime takes care of correctly allocating and freeing up memory, saving the programmer from the drudgery—and mistakes—of manually managing memory.
Having said that, the JVM garbage collectors are much, much better performing than the Python GC. This can become a concern depending on the type of application you are building. Finally, and above all, Python is fun and a joy to use. This is a strong claim to make but I hope that by the time you are done reading this book, you’ll agree with me and the millions of other Python programmers out there!
History
Python is the brainchild of a Dutch programmer named Guido van Rossum. He started working on it when he got frustrated with the ABC language in the late 1980s and after some years of private development, he released the first version of Python in 1994. This actually makes Python older than Java, the first version of which was released in 1996, a full two years later! A comparison of the two languages is shown in Table -
Historical comparison of Java and Python
Since then, the language has continued to refine and evolve, with Python 2.0 being released in 2000. As of this writing, the 2.x versions are the most widely deployed.
In version 3.0, the language designers decided to break backward compatibility in order to clean up some of the accumulated language warts. Although this has been good from a language perspective, it has been a significant hindrance to those upgrading from 2.x to 3.x. Imagine if Sun had decided to introduce generics in Java 5 without type erasure, thus breaking backward compatibility. The Java language would’ve been much nicer today but the transition period would’ve been difficult, to say the least. That is the kind of transition the Python user community is going through right now.
From the outset, Python’s development has been done in the open with a community of volunteers contributing to the language and the core libraries. Any language changes are proposed and discussed through a process called PEP (Python Enhancement Proposals), with Guido having final say in deciding the outcome. For his stewardship and continuous involvement in the development of Python, Guido is affectionately called the “Benevolent Dictator For Life.” He also periodically writes a Python History blog chronicling the evolution of various language features.
Installation
This book is full of example code, and the best way to follow along is to actually try these examples by yourself. To do this, you’ll obviously need to install Python on your system. But an easier way is to check if you already have access to a system with Python installed! Almost all systems running Linux should have Python preinstalled. Recent versions of Mac OS X also come with Python preinstalled. Just open a command shell on either of these two systems and type in python. If you get a Python shell prompt, you are all set! The version of Python installed may be a bit outdated but it should be sufficient to get started.
Posted on Utopian.io - Rewarding Open Source Contributors