Commit c900b6b5 authored by Hector Martinez-Seara Monne's avatar Hector Martinez-Seara Monne 🤖
Browse files

Python-0 course material

parent a239af48
Loading
Loading
Loading
Loading
+63 −5
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

# A Python introductory course for Scientists
[Computational core facility](https://intraweb.uochb.cas.cz/computational-core-facility-510.html)

[Institute of Organic Chemistry an Biochemistry of the Czech Academy of Sciences](https://www.uochb.cz/)

version 2020.1

This work is licensed under a [Creative Commons Attribution-ShareAlike 3.0 Unported License](http://creativecommons.org/licenses/by-sa/3.0/deed.en_US).

%% Cell type:markdown id: tags:

# Attributions and credits
## Attributions and credits
This course is not my creation. It is build on these fantastic courses and sources:
- **[A Crash Course in Python for Scientists](https://nbviewer.jupyter.org/gist/rpmuller/5920182)** by [Rick Muller](http://www.cs.sandia.gov/~rmuller/), Sandia National Laboratories.
- The w3schools [Python Tutorial](https://www.w3schools.com/python/)
- The official [Python Tutorial](https://docs.python.org/3/tutorial/)
- The [scientific python lectures](https://github.com/jrjohansson/scientific-python-lectures) by J.R. Johansson

%% Cell type:markdown id: tags:

# Why Python?
## Why Python?
Python is currently the programming language of choice for many scientists. This is why:
- It is straightforward to learn
- It is effortless to read
- Quick prototyping
- It offers plenty of libraries (no need to reinvent the wheel).
- It provides the power to analyze and model scientific data (Good scientific libraries, no need to reinvent the wheel).
- The standard de facto language for machine learning.
- Not restricted to Science, you can do virtually everything with it.
- Easy to find help on the internet.

%% Cell type:markdown id: tags:

## ~~Python 2~~ or Python 3
### ~~Python 2~~ or Python 3
This is no longer a valid question. Python 2 is finally discontined as 1st of January 2020. If you need further information about the issue please check the following [post](https://www.python.org/doc/sunset-python-2/).

There were for a long time two branches of current releases in Python: the older-syntax Python 2, and the newer-syntax Python 3. This schizophrenia is largely intentional: when it became clear that some non-backwards-compatible changes to the language were necessary, the Python dev-team decided to go through a five-year (or so) transition, during which the new language features would be introduced and the old language was still actively maintained, to make such a transition as easy as possible. We're now (2020) only Python 3 should be used. Therefore this is what we will use in the course.

%% Cell type:markdown id: tags:

## IPython and JupyterLab
### IPython and JupyterLab

I find IPython notebooks an easy way both to get important work done in my everyday job, as well as to communicate what I've done, how I've done it, and why it matters to other scientists. I find myself endlessly sweeping the [IPython subreddit](http://ipython.reddit.com) hoping someone will post a new notebook.

[JupyterLab](https://jupyter.org/) is a web-based interactive development environment for Jupyter notebooks, code, and data. JupyterLab is flexible: configure and arrange the user interface to support a wide range of workflows in data science, scientific computing, and machine learning. JupyterLab is extensible and modular: write plugins that add new components and integrate with existing ones.

A quick nice introductory video about JupyterLab can be found here:

https://youtu.be/A5YyoCKxEOU

%% Cell type:markdown id: tags:

## Free resources
### Free resources

This is a quick introduction to Python. There are lots of other places to learn the language more thoroughly. I have collected a list of useful links, including ones to other learning resources, at the end of this notebook. If you want a little more depth, [Python Tutorial](http://docs.python.org/2/tutorial/) is a great place to start, as is Zed Shaw's [Learn Python the Hard Way](http://learnpythonthehardway.org/book/).

The lessons that follow make use of the IPython notebooks. There's a good introduction to notebooks [in the IPython notebook documentation](http://ipython.org/notebook.html) that even has a [nice video](http://www.youtube.com/watch?v=H6dLGQw9yFQ#!) on how to use the notebooks. You should probably also flip through the [IPython tutorial](http://ipython.org/ipython-doc/dev/interactive/tutorial.html) in your copious free time.

-  **Python online tutorials**
  - [An Informal Introduction to Python](https://docs.python.org/3/tutorial/introduction.html)
  - The [Python Tutorial](http://docs.python.org/3/tutorial/) is a great place to start getting a feel for the language.
  - Rob Johansson's [excellent notebooks](http://jrjohansson.github.io/)
- **IPhython and jupyterlab**
  - [A collection of Notebooks for using IPython effectively](https://github.com/ipython/ipython/tree/master/examples/notebooks#a-collection-of-notebooks-for-using-ipython-effectively)
  - [A gallery of interesting IPython Notebooks](https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks)
  - [JupyterLab documentation](https://jupyterlab.readthedocs.io/en/latest/)
-  **Plotting**
  - [Examples matplotlib](https://matplotlib.org/3.1.1/gallery/index.html)
  - [XKCD style graphs in matplotlib](http://nbviewer.ipython.org/url/jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb);
- **Numpy**
  - [Numpy Tutorial](https://numpy.org/doc/stable/user/quickstart.html)
  - [NumPy for MATLAB users](http://mathesaurus.sourceforge.net/matlab-numpy.html)

## Cheatsheets
- [Python](https://perso.limsi.fr/pointal/_media/python:cours:mementopython3-english.pdf)

%% Cell type:markdown id: tags:

# Course prerequisites

%% Cell type:markdown id: tags:

## Programing level
- The course is meant for people with zero level of python
- Knowledge of other programming language helps

%% Cell type:markdown id: tags:

## What You Need to Install

This course assume that you have a Python distribution that includes:

* [Python](http://www.python.org) version >3.8;
* [Numpy](http://www.numpy.org), the core numerical extensions for linear algebra and multidimensional arrays;
* [Scipy](http://www.scipy.org), additional libraries for scientific programming;
* [Matplotlib](http://matplotlib.sf.net), excellent plotting and graphing libraries;
* [IPython](http://ipython.org), with the additional libraries required for the notebook interface.


The easier way to install all those packages is by intalling [Anaconda](https://www.anaconda.com/distribution/) form Continuum. We will asume this in this tutorial.

%% Cell type:markdown id: tags:

## Preparing the environment

- Install an enviroment for the course:
  ```
  conda create --name py38-course python=3.8 jupyterlab numpy nodejs matplotlib scipy pandas
  ```
- Activate the created environment:
  ```
  conda activate py38-course
  ```
- Configure jupyter lab
  - [Table of contents](https://github.com/jupyterlab/jupyterlab-toc)
    ```
    jupyter labextension install @jupyterlab/toc
    ```
  - [Variable inspector](https://github.com/lckr/jupyterlab-variableInspector)
    ```
    jupyter labextension install @lckr/jupyterlab_variableinspector
    ```
  - [pycodestyle_magic and pep8](https://github.com/mattijn/pycodestyle_magic)
    ```
    conda install flake8 pycodestyle
    pip install pycodestyle_magic
    ```
  - [Matplotlib widgets](https://github.com/matplotlib/ipympl)
    ```
    conda install ipympl
    jupyter labextension install @jupyter-widgets/jupyterlab-manager
    # If you already installed the @jupyter-widgets/jupyterlab-manager extension,
    #you will still need to rebuild JupyterLab after you installed ipympl
    #jupyter lab build
    conda install version_information
    ```

%% Cell type:markdown id: tags:

## Checking the configuration of your computer:

%% Cell type:code id: tags:

``` python
import platform
import os
import sys
print(os.name)
print(platform.system())
print(platform.release())
print(f"Python version {sys.version}")
print(f"{sys.version_info}")
```

%% Cell type:code id: tags:

``` python
%reload_ext version_information
%version_information numpy, scipy, matplotlib
```

%% Cell type:markdown id: tags:

## Structure of the course

Briefly, notebooks have code cells (that are generally followed by result cells) and text cells. The text cells are the stuff that you're reading now. The code cells start with "In []:" with some number generally in the brackets. If you put your cursor in the code cell and hit Shift-Enter, the code will run in the Python interpreter and the result will print out in the output cell. You can then change things around and see whether you understand what's going on. If you need to know more, see the [IPython notebook documentation](http://ipython.org/notebook.html) or the [IPython tutorial](http://ipython.org/ipython-doc/dev/interactive/tutorial.html).

%% Cell type:markdown id: tags:

# Python as a calculator
Many of the things I used to use a calculator for, I now use Python for.

%% Cell type:markdown id: tags:

## Simple calcularor

%% Cell type:code id: tags:

``` python
2+2
```

%% Cell type:markdown id: tags:

(If you're typing this into an IPython notebook, or otherwise using notebook file, you hit shift-Enter to evaluate a cell.)

%% Cell type:code id: tags:

``` python
(50-5*6)/4
```

%% Cell type:code id: tags:

``` python
7/3
```

%% Cell type:code id: tags:

``` python
7%3
```

%% Cell type:code id: tags:

``` python
2**8
```

%% Cell type:code id: tags:

``` python
9**(0.5)
```

%% Cell type:code id: tags:

``` python
a=2*3
a*4
```

%% Cell type:markdown id: tags:

## Advanced calculator

Python libraries allow us to extend python funtionality. Let's have our first glace to the **import** statement. Python has a huge number of libraries included with the distribution. To keep things simple, most of these variables and functions are not accessible from a normal Python interactive session. Instead, you have to import the name. For example, there is a **math** module containing many useful functions. To access, say, the square root function, you can either first

    from math import sqrt

and then

    sqrt(81)

%% Cell type:code id: tags:

``` python
from math import sqrt
sqrt(81)
```

%% Cell type:markdown id: tags:

or you can simply import the math library itself

%% Cell type:code id: tags:

``` python
import math
math.sqrt(81)
```

%% Cell type:markdown id: tags:

## Try yourself using python as a calculator

%% Cell type:code id: tags:

``` python
import math
# Try to calculate the log of e using the math library.
```

%% Cell type:code id: tags:

``` python
import math
# Experiment yourself
```

%% Cell type:markdown id: tags:

## What to expect of this Python overview course
I've tried to keep this brief enough so that you can jump in and start using Python to simplify your life and work. There is, of course, much more to the language than I will cover here. When learning programming languages the information doesn't "stick" unless you try and use it for something in real life.

You will no doubt need to learn more as you go. I've listed several other good references, including the [Python Tutorial](http://docs.python.org/2/tutorial/) and [Learn Python the Hard Way](http://learnpythonthehardway.org/book/). Additionally, now is a good time to start familiarizing yourself with the [Python Documentation](http://docs.python.org/2.7/), and, in particular, the [Python Language Reference](http://docs.python.org/2.7/reference/index.html).

Tim Peters, one of the earliest and most prolific Python contributors, wrote the "Zen of Python", which can be accessed via the "import this" command:

%% Cell type:code id: tags:

``` python
import this
```

%% Cell type:code id: tags:

``` python
```
Loading