1. Introduction: General
The Python Programming Language
At the moment of writing Python is the third most used programming language in the world. It was originally developed and maintained in the early nineties by Guido van Rossum, at the time affiliated with the Centrum voor Wiskunde en Informatica (CWI) in Amsterdam Science Park. Python is named after Van Rossum's favourite TV show, Monty Python's Flying Circus. Nowadays, Python is maintained by a dedicated group of developers supported by a large number of volunteers, its continued development is directed by the Python Software Foundation.
Python was created with a focus on readability, which forces a clean programming style on the programmer. Unlike other programming languages, Python's structure is determined by indentation. Python also supports duck-typing; it is not necessary to declare variable typings in the code itself, typings will be assigned on compilation. Furthermore, Python is designed to be highly extensible, rather than having all functionality built into the core language.
Python's core philosophy is formalised in The Zen of Python or PEP20, a collection of 19 guiding principles for writing any Python program. Each of the guiding principles is a concise and memorable expression referencing an aspect of programming design. Don't expect to understand all of the principles right away.
When your program eventually abides to these guidelines, it can be referred to as pythonic code.
- 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!
Python's simplicity and readability becomes especially apparent when compared to a more conventional and less abstract language, like the C programming language.
An example of code that prints the numbers #1# through #9#, in the C and Python programming languages. Note that both these programs could be shorter, but they are supposed to showcase the differences in the function declaration.
#\mathtt{\color{#8959A8}{\text{#include }}\text{<stdio.h>}}#
#\mathtt{\color{#8959A8} {\text{int }} \text{increment_by_one(}\color{#8959A8} {\text{int }}\text{n) \{}}#
#\hspace{2em}\mathtt{\color{#8959A8}{\text{return }}\text{n} \color{#3e999f} {\text{ + }}\color{#F5871F}{\text{1}}\text{;}}#
#\mathtt{\text{\}}}#
#\mathtt{\color{#8959A8} {\text{void }}\text{main()} \text{ \{}}#
#\hspace{2em} \mathtt{\color{#8959A8} {\text{int }} \text{n} \color{#3e999f}{\text{ = }} \color{#F5871F}{\text{0}} \text{;}}#
#\hspace{2em}\mathtt{\color{#8959A8}{\text{for }} \text{(} \color{#8959A8}{\text{int }}\text{i}\color{#3e999f}{\text{ = }}\color{#F5871F}{\text{1}}\text{; } \text{i}\color{#3e999f}{\text{ < }}\color{#F5871F}{\text{10}}\text{; }\text{i}\color{#3e999f}{\text{++}}\text{) \{}}#
#\hspace{3.9em}\mathtt{\text{n} \text{ = } \text{increment_by_one(n);}} #
#\hspace{3.9em}\mathtt{\color{#4271ae}{\text{printf}} \text{(}\color{#718c00 }{\text{"}}\color{#F5871F}{\text{%d\\n}}\color{#718c00}{\text{"}}\text{, n);}}#
#\hspace{2em}\mathtt{\text{\}}}#
#\mathtt{\text{\}}}#
#\mathtt{\color{#8959A8}{\text{def }} \text{increment_by_one(n):}}#
#\hspace{2em}\mathtt{\color{#8959A8}{\text{return }}\text{n} \color{#3e999f}{\text{ + }}\color{#F5871F}{\text{1}}}#
#\mathtt{\color{#8959A8}{\text{if }}\text{__name__}\color{#3e999f}{\text{ == }}\color{#718c00}{\text{"__main__"}}\text{:}}#
#\hspace{2em}\mathtt{\text{n}\color{#3e999f}{\text{ = }}\color{#F5871F}{\text{0}}}#
#\hspace{2em}\mathtt{\color{#8959A8}{\text{for }}\text{_ }\color{#8959A8}{\text{in }}\color{#4271ae} {\text{range}}\text{(}\color{#F5871F}{\text{1}}\text{, }\color{#F5871F}{\text{10}}\text{):}}#
#\hspace{3.9em}\mathtt{\text{n} \color{#3e999f}{\text{ = }} \text{increment_by_one(n)}} #
#\hspace{3.9em}\mathtt{\color{#8959A8}{\text{print}} \text{(n)}}#
Even for this simple example, the C program is longer and less readable because of the necessity for semicolons and accolades to annotate structure. The program is written with conventional indentation here, but we could write the entire C program on one line and it would still work, whereas the Python program would not. Additionally, C does not support duck-typing, so we also have to declare typings of variables, functions, and arguments.
Python is an interpreted programming language, it has a high level of abstraction compared to the binary code that is actually executed. As a result, any Python program has to be compiled to code that translates to binary. The reference implementation for Python that accomplishes this is CPython.
Or visit omptest.org if jou are taking an OMPT exam.