An introduction to Python 3

This entry is part 1 of 14 in the series Python Tutorial

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.

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
  • print became a function instead of a statement
  • strings are unicode per default in Python 3
  • raising and handling of exceptions changed
  • input-parsing changed
  • xrange has been removed and range is implemented like the former xrange

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.

import sys
if sys.version_info[0] != 3:
   print("Wrong Python version used")
   exit(-1)

Or alternatively we could raise and exception — but more about this in a later article when learning about exception handling in more detail.

Installing Python

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

python

 

in the command prompt and hit return. If it is installed you should get something like the following message:

Python 2.7.10 (default, Jun 10 2015, 19:42:47)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

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

python3

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:

python3
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 5 2014, 20:42:22)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

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?

Installing with pip

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 pip too.

Let’s go right ahead and install the lxml module which we will need later when I tell you about XML processing with Python.

$ pip3 install lxml
Collecting lxml
Downloading lxml-3.4.4.tar.gz (3.5MB)
   100% |████████████████████████████████| 3.5MB 153kB/s
Installing collected packages: lxml
Running setup.py install for lxml
Successfully installed lxml-3.4.4

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:

$ pip -V
pip 7.0.3 from /usr/local/lib/python2.7/site-packages/pip-7.0.3-py2.7.egg (python 2.7)
$ pip3 -V
pip 1.5.6 from /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (python 3.4)

The code snippet above tells me that I have to use pip3 because it is the right version for Python 3.4.

Locating modules

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).

>>> import multiprocessing as mp # from now on you can use 'mp' instead of 'multiprocessing'
>>> from calendar import isleap
>>> from calendar import isleap as leap1 # 'leap1' has the same functionality than isleap
>>> isleap(2015)
False
>>> leap1(2016)
True

 

Series NavigationPython basics — a.k.a. “Hello World!” and how to achieve it >>
By | 2015-11-14T16:54:21+00:00 November 14th, 2015|Python Tutorial|0 Comments

About the Author:

Leave A Comment