[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
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?
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
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
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
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)
- 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.
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
frommathimportsqrt
sqrt(81)
```
%% Cell type:markdown id: tags:
or you can simply import the math library itself
%% Cell type:code id: tags:
``` python
importmath
math.sqrt(81)
```
%% Cell type:markdown id: tags:
## Try yourself using python as a calculator
%% Cell type:code id: tags:
``` python
importmath
# Try to calculate the log of e using the math library.