“Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Readability count…”
Python was the language we spoke at our previous Technical Workshop @Q backed by Cristin Iosif. Our colleague dedicated this subject three presentation sessions with the purpose to enlarge the programming portfolio used by the Q team. These sessions will cover three areas, as follows:
- Python overview, core concepts and characteristics, basic data types, syntax and tools
- Data types, classes, frameworks and applications
- Creating a small web application
Guido van Rossum, Python’s creator, named the programming language inspired by the “Monty Python’s Flying Circus” TV Show. The language was created in late 1989.
What is Python?
Python is a free, open source widely known scripting language. It is a useful and easy learning programming language application appreciated due to its full featured general-purpose aspect. Also is a high-level programming language, strongly and dynamic typed. Python is spread in various domains, database applications, scientific applications, GUI applications and used for rapid prototyping, ad-hoc programming and web development, as extension and a glue language. Being easy to use and powerful, even big companies like Google, Yahoo, Reddit, DropBox, CERN, Quora, Survery Monkey and many others institutions are embracing it.
The language takes different implementations forms as Jython – for Java and IronPython – for .NET, but the most known implementation is Cpython, written in C. Also, it exists PyPy implementation which is written in a specialised subset of Python called Rpython. All four implementations have a JIT compiler which interprets the code. CPython interprets the code and compiles it into byte code at runtime while Jython, IronPython and PyPy compiles the code to machine code at runtime. Python has two major versions that are widely used: version 2 and version 3. Version 2 is the most used one due to its maturity and stability compared to version 3. Unfortunately, Python’s last version is not fully backwards compatible with version 2. However, the latest releases of version 2 smoothed the transition to version 3 by making it more compatible in terms of updates and libraries.
As the majority of dynamic languages, Python have a central package/library repository which is called PyPI – Python Package Index – pypi.python.org. In order to download and install packages with all dependencies , there are some tools available: easy_install and pip, that search PyPI repository. Another great tool for Python is virtualenv – virtualenv.org. This tool is for creating and using isolated Python environments with different version, and does not require special user permissions to install and use different packages in these environments. Python 3 is fitted with „pyvenv” that does the same thing as „virtualenv”. Also, Python comes with a REPL (read, evaluate, print, loop) that works as an interactive console where you can input(type) Python code which is evaluated and, if necessary, the output is printed.
In Python whitespace is syntactically significant. This means that Python statement’s body block constructs are delimited by white space as indentation levels. The community convention is to use 4 spaces for indentation. This aspect of the language forces the developers to use the same indentation level which is a good practice in any language because it makes the code more readable and avoids cluttering by not using curly braces. In this way the code interpretation is more consistent between the author, the interpreter (computer) and the future developers who can maintain There are a few rules that applies here:
- it’s preferably to use four spaces for one indentation level
- never mix space and tabs or the interpreter will get confused
- be consistent on consecutive lines and you can deviate from the rules only to improve readability when there are long lines of code.
Python standard library
Having the philosophy “Batteries Included”, Python’s standard library have a lot of useful packages: string, difflib, struct, codecs, copy, pprint, numbers, random, math, time, datetime, itertools, pickle, marshal, configparse, hashlib, logging, threading, concurrent, multiprocessing, email etc. All of these are well documented on docs.python.org
Another essential thing about Python is modularity which offers the possibility to create encapsulated chunks of reusable code. Collections of functions and classes that are grouped in different files, contains Python code and have the “.py” extension are called modules.
Basic things to know in Python
- Scalar(int, float, None, bool) and collection types(str, list, tuple, dict, set, range)
- Assignment(=), arithmetic(+ – * / % //) and relational operators (== != < > <= =>)
- Conditional statements (if … elif … else …)
- Loops (while loops and for loops)
- Exception handling (try: … except: … finally: …)
Python community embraces a set of values, practices and philosophies and most of them are documented in the Python Enhancement Proposals(PEPs). There are a series of articles that documents the language development in python.org/dev/peps. The most important PEP is PEP8. You can find a set of widely adopted conventions and Python principles at this link. PEP20 is a set of 20 aphorism that it best represents Python’s principles. Find more info.