{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "ChEn-3170: Computational Methods in Chemical Engineering Spring 2020 UMass Lowell; Prof. V. F. de Almeida **20Jan20**\n", "\n", "# 01. Introduction to Jupyter Notebooks and Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "## Table of Contents\n", "* [Objectives](#obj)\n", "* [Inspiration](#nasa)\n", "* [What is a Jupyter Notebook?](#whatisit)\n", "* [Access to a JNotebook server](#access)\n", "* [Jupyter Notebook cell](#jnbcell)\n", " + [Markdown text](#markdown)\n", " + [Execute Python code](#execute)\n", " + [Inspect Python variables](#variables)\n", "* [Additional help](#umllinux)\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Objectives\n", "\n", " + Outline how Jupyter electronic notebooks will be used to present the content of this course.\n", " + Show the structure of the notebooks used in this course.\n", " + Provide additional resources for help." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Inspiration\n", "*Lines of python code*:\n", " 1. [We are NASA (\"men in the moon\")](https://www.youtube.com/watch?v=WeA7edXsU40&app=desktop) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What is a Jupyter Notebook?\n", "A web application for presenting code, results, and discussion (analysis) all in one place. There are many sources to learn additional information from, such as:\n", " 1. [Visit the website](http://jupyter.org/) \n", " 2. [Read the basic documentation](https://jupyter-notebook.readthedocs.io/en/stable/index.html) \n", " + [Additiona doc.](https://jupyter.brynmawr.edu/services/public/dblank/Jupyter%20Notebook%20Users%20Manual.ipynb)\n", " 3. [Try examples first](https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/examples_index.html)\n", " 4. [Recent Journal Article in Nature](https://www.nature.com/articles/d41586-018-07196-1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Access to a Jupyter Notebook\n", " 1. Install [Anaconda](https://docs.anaconda.com/anaconda/install/#) (free download) and use (Mac OS X and Windows) the Python 3 tarball. After install, use [Anaconda-Navigator](https://docs.anaconda.com/anaconda/navigator/) to start a Jupyter Notebook server.\n", " 2. Use the web-based cloud service [Azure](https://notebooks.azure.com/). Login with your student UMass Lowell student credential and start a Jupyter Notebook. This is a **free** Microsoft cloud service.\n", " 3. Access [this repository](https://github.com/dpploy/chen-3170) and use its Binder deployment of a Jupyter Notebook server." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Jupyter Notebook cell: the basic concept\n", "\n", "### Writing your document (`this cell`)\n", "Have you seen this formula \n", "\n", "\\begin{equation}\n", "k = k_0 \\, e^{-\\frac{E_a}{RT}}\n", "\\end{equation}\n", "\n", "before?\n", "\n", "How about typesetting formula in text: $\\ln k = \\ln k_0 - \\frac{E_a}{RT}$? How was this done?\n", "\n", "All of this text was written in Jupyter Markdown markup language and rendered via executing **this cell**. If you double click on this cell, you will see the Markdown source file. Then execute `Run` on the cell to render it again.\n", "\n", "### Markdown links\n", "[Jupyter Markdown markup language](https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Working%20With%20Markdown%20Cells.html).\n", "\n", "[More on Jupyter Markdown markup language](https://sourceforge.net/p/jupiter/wiki/markdown_syntax/).\n", "\n", "[Additional on Jupyter Markdown markup language](http://assemble.io/docs/Cheatsheet-Markdown.html).\n", "\n", "[Useful extensions and colored boxes](https://www.ibm.com/support/knowledgecenter/en/SSQNUZ_1.1.0/dsx/markd-jupyter.html)\n", "\n", "[Markdown project (see markdown render tool)](https://daringfireball.net/projects/markdown/)\n", "\n", "NB: you can export the notebook to different formats (Menu Bar: file -> Download as ->).\n", "\n", "### Markdown your code (*fenced code block with language highlighting*; double click here)\n", "\n", "Here is a plotting example from the [Matplotlib site](https://matplotlib.org/index.html):\n", " ```python\n", " import matplotlib.pyplot as plt # this is an import of the package\n", " plt.plot([1,2,3,4])\n", " plt.ylabel('some numbers')\n", " plt.show()\n", " ```\n", "this Python code will be executed in the next cell, the last line of source code will display the x-y plot." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execute your Python code" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "plt.plot([1,2,3,4])\n", "plt.ylabel('some numbers')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's use another code plotting script:\n", "```python\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "def f(t):\n", " return np.exp(-t) * np.cos(2*np.pi*t)\n", "\n", "t1 = np.arange(0.0, 5.0, 0.1)\n", "t2 = np.arange(0.0, 5.0, 0.02)\n", "\n", "plt.figure(1)\n", "plt.subplot(211)\n", "plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')\n", "\n", "plt.subplot(212)\n", "plt.plot(t2, np.cos(2*np.pi*t2), 'r--')\n", "plt.show()\n", "```\n", "and execute the next cell to obtain the plot below." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [] }, "outputs": [], "source": [ "'''\n", "This is an example of two stacked plots using\n", "the subplot() method of the pyplot package.\n", "'''\n", "import numpy as np # import the numpy package for arrays computation\n", "import matplotlib.pyplot as plt # import the matplotlib package for plotting\n", "\n", "def f(t):\n", " '''\n", " Define a decaying cosine function\n", " '''\n", " return np.exp(-t) * np.cos(2*np.pi*t)\n", "\n", "t1 = np.arange(0.0, 5.0, 0.1)\n", "t2 = np.arange(0.0, 5.0, 0.02)\n", "\n", "plt.figure(1)\n", "plt.subplot(211)\n", "plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')\n", "\n", "plt.subplot(212)\n", "plt.plot(t2, np.cos(2*np.pi*t2), 'r--')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Inspecting Python variables\n", "#### `t1` is a [NumPy](http://www.numpy.org/) `ndarray` (n-dimensional array)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(type(t1))\n", "print('t1 = ',t1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use the help() command on the `f` function to access its _docstring_" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### What does this `plt.subplot(211)` mean? Use help to access documentation if any:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(plt.subplot)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## _Commercial:_...\n", "## Interested in Python development? Join us at [Cortix](https://cortix.org) for collaboratory coding on GitHub.\n", "\n", "\n", "\n", "## [UML Linux Group](https://github.com/UML-Linux-Club) meetings on Thursdays, Pinanski Hall, room 301, North Campus, 4:30 pm. Join the UML Linux google group; email: uml-linux@googlegroups.com.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.1" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false } }, "nbformat": 4, "nbformat_minor": 2 }