<h1 class="title">Programming languages 2 - Getting started with Python</h1>


<ul>
<li><a href="#sec-1">1 Characteristics</a></li>
<li><a href="#sec-2">2 Installation</a></li>
<li><a href="#sec-3">3 Syntax and semantics</a></li>
<li><a href="#sec-4">4 Interpreter</a></li>
<li><a href="#sec-5">5 Standard library</a></li>
<li><a href="#sec-6">6 Documentation and tutorials</a></li>
</ul>

Python is a very interesting language in the sense that it covers a
very wide range of use cases. 
<ol>
<li>It can be useful for simple scripting tasks, that is automating
   repetitive tasks that you usually do by hand.
</li>
<li>It can also be useful for text file processing, like parsing log
   files or specific formats like XML.
</li>
<li>You can use use it as a glue language, that is a mix of system
   calls to command-line programs, like in scripting, but also by
   calling foreing language libraries which provide Python bindings.
</li>
<li>You can use Python as a first language in a Computer Science
   curriculum, since it is simple to learn and supports different
   programming paradigms (Object Oriented, Procedural, Functional).
</li>
<li>You can it also as an extension language, since a Python
   interpreter can be embedded in C/C++ programs.
</li>
<li>Python being a very rich language with a very rich standard
   library, you can use it to build very complex applications. There
   are many ways of using it to build complex Graphical User
   Interfaces, since many graphical libraries provide Python bindings
   (<a href="#Qt">https://www.diotavelli.net/PyQtWiki/PyQt4</a>, <a href="https://www.pygtk.org/">GTK</a>, etc.). Python also
   provides a default library for GUIs, which is called <a href="https://tkinter.unpythonic.net/wiki/">Tkinter</a> and is
   based on Tcl/Tk.
</li>
<li>You can also use Python for web development, either by using the
   standard library utilities or by using one of the very popular web
   frameworks like <a href="https://www.zope.org/">Zope</a>, <a href="https://plone.org/">Plone</a> or <a href="https://www.djangoproject.com/">Django</a>.
</li>
<li>Finally, Python is also extensively used in scientific computing,
   since projects like <a href="https://www.scipy.org/">SciPy</a>, <a href="https://numpy.scipy.org/">Numpy</a> or <a href="https://matplotlib.sourceforge.net/">Matplotlib</a> provide a set of
   tools which allow Python to be as powerful as languages like Matlab
   or IDL with the advantage of being a full fledged language with a
   very rich standard library.
</li>
</ol>


<h2 id="sec-2"><span class="section-number-2">2</span> Installation</h2>
<p>
There are 2 current versions of Python: version 2 and
version 3. Version 3 is not fully compatible with version 2, so if you
are starting with Python, I think is is wise to go with version 3, but
be aware that most existing applications and Open Source projects use
version 2.
</p>
<p>
If you are using a GNU based system, chances are that Python is
already installed in you system. Otherwise, it will be available in
your distribution repositories. As far as I know, Python is also
available on the Mac via the terminal. On widows, you will have to
download a Python distribution from <a href="https://python.org/download">https://python.org/download</a>. On
this page you will also find links for downloading Python for Linux,
MacOS, etc.
</p>
<p>
If you go to this site, you will notice that they mention alternative
implementations of Python. The implementation I will be talking about
here is the one done in C.
</p>
<p>
To install Python, you also can download the source code and compile
it yourself.
</p>

<h2 id="sec-3"><span class="section-number-2">3</span> Syntax and semantics</h2>

<p>
Have a look at <a href="https://en.wikipedia.org/wiki/Python_(programming_language)#Syntax_and_semantics">this link</a>
</p>
<p>
Python is intended to be a highly readable language. It is designed to
have an uncluttered visual layout, frequently using English keywords
where other languages use punctuation. Python requires less
boilerplate than traditional manifestly typed structured languages
such as C or Pascal, and has a smaller number of syntactic exceptions
and special cases than either of these.
</p>
<p>
The simplicity of Python is demonstrated by its version of the classic
"Hello world" program:
</p>
<p>
print("Hello world")
</p>
<p>
Indentation
</p>
<p>
Python uses whitespace indentation, rather than curly braces or
keywords, to delimit blocks; a feature also termed the off-side
rule. An increase in indentation comes after certain statements; a
decrease in indentation signifies the end of the current block.
</p>
<p>
Statements and control flow
</p>
<p>
Python's statements include (among others):
</p>
<p>
    The if statement, which conditionally executes a block of code,
    along with else and elif (a contraction of else-if).
</p>
<p>
    The for statement, which iterates over an iterable object,
    capturing each element to a local variable for use by the attached
    block.
</p>
<p>
    The while statement, which executes a block of code as long as its
    condition is true.
</p>
<p>
    The class statement, which executes a block of code and attaches
    its local namespace to a class, for use in object-oriented
    programming.
</p>
<p>
    The def statement, which defines a function or method.
</p>
<p>
    The import statement, which is used to import modules whose
    functions or variables can be used in the current program.
</p>
<p>
Each statement has its own semantics: for example, the def statement
does not execute its block immediately, unlike most other statements.
</p>
<p>
Expressions
</p>
<p>
Python expressions are similar to languages such as C and Java.
</p>
<p>
    In Python, == compares by value, in contrast to Java, where it
    compares by reference. (Value comparisons in Java use the equals()
    method.) Python's is operator may be used to compare object
    identities (comparison by reference). Comparisons may be chained,
    for example a <= b <= c.
</p>
<p>
    Python uses the words and, or, not for its boolean operators
    rather than the symbolic &amp;&amp;, ||, ! used in Java and C.
</p>
<p>
    Conditional expressions in Python are written as x if c else y
    (different in order of operands from the ?: operator common to
    many other languages).
</p>

<p>
Methods
</p>
<p>
Methods on objects are functions attached to the object's class; the
syntax instance.method(argument) is, for normal methods and functions,
syntactic sugar for Class.method(instance, argument). Python methods
have an explicit self parameter to access instance data, in contrast
to the implicit self in some other object-oriented programming
languages (for example, Java, C++ or Ruby).
</p>
<p>
Typing
</p>
<p>
Python uses duck typing and has typed objects but untyped variable
names. Type constraints are not checked at compile time; rather,
operations on an object may fail, signifying that the given object is
not of a suitable type. Despite being dynamically typed, Python is
strongly typed, forbidding operations that are not well-defined (for
example, adding a number to a string) rather than silently attempting
to make sense of them.
</p>
<p>
Python allows programmers to define their own types using classes,
which are most often used for object-oriented programming. New
instances of classes are constructed by calling the class (for
example, SpamClass() or EggsClass()), and the classes themselves are
instances of the metaclass type (itself an instance of itself),
allowing metaprogramming and reflection.
</p>

<h2 id="sec-4"><span class="section-number-2">4</span> Interpreter</h2>

<p>The section 3 of the python tutorial (<a href="https://docs.python.org/3/tutorial/introduction.html">An informal introduction to Python</a>) gives a very good overview of the use of the interactive interpreter.
</p>
<p>
Of course, if you are going to write long programs, you will want to
save them to files which can then be passed to the interpreter for execution.
</p>

<h2 id="sec-5"><span class="section-number-2">5</span> Standard library</h2>

<p>Python has a very rich standard library, that is a set of modules
which are part of the standard Python installation and which provide
many interesting functions which in many other languages are only
provided by 3rd party libraries :
</p>
<ul>
<li>Operating System Interface
</li>
<li>Command Line Arguments
</li>
<li>Error Output Redirection and Program Termination
</li>
<li>String Pattern Matching
</li>
<li>Mathematics
</li>
<li>Internet Access
</li>
<li>Dates and Times
</li>
<li>Data Compression
</li>
<li>Performance Measurement
</li>
<li>Output Formatting
</li>
<li>Working with Binary Data Record Layouts
</li>
<li>Multi-threading
</li>
<li>Logging
</li>
<li>Decimal Floating Point Arithmetic
</li>
</ul>


<h2 id="sec-6"><span class="section-number-2">6</span> Documentation and tutorials</h2>

<p>The main reference is the Python documentation page at
<a href="https://docs.python.org/">https://docs.python.org/</a> . There you will find a very useful tutorial
(this is the place to start), the standard library reference, and many
other interesting information.
</p>
