Python, for easy programming
Python code must be indented
(Monty Python at work )
A day is enough to start programming with Python and you can write scripts in a few hours while you need for days with other languages. Is is an interpreted language widely used and cross-platforms.
It has powerful features such as lists, tuples, dictionaries, and that allow you to translate your ideas into lines of code easely.
These integrated lists make it a successor to Awk and any other word processor language.
The name is a tribute to the comedy group "Monty Python", but the snake with the same name is also the symbol of the language.
The creator of the language, Guido Van Rossum, was Google employee from 2005 to 2012 and works for Dropbox since. However, in 2014, Dropbox migrated a large part of its software to Go, a current trend in web services.
Evolution
Designed after the ABC language that was a model for simplifying a programming language (the variables for example retain their value from one session to another), Python is initially a very easy to read language.
But programmers never resist their evil demon that incites to write more and more convoluted and crytographic code, bringing the language to have an evolution in the complication that with the time make it approaching C++. The same function in the same language can become less and less comprehensible to the delight of the "expert".
Although if it is here since 2008, Python 3 is still less popular in 2016 than Python 2. The incompatibility with previous versions, without making performance gain, discourages transition. Fear that the existing libraries are not compatible, which is often the case, also hinders its adoption.
This is why an unofficial version of the interpreter, Thauton, appeared which brings to Python 2 new features introduced in the 3 as async/await for example.
Several projects aim to accelerate Python, by compiling it to C with CPython, or to a virtual machine with Jython. The Unladen Swallow project to run Python on LLVM was abandoned due to technical obstacles, including impossible compatibility with C libraries.
In fact Python, as a corporate language is at an impasse: the standard interpeteur is not designed for the modern environment of multi-core processors. It is too slow. Alternative implementations may be used including one that uses a JIT. But in this case we lose compatibility with extensions and libraries, which are actually the primary reason to choose this language.
Dynamic scripting
This scripting language may be used on the server or for applications.
- It is originally interpreted, but compilers exists and there are also versions for the JVM or .NET.
- It is object oriented.
- Variables are dynamic, the type is not declared and may change.
- Indentation is used for block recognition and this is unic to Python.
- Tuples are variables or objects packed all together, for functions' return, for example.
- Lists and dictionaries are other built-in composed objects.
- Functions may be embedded inside other functions.
- Can be extended with C modules.
Python 3.0 changes the syntax of the language which makes it partially incompatible with the previous ones, to the point that it is considered like a new language by early adopters.
- print x is replaced by print (x).
- Range is now an object and not a list of values.
- No more byte strings, all is unicode.
- <> operator is removed .
- dict.keys returns a view and not a list.
That is often blamed in the language, regardless the version:
- Code execution is slow. This is why it tends to be replaced by Go.
- You can not declare a variable like in JavaScript with var. This reduces clarity and security of the code.
- The object orientation is rather rustic and improvised.
- Using indentation to define a block is a design choice often contested. So if you comment a line, that changes the structure!
- Some builtin global functions should be methods of object instead (but PHP is much worse in this regard).
You can compare the syntax of Python, Ruby and PHP, to judge the readability.
Examples of code
Displaying chars of a string:
s = "demo"
for c in s:
print c
Displaying elements of a list:
listdemo = [1,2,3] + [4,5]
subdemo = listdemo[1:3]
for num in subdemo:
print num
Should print: 2 3 4...
While ... else
Adding an else clause to the while control structure is unique to Python. Let's look at it with an example:
while x < 10 :
x = x + 1
else:
print("x = 10")
The else clause is activated when the condition of while ceases to be true, so as soon as x is 10.
We could thing that this clause is a little superfluous because it is always the case when we leave the loop, but there is however a justification for this syntax:
while x < 10:
x = x + 1
if mytaylor == rich: break
else:
print("x = 10")
We see that we can leave the loop independently of the value of x, and the else clause is not always activated. It depends only on the value of x.
The fact remains that to leave a loop independently of the condition is a disruption in the logic of the program.
Tools
We can make Python programs more easily with a free IDE like Eclipse for which numerous examples of use can be found on the Web, or if we are more involved, a commercial software like PyCharm.
Here is a list of the most popular tools to make Python scripts:
- Python.org
The official distribution with a complete tutorial. Thank to the installer to download, installation is very easy. - Tauthon
Unofficial version compatible with Python 2.7 but with features of 3. - PyCharm by JetBrains.
Specialized IDE, exists as open source and free version. - Nuitka
Python compiler for the full language. - IPython
A free modern tool for interactive development, that won the 2012 Award from the FSF. A version is available for Linux, Windows and Mac. - Jython
A Java compatible version. Compile any Python source to bytecode, interpreted by the Java virtual machine. Limited to older versions of the language however.
There is also a compiler for .NET, IronPython. - To use Python in the browser instead of JavaScript there are several implementations: Brython, Sculpt, yjaco, Pyjs, Empythoned. On the server-side, there is Oink. Pythonium also converts Python to JS.