|From The Quick Python Book, Third Edition by Naomi Ceder
Read this article if you want a basic overview of how Python compares to other languages, and its place in the grand scheme of things.
This article covers:
- Reasons to use Python;
- What Python does well; and
- Reasons to learn Python 3.
Why should YOU use Python?
Hundreds of programming languages are available today, from mature languages like C and C++, to newer entries like Ruby, C#, and Lua, to enterprise juggernauts like Java. Choosing which language to learn is difficult. Although no one language is the right choice for every possible situation, Python is a good choice for many programming problems, and it’s also a good choice if you’re learning to program. Hundreds of thousands of programmers around the world use Python, and the number grows every year.
Python continues to attract new users for a variety of reasons. It’s a true cross-platform language, running equally well on Windows, Linux/UNIX, and Macintosh platforms, as well as others, ranging from supercomputers to cell phones. It can be used to develop small applications and rapid prototypes, but it scales well to permit development of large programs. It comes with a powerful and easy-to-use graphical user interface (GUI) toolkit, web programming libraries, and more. And, best of all – it’s free.
What Python does well
Python is a modern programming language, developed by Guido van Rossum in the 1990s (and named after a famous comedic troupe). Although Python isn’t perfect for every application, its strengths make it a good choice for many situations.
Python is easy to use
Programmers familiar with traditional languages will find it easy to learn Python. All the familiar constructs, such as loops, conditional statements, arrays, and so forth, are included, but many are easier to use in Python. Here are a few of the reasons why:
- Types are associated with objects, not variables. A variable can be assigned a value of any type, and a list can contain objects of many different types. This also means that type casting usually isn’t necessary, and your code isn’t locked into the straitjacket of predeclared types.
- Python typically operates at a much higher level of abstraction. This is partly the result of the way the language is built, and partly the result of an extensive standard code library that comes with the Python distribution. A program to download a web page can be written in two or three lines!
- Syntax rules are simple. Although becoming an expert Pythonista takes time and effort, even beginners can absorb enough Python syntax to write useful code quickly.
Python is well suited for rapid application development. It isn’t unusual for coding an application in Python to take one-fifth the time that it would take if coded in C or Java, and require as little as one-fifth the number of lines of an equivalent C program. This depends on the application; for a numerical algorithm performing mostly integer arithmetic in for loops, there’d be a much smaller productivity gain. For the average application, however, the productivity gain can be significant.
Python is expressive
Python is an expressive language. Expressive in this context means that a single line of Python code can do more than a single line of code in most other languages. The advantages of a more expressive language are obvious – the fewer lines of code you write, the faster you can complete the project. Not only that – the fewer lines of code there are, the easier the program will be to maintain and debug.
To get an idea of how Python’s expressiveness can simplify code, let’s consider swapping the values of two variables, var1 and var2. In a language like Java, this requires three lines of code and an extra variable:
int temp = var1; var1 = var2; var2 = temp;
The variable temp is needed to save the value of var1 when var2 is put into it, and then that saved value is put into var2. The process isn’t terribly complex, but reading those three lines and understanding that a swap has taken place takes a certain amount of overhead, even for experienced coders.
In contrast, Python lets you make the same swap in one line of code, in a way that makes it obvious that a swap of values has occurred:
var2, var1 = var1, var2
This is a simple example, but you find the same advantages throughout the language.
Python is complete—“batteries included”
Another advantage of Python is its “batteries included” philosophy regarding libraries. The idea is that when you install Python, it includes everything you need to do real work, without the need to install additional libraries. This explains why the Python standard library comes with modules for handling email, web pages, databases, operating system calls, GUI development, and more.
For example, with Python, you can write a web server to share the files in a directory with two lines of code:
import http.server http.server.test(HandlerClass=http.server.SimpleHTTPRequestHandler)
There’s no need to install libraries to handle network connections and HTTP—it’s already in Python, right out of the box.
Python is cross-platform
Python is also an excellent cross-platform language (we mentioned this earlier). Python runs on many different platforms: Windows; Mac; Linux; UNIX; and so on. Because it’s interpreted, the same code can run on any platform that has a Python interpreter, and almost all current platforms have one. Several versions of Python run on Java (Jython) and .NET (IronPython), giving you even more platforms to run Python.
Why learn Python 3?
Python has been around for many years and has evolved over that time. This article refers to Python 3.5.
Python 3, whimsically dubbed Python 3000, is notable because it’s the first version of Python in the history of the language to break backward compatibility. What this means is that code written for earlier versions of Python probably won’t run on Python 3 without some changes. In earlier versions of Python, for example, the print statement didn’t require parentheses around its arguments:
In Python 3, print is a function and needs the parentheses:
You may be thinking, “Why change details like this, if it’s going to break old code?” Because this kind of change is a big step for any language, the core developers of Python thought about this issue carefully. Although the changes in Python 3 break compatibility with older code, those changes are small, and for the better—they make the language more consistent, more readable, and less ambiguous. Python 3 isn’t a dramatic rewrite of the language; it’s a well-thought-out evolution. The core developers also took care to provide a strategy and tools to safely and efficiently migrate old code to Python 3.
Why learn Python 3? Because it’s the best Python, and as projects switch to take advantage of its improvements, it’ll be the dominant Python version for years to come. The porting of libraries to Python 3 has been steady since its introduction, and by now many of the most popular libraries support Python 3. In fact, according to py3readiness.org, 319 of the 360 most popular libraries have already been ported to Python 3. If you need a library that hasn’t been converted yet, or if you’re working on an established codebase in Python 2, stick with Python 2.x; but if you’re starting to learn Python or starting a project, then go with Python 3—not only is it better, it’s the future.