{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"
\n",
"\n",
"\n",
"# Introduction to Python \n",
"\n",
"This material was prepared at the [Luthey-Schulten Group](http://www.scs.illinois.edu/schulten/), University of Illinois at Urbana-Champaign, for the [_\"Hands-On\" Workshop on Cell Scale simulations_](http://www.ks.uiuc.edu/Training/Workshop/Urbana2018e/).\n",
"\n",
"This introduction will cover main aspects of Python, some of it's main libraries, and Jupyter Notebooks. We will focus on concepts and techniques that will be used throughout the _\"Hands-On\" Workshop on Cell Scale simulations_. We will begin from the basics of the language, assuming you have never seen it before, but also assuming you have had *some* programing experience.\n",
"\n",
"Overview: \n",
"\n",
"- [Python language](#intro_python)\n",
" - Interpreted Modern Day Stuff\n",
" - Installation (system package/Anaconda)\n",
" - Python 3 vs 2\n",
"- [Jupyter Notebook (What is this thing I am looking at?)](#intro_notebook)\n",
" - [Shortcuts!](#intro_shortcuts)\n",
"- [Variables and Collections](#intro_vac)\n",
" - [Native Types and Dynamic Typing](#intro_ntdt)\n",
" - [Everything is a class](#intro_eiac)\n",
" - [Numbers](#intro_numbers)\n",
" - [Strings/bytes](#intro_strings)\n",
" - Basic methods\n",
" - [Lists](#intro_lists)\n",
" - Indexing, slicing, negative indices\n",
" - Basic methods\n",
" - [Tuples](#intro_tuples)\n",
" - [Sets](#intro_sets)\n",
" - Basic methods\n",
" - [Dictionaries](#intro_dictionaries)\n",
" - Basic methods\n",
" - [is vs. equals (or, value vs. reference)](#intro_ive)\n",
" - [None](#intro_none)\n",
"- [Control Flow](#intro_controlflow)\n",
" - [Indentation and Scope](#intro_ias)\n",
" - [If/Else](#intro_ifelse)\n",
" - [For/While Loops](#intro_fwl)\n",
" - Continue/Break/Else\n",
" - [Try/Except](#intro_tryexcept)\n",
"- [Functions](#intro_functions)\n",
" - [Def/Lambda](#intro_deflambda)\n",
" - [Arguments (and default arguments )](#intro_arguments)\n",
" - \\*args and \\*\\*kwargs\n",
" - [Comments and Doc-Strings](#intro_cads)\n",
" - [Scope](#intro_scope)\n",
"- [Classes](#intro_classes)\n",
" - [Encapsulation](#intro_encapsulation)\n",
" - [Inheritance](#intro_inheritance)\n",
" - [Polymorphism](#intro_polymorphism)\n",
"- [Iterators and Generators](#intro_iag)\n",
" - [Comprehensions/range/map](#intro_crm)\n",
"- [Modules](#intro_modules)\n",
" - [Import syntax](#intro_importsyntax)\n",
" - [Install new modules](#intro_inm)\n",
"- [File IO](#intro_fio)\n",
" - [open() and *with*](#intro_oaw)\n",
" - [csv/Pickle/Json](#intro_cpj)\n",
"- [Virtual Environments](#intro_ve)\n",
" - [python -m venv](#intro_pve)\n",
" - [conda new](#intro_condanew)\n",
"- [Scientific Modules](#intro_scientificmodules)\n",
" - [Numpy/Scipy and Matplotlib](#intro_nsm)\n",
" - [Pandas](#intro_pandas)\n",
" - Wide vs Long data formats: melting and casting\n",
" - [Plotnine](#intro_plotnine)\n",
" - [Cython and Numba](#intro_can)\n",
" - [Mpi4Py](#intro_mpi4py)\n",
"- [Jupyter](#intro_jupyter)\n",
" - [ipywidgets](#intro_ipywidgets)\n",
"- [Integrations](#intro_integrations)\n",
" - [Magics](#intro_magics)\n",
" - [pybind11](#intro_pybind11)\n",
"- [Sources and Aknowledgements](#intro_saa)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"\n",
"########\n",
"\n",
"# The Python Language \n",
"\n",
"[Python][1] was created almost 30 years go as an interpreted, high-level, object-oriented programing language. It allows for different interactive environments (such as the notebook you are looking at), and lets one develop, test and distribute code much faster than traditional compiled languages like C/C++ and Fortran.\n",
"\n",
"[1]: https://www.python.org/\n",
"\n",
"## Installation\n",
"\n",
"- In **windows**, use [Anaconda][2].\n",
"\n",
"- In **MacOS** (which is almost Windows), also use [Anaconda][3].\n",
"\n",
"- In **Linux**, Python should come pre-installed (if not, use your package manager to add it), but you will need to install many interesting packages. You can do that using the `pip` tool, as in\n",
"\n",
" `pip install scipy`\n",
"\n",
"We will talk about creating individual environments (which would look like different python installations), so that one can organize packages, keep different versions of the same package, or keep conflicting packages in the same computer.\n",
"\n",
"[2]: https://conda.io/docs/user-guide/install/windows.html\n",
"[3]: https://conda.io/docs/user-guide/install/macos.html\n",
"\n",
"## Python 2 vs 3\n",
"\n",
"Python 3 is better! Also, [\"The End Of Life date (EOL, sunset date) for Python 2.7 has been moved five years into the future, to 2020.\"](https://www.python.org/dev/peps/pep-0373/)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Jupyter Notebook (What is this thing I am looking at?) \n",
"\n",
"A notebook is a special type of interactive interface that allows us to combine text, image and video, with code blocks that are executed on demand, and can create interactive interfaces, as we will see later in this introduction and along the tutorials in this workshop.\n",
"\n",
"The image below describes some of the toolbar functionalities, but you will find some useful shortcuts below.\n",
"\n",
"
\n",
"\n",
"