{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###### Content provided under a Creative Commons Attribution license, CC-BY 4.0; code under BSD 3-Clause license. (c)2014 Lorena A. Barba. Thanks: Gilbert Forsyth and Olivier Mesnard, and NSF for support via CAREER award #1149784." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Crash Course\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hello! This is a quick intro to numerical programming in Python to help you hit the ground running with the _AeroPython_ set of notebooks. (This intro is a modified version of the first notebook of the [_CFD Python_](http://lorenabarba.com/blog/cfd-python-12-steps-to-navier-stokes/) series by Prof. Lorena A. Barba, July 2013.)\n", "\n", "Python may already be installed on your computer (especially if you use OSX or a flavor of Linux). Even so, we recommend that you download and install the free [Anaconda Scientific Python](https://www.continuum.io/downloads) distribution. It makes it much easier to hit the ground running.\n", "\n", "You will probably want to download a copy of this notebook, or the whole _AeroPython_ collection. We recommend that you then follow along each lesson, experimenting with the code in the notebooks, or typing the code into a separate Python interactive session.\n", "\n", "If you decided to work on your local Python installation, you will have to navigate in the terminal to the folder that contains the .ipynb files. Then, to launch the notebook server, just type:\n", "\n", "`jupyter notebook`\n", "\n", "You will get a new browser window or tab with a list of the notebooks available in that folder. Click on one and start working! " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Libraries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python is a high-level open-source language. But the *Python world* is inhabited by many packages or libraries that provide useful things like array operations, plotting functions, and much more. We can import libraries of functions to expand the capabilities of Python in our programs. \n", "\n", "OK! We'll start by importing a few libraries to help us out. First: our favorite library is **NumPy**, providing a bunch of useful array operations (similar to MATLAB). We will use it a lot! The second library we need is **Matplotlib**, a 2D plotting library which we will use to plot our results. \n", "\n", "The following code will be at the top of most of your programs, so execute this cell first:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# <-- comments in python are denoted by the pound sign, like this one\n", "\n", "import numpy # we import the array library\n", "from matplotlib import pyplot # import plotting library" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are importing one library named `numpy` and we are importing a module called `pyplot` of a big library called `matplotlib`. \n", "\n", "To use a function belonging to one of these libraries, we have to tell Python where to look for it. For that, each function name is written following the library name, with a dot in between. \n", "\n", "So if we want to use the NumPy function [`linspace()`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html), which creates an array with equally spaced numbers between a start and end, we call it by writing:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.55555556 1.11111111 1.66666667 2.22222222 2.77777778\n", " 3.33333333 3.88888889 4.44444444 5. ]\n" ] } ], "source": [ "myarray = numpy.linspace(0, 5, 10)\n", "print(myarray)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we *don't* preface the `linspace()` function with `numpy`, **Python will throw an error**, because it doesn't know where to find this function. Try it:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'linspace' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmyarray\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlinspace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mNameError\u001b[0m: name 'linspace' is not defined" ] } ], "source": [ "myarray = linspace(0, 5, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "The function [`linspace`()](http://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html) is very useful. Try it changing the input parameters!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Import style:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will often see code snippets that use the following lines\n", "\n", "```Python\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "```\n", "\n", "What's all of this `import-as` business? It's a way of creating a 'shortcut' to the NumPy library and the pyplot module. You will see it frequently as it is in common usage, but we prefer to keep out imports *explicit*. We think it helps with code readability. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Pro tip:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes, you'll see people importing a whole library without assigning a shortcut for it (like `from numpy import *`). This saves typing but is sloppy and can get you in trouble. Best to get into good habits from the beginning!\n", "\n", "To learn new functions available to you, visit the [NumPy Reference](http://docs.scipy.org/doc/numpy/reference/) page. If you are a proficient `Matlab` user, there is a wiki page that should prove helpful to you: [NumPy for Matlab Users](http://wiki.scipy.org/NumPy_for_Matlab_Users)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python doesn't require explicitly declared variable types, like C and other languages do. Just assign a variable and Python understands what you want:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = 5 # a is an integer 5\n", "b = 'five' # b is a string of the word 'five'\n", "c = 5.0 # c is a floating point 5 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ask Python to tell you what type it has assigned to a given variable name like this:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(b)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Python 3, division between integers, floats, or a combination thereof produces a float with the correct value, to machine precision. For example," ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.8" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "14/a" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.8" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "14/c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "both produce the same floating point number." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Whitespace in Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python uses indents and whitespace to group statements together. For contrast, if you were to write a short loop in the C language, you might use:\n", "\n", " for (i = 0, i < 5, i++){\n", " printf(\"Hi! \\n\");\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python does not use curly braces like C, it uses indentation instead; so the same program as above is written in Python as follows:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hi \n", "\n", "Hi \n", "\n", "Hi \n", "\n", "Hi \n", "\n", "Hi \n", "\n" ] } ], "source": [ "for i in range(5):\n", " print(\"Hi \\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Did you notice the [`range()`](http://docs.python.org/release/1.5.1p1/tut/range.html) function? It is a neat built-in function of Python that gives you a list from an arithmetic progression.\n", "\n", "If you have nested `for` loops, there is a further indent for the inner loop, like this:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0\n", "0 1\n", "0 2\n", "This statement is within the i-loop, but not the j-loop\n", "1 0\n", "1 1\n", "1 2\n", "This statement is within the i-loop, but not the j-loop\n", "2 0\n", "2 1\n", "2 2\n", "This statement is within the i-loop, but not the j-loop\n" ] } ], "source": [ "for i in range(3):\n", " for j in range(3):\n", " print(i, j)\n", " \n", " print(\"This statement is within the i-loop, but not the j-loop\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Slicing arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In NumPy, you can look at portions of arrays in the same way as in MATLAB, with a few extra tricks thrown in. Let's take an array of values from 1 to 5:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myvals = numpy.array([1, 2, 3, 4, 5])\n", "myvals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python uses a **zero-based index** (like C), which is [a good thing](http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html). Knowing this, let's look at the first and last element in the array we have created above," ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 5)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myvals[0], myvals[4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are 5 elements in the array `myvals`, but if we try to look at `myvals[5]`, Python will be unhappy and **throw an error**, as `myvals[5]` is actually calling the non-existent 6th element of that array." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "index 5 is out of bounds for axis 0 with size 5", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmyvals\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mIndexError\u001b[0m: index 5 is out of bounds for axis 0 with size 5" ] } ], "source": [ "myvals[5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arrays can also be *sliced*, grabbing a range of values. Let's look at the first three elements," ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myvals[0:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note here, the slice is inclusive on the front end and exclusive on the back, so the above command gives us the values of `myvals[0]`, `myvals[1]` and `myvals[2]`, but not `myvals[3]`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Assigning array variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the strange little quirks/features in Python that often confuses people comes up when assigning and comparing arrays of values. Here is a quick example. Let's start by defining a 1-D array called $a$:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = numpy.linspace(1,5,5)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3., 4., 5.])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OK, so we have an array $a$, with the values 1 through 5. I want to make a copy of that array, called $b$, so I'll try the following:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3., 4., 5.])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great. So $a$ has the values 1 through 5 and now so does $b$. Now that I have a backup of $a$, I can change its values without worrying about losing data (or so I may think!)." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a[2] = 17" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 17., 4., 5.])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, the 3rd element of $a$ has been changed to 17. Now let's check on $b$." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 17., 4., 5.])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And that's how things go wrong! When you use a statement like `a = b`, rather than copying all the values of `a` into a new array called `b`, Python just creates an alias called `b` and tells it to route us to `a`. So if we change a value in `a`, then `b` will reflect that change (technically, this is called *assignment by reference*). If you want to make a true copy of the array, you have to tell Python to create a copy of `a`." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "c = a.copy()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we can try again to change a value in $a$ and see if the changes are also seen in $c$. " ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a[2] = 3" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3., 4., 5.])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 17., 4., 5.])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OK, it worked! If the difference between `a = b` and `a = b.copy()` is unclear, you should read through this again. This issue will come back to haunt you otherwise.\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Learn more" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are a lot of resources online to learn more about using NumPy and other libraries. Just for kicks, here we use IPython's feature for embedding videos to point you to a short video on YouTube on using NumPy arrays." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import YouTubeVideo\n", "YouTubeVideo('vWkb7VahaXQ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Please ignore the cell below. It just loads our style for the notebook." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.core.display import HTML\n", "def css_styling(filepath):\n", " styles = open(filepath, 'r').read()\n", " return HTML(styles)\n", "css_styling('../styles/custom.css')" ] } ], "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" } }, "nbformat": 4, "nbformat_minor": 1 }