Lets start our Python journey by first gaining an understanding of what Python has to offer thats different from Java. Ill then help you get setup with Python before we dive into the languages syntax in the next chapter.
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! Lets 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. Pythons 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 doesnt 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 wouldnt be a problem. In Java, this reassignment would fail to compile 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 drudgeryand mistakesof 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, youll 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 .
Table 1-1
Historical comparison of Java and Python
Java | Python |
---|
James Gosling | Guido van Rossum |
From C++/Oak | From ABC |
1.0 - Jan 1996 | 1.0 - Jan 1994 |
9.0 - Sep 2017 | 3.6 - Dec 2016 |
JSR | PEP |
Commercial | Community |
Note
Ill use this tabular format to compare and contrast Python and Java whenever it makes sense.
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 wouldve been much nicer today but the transition period wouldve been difficult, to say the least. That is the kind of transition the Python user community is going through right now.
Note
Since 2.x is still the most widely used version of Python, this book will cover Python 2.x features and syntax, calling out any differences with 3.x from time to time.
From the outset, Pythons 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.