SoSLUG Archive

Python programming

Introduction to python 3.x


Python was first created by Guido von Rossum in 1990 and was named after the British comedy Monty Python’s Flying Circus. team of volunteers ensure its ongoing development. Python is a general purpose programming language that is able to be used on any modern computer operating system. Python is an interpreted language, meaning that it does not have to be converted (compiled) to machine code, before the program is run. However Python programs can also be compiled if necessary.

There are currently two versions of Python, which for legacy reasons sit along side each other. The default, especially for new users is Python 3. Python 3.x by design is not backwards compatible with the the older version Python 2.x. Because of these differences you cannot run python 2.x programs under python 3, nor vica versa. Python is freely available from the Python Software Foundation.

At the time of writing Linux distro’s default to python2 however both python 3 and python 2 packages are included in the download. To force the use of the python version you want, call it explicitly from the command line. E.g. python3 or python2.

Once installed, Python can be started from the command line by typing

    > python3
    Python 3.3.0 (default, Oct 01 2012, 09:13:30) [GCC] on linux
    Type "help", "copyright", "credits" or "license" for more information.

Or to obtain the legacy python 2.

    > python2
    Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2
    Type "help", "copyright", "credits" or "license" for more information.

Note: Since Python2 is reaching the end of its life, all the examples will follow the conventions and syntax of Python3. Legacy (python2) code will only be used to illustrate differences where appropriate.

Benefits of Python

A major benefit claimed of python is that, in common with other high level languages such as C++ and Java, it is object-oriented. This has significant benefits in terms of the readability, maintainability, and scalability of programs. Being a relatively new program, the syntax (grammar of the language) has benefited from more recent advances. It’s ethos can perhaps be best summed up by typing the following code at the python command line prompt.

    > python3
    Python 3.3.0 (default, Oct 01 2012, 09:13:30) [GCC] on linux
    Type "help", "copyright", "credits" or "license" for more information.
     >>> import this

Which the last time I looked yielded the following.

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Author: Alan Campion - Page reference: 3062
Last modified: Alan Campion - 2015-08-30