What is Python?
It's a programming language!
So what is Python? Simply put, Python is a programming language. It was initially developed by Guido van Rossum in the late 1980's in the Netherlands. Guido continues to be actively involved in guiding the development and evolution of the language, so much so that he's been given the title "Benevolent Dictator for Life", or, more commonly, BDFL. Python is developed as an open-source project and is free to download and use as you wish. The non-profit Python Software Foundation manages
Python's intellectual property, plays a strong role in promoting the language, and in some cases funds its development.
On a technical level, Python is a strongly typed language. This means that every object in the language has a definite type, and there's generally no way to circumvent that type. At the same time, Python is dynamically typed, meaning that there's no type-checking of your code prior to running it. This is in contrast to statically typed languages like C++ or Java where a compiler does a lot of type-checking for you, rejecting programs which misuse objects. Ultimately, the best description of the Python type system is that it uses duck-typing where an object's suitability for a context is only determined at runtime. We'll cover this in more detail in Chapter 8, Defining new types with classes.
Python is a general-purpose programming language. It's not intended for use in any particular domain or environment, but instead can be fruitfully used for a wide variety of tasks. There are, of course, some areas where it's less suitable than others – for example in extremely time-sensitive or memory-constrained environments – but for the most part Python is as flexible and adaptable as many modern programming language, and more so than most.
Python is an interpreted language. This is a bit of a misstatement, technically, because Python is normally compiled into a form of byte-code before it's executed. However, this compilation happens invisibly, and the experience of using Python is normally one of immediately executing code without a noticeable compilation phase. This lack of an interruption between editing and running is one of the great joys of working with Python.
The syntax of Python is designed to be clear, readable, and expressive. Unlike many popular languages, Python uses white-space to delimit code blocks, and in the process does away with reams of unnecessary parentheses while enforcing a universal layout. This means that all Python code looks alike in important ways, and you can learn to read Python very quickly. At the same time, Python's expressive syntax means that you can get a lot of meaning into a single line of code. This expressive, highly-readable code means that Python maintenance is relatively easy.
There are multiple implementations of the Python language. The original – and still by far the most common – implementation is written in C. This version is commonly referred to as CPython. When someone talks about "running Python", it's normally safe to assume that they are talking about CPython, and this is the implementation that we'll be using for this book.
Other implementations of Python include:
-
Jython, written to target the Java Virtual Machine
-
IronPython, written to target the .NET platform
-
PyPy, written (somewhat circularly) in a language called RPython which is designed for developing dynamic languages like Python
These implementations generally trail behind CPython, which is considered to be the "standard" for the language. Much of what you will learn in this book will apply to all of these implementations.
Versions of the Python language
There are two important versions of the Python language in common use right now:
Python 2 and Python 3. These two versions represent changes in some key elements
of the language, and code written for one will not generally work for the other unless you take special precautions. Python 2 is older and more well-established than Python 3, but Python 3 addresses some known shortcomings in the older version. Python 3 is the definite future of Python, and you should use it if at all possible.
While there are some critical differences between Python 2 and 3, most of the fundamentals of the two version are the same. If you learn one, most of what you know transfers cleanly to the other. In this book we'll be teaching Python 3, but we'll point out important differences between the versions when necessary.
It's a standard library!
Beyond being a programming language, Python comes with a powerful and broad standard library. Part of the Python philosophy is "batteries included", meaning that you can use Python for many complex, real-world tasks out-of-the box, with no need to install third-party packages. This is not only extremely convenient, but it means that
it's easier to get started learning Python by using interesting, engaging examples – something we aim for in this book!
Another great effect of the "batteries included" approach is that it means that many scripts – even non-trivial ones – can be run immediately on any Python installation. This removes a common, annoying barrier to installing software that you can face with other languages.
The standard library has a generally high level of good documentation. The APIs are well documented, and the modules often have good narrative descriptions with quick start guides, best practice information, and so forth. The standard library documentation is always available online, and you can also install it locally if you want to.
Since the standard library is such an important part of Python, we'll be covering parts of it throughout this book. Even so, we won't be covering more than a small fraction of it, so you're encouraged to explore it on your own.
It's a philosophy
Finally, no description of Python would be complete with mentioning that, to many people, Python represents a philosophy for writing code. Principles of clarity and readability are part of what it means to write correct or pythonic code. It's not always clear what pythonic means in all circumstances, and sometimes there may be no single correct
way to write something. But the fact that the Python community is concerned about issues like simplicity, readability, and explicitness means that Python code tends to be more…well…beautiful!
Many of Python's principles are embodied in the so-called Zen of Python. The "zen" isn't a hard-and-fast set of rules, but rather a set of guidelines or touchstones to keep in mind when coding. When you find yourself trying to decide between several courses of action, these principles can often give you a nudge in the right direction. We'll be highlighting elements from the "Zen of Python" throughout this book.