An introduction to Python
In this article I will give you an introduction to Python 3. This will be the first article of a series about Python with the intention to give you a tutorial where you can start learning Python and have hands-on examples to ease your learning efforts.
This article is an excerpt of my book Python 3 in Anger. For a more detailed version take a look at the book.
Python is a high-level, interactive, object-oriented and interpreted scripting language. It was designed to have a readable source code and it uses English keywords more frequently where other programming languages use punctuation.
Let’s see what each definition hides behind its name:
- High-level: Python is easy to write and to read by developers. As deeper you go to the machine level the code becomes less readable — and at the end, when the computer executes the commands it are only a series of ones and zeros.
- Interactive: Python enables you to code right into the interpreter and your code gets executed as you hit return.
- Object-oriented: Python supports object-oriented development style which encapsulates information into objects.
- Interpreted: Python is processed at runtime by the Python interpreter so you do not need any compiling before executing. This is similar to PHP or Perl or shell script.
Why should I care?
If you are not convinced already to learn Python I can only tell you one thing: big companies use Python for their development. Some of these big companies are: Google, Dropbox and Disney.
Each of these companies uses Python in different ways. Google’s Youtube is primary based on Python; the Google App Engine’s first version was developed in Python and the idea was to use only Python for the platform. Dropbox developed with Python from the beginning and when they noticed they were serving 40 million clients with their Python codebase. Disney uses Python to power their creative processes. And by the way: NASA uses Python too for some of their development.
And all this because Python is an easy to lear and easy to read programming language.
Differences between Python 2 and Python 3
Currently there are two major versions in use by developers: 2 and 3. I will use Python 3.5 because it is the most recent version and almost every Python 3 script can be executed with a Python 2 interpreter. I say almost because there are some differences which required the new major version number to increase.
Because Python 3 was introduced with a new version number it has to have differences between Python 2. So let’s take a brief look at the main differences which you can probably encounter when you develop Python 2 and Python 3:
- integer division
- strings are unicode per default in Python 3
- raising and handling of exceptions changed
- input-parsing changed
xrangehas been removed and
rangeis implemented like the former
Determining the Python version at runtime
A solution would be to determine the Python version at runtime and
exit the application if the user executed the script with the wrong version. To achieve this let me show you a simple code. For now just have a look, and you know: you can come back later to this section to revise based on the knowledge you gathered.
<br /> import sys<br /> if sys.version_info != 3:<br /> print("Wrong Python version used")<br /> exit(-1)<br />
Or alternatively we could
raise and exception — but more about this in a later article when learning about exception handling in more detail.
Python 3.5 is currently only a release candidate. This means that Python developers are not encouraged to use this version in production — but it is scheduled to come out in mid-september so it will be quite actual in the very near future, and a release candidate is very likely containing the same features as the final release will.
Perhaps you have noticed that you already have Python installed on your computer. To verify this you have to enter
<br /> python<br />
in the command prompt and hit return. If it is installed you should get something like the following message:
<br /> Python 2.7.10 (default, Jun 10 2015, 19:42:47)<br /> [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin<br /> Type "help", "copyright", "credits" or "license" for more information.<br /> >>><br />
This message contains the version information on the first line. If you look carefully you can see that it is most of the time Python 2.7 (at least it comes with Macs already installed).
To see if you have Python 3 installed you should enter
<br /> python3<br />
at the command prompt and hit return. Most of the time this version is not installed so let’s take a look how to install it.
You can find the version I installed here.
Because the basic distributed version is available for almost every platform I suggest you follow the installation instructions provided by the site — or as I did you get the executable installer if you have a Mac or Windows OS then you have no complicated installation steps. If you are using Linux you should know by now how to compile (or install) extensions and software on your system.
So this was it. Now it is time to fire up the Python interpreter shell and see that the installation was OK:
<br /> python3<br /> Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 5 2014, 20:42:22)<br /> [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin<br /> Type "help", "copyright", "credits" or "license" for more information.<br /> >>><br />
You should see something similar to this with the right version number of Python displayed.
Using other modules
Sometimes you do not want to re-invent the wheel because you have done it already or you have found a module which can be used for your purposes.
In this case you can
import the module which contains the functionality you need. We saw how it is done with our own scripts which are located in the same folder. But how does it work with scripts of other developers?
Some modules are available through the PyPI (Python Package Index).
Probably you have heard already of the
pip command. It is an abbreviation for “Python Install Packages” or “Package Installer Python” or some more. You can use your on version too. The main thing you should care about is that with pip you can install modules and their dependencies through one command. If you have a module which got a newer version you can update it with
Let’s go right ahead and install the
lxml module which we will need later when I tell you about XML processing with Python.
<br /> $ pip3 install lxml<br /> Collecting lxml<br /> Downloading lxml-3.4.4.tar.gz (3.5MB)<br /> 100% |████████████████████████████████| 3.5MB 153kB/s<br /> Installing collected packages: lxml<br /> Running setup.py install for lxml<br /> Successfully installed lxml-3.4.4<br />
If you wonder why I use
pip3 instead of
pip the reason is very simple: Python 2 has its own
pip and if you install a module for a different Python version it is obviously not available for another.
To see if the
pip command is right for you you can look-up its version and see which Python version it modifies:
<br /> $ pip -V<br /> pip 7.0.3 from /usr/local/lib/python2.7/site-packages/pip-7.0.3-py2.7.egg (python 2.7)<br /> $ pip3 -V<br /> pip 1.5.6 from /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (python 3.4)<br />
The code snippet above tells me that I have to use
pip3 because it is the right version for Python 3.4.
Sometimes you do not have the module you want to use in the Python Package Index (you acquired the code from GitHub for example) or you have a structured project with some folders and Python script files and you need to use one file in another.
In this case you can use this rule of thumb: modules are the names of the Python script files without the
.py extension, folders have to be added with a dot (
.) as a separator before the script’s name.
Naming imported modules
As you can see, with importing over a folder-structure or a module with a long name can become problematic for a long-term usage. I mean consider the following:
import multiprocessing. In this case you would have to type every time
multiprocessing.something to get the desired function or class.
Or another problem can occur when you have two modules with the same name or want to import two methods from different modules which happen to have the same name. Which one will be used in this case? How could you use both?
Fortunately there is a solution: you can give a custom name for imported modules or even imported parts (functions, classes).
<br /> >>> import multiprocessing as mp # from now on you can use 'mp' instead of 'multiprocessing'<br /> >>> from calendar import isleap<br /> >>> from calendar import isleap as leap1 # 'leap1' has the same functionality than isleap<br /> >>> isleap(2015)<br /> False<br /> >>> leap1(2016)<br /> True<br />
Python on Windows
If you are using Windows you have to do some more configuration than using a Unix-like system (Linux, Mac). That’s just because.
The only thing you need is to add Python and
pip to the
PATH to have it available through the command line. And this you can achieve in three fashions.
One is a temporal one where you write a batch script which launches and sets the
PATH every time you open up the command line through this script. I will not go into deeper detail how to achieve this, because this is not the best approach in my opinion and sometimes you want to access Python from where you are.
The two permanent solutions are the same, the only difference is that in one version you have to do the same steps manually as the other does automatically.
The automatic version is to set the checkbox “Add Python 3.5 to PATH” when you install Python. This creates automatically the entries required in the
PATH which enables you the access to Python through the command line.
Alternatively if you choose “Customize installation” there you have the option too to add Python to the
The second version is for those who missed the previous step. In this case you have to edit your
PATH manually. Now here are the steps:
Open up the “Start menu” on your Windows and select “Control Panel”:
In the “Control Panel” select “System and Security”:
In the “System and Security” view select “System”:
In the “System” view select “Advanced system settings” from the left-hand side:
In the “Advanced system settings” window select “Environment Variables…” from the bottom part:
Now you can search for the “Path” entry in the lower part of the window called “System variables”. Here you should select the entry and click on “Edit…”. Now add the installation location of your Python to the end of this entry, and separate it with a semi-colon from the previous entries. For example I would enter the following:
<br /> C:\Python3\Scripts\;C:\Python3\<br />
Scripts folder under the Python installation contains
pip so it is wise to add it to the
Alternatively you can create an environment variable for your user only. In this case select “New…” in the upper part of the window called “User variables for …” and add a new entry where the Variable is set to “PATH” and the value is the location where you installed Python — the same as in the previous step.
Now if you open up a command line and enter
python you should see something like the following command prompt:
And this was it. Now you can access Python every time you open up a command line.