From 365ef383e810905433f84bb48a45aeffde88f652 Mon Sep 17 00:00:00 2001 From: Peter Teuben Date: Wed, 20 Jul 2016 08:09:16 -0400 Subject: [PATCH] add some code created during last night demo --- notebooks/python-basic.ipynb | 102 ++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 19 deletions(-) diff --git a/notebooks/python-basic.ipynb b/notebooks/python-basic.ipynb index 9213fff..cea0222 100644 --- a/notebooks/python-basic.ipynb +++ b/notebooks/python-basic.ipynb @@ -7,7 +7,7 @@ "# Some Very Basic Python\n", "\n", "\n", - "Showing some very basic python: variables, printing, control structures, lists and arrays, math and plotting:\n", + "Showing some very basic ppython: variables, printing, control structures, lists and arrays, math and plotting:\n", "\n", "### Variables and printing them\n" ] @@ -23,9 +23,11 @@ "# setting a variable in python uses dynamic typing\n", "a = 1.23\n", "\n", + "# in this cell, several python commands can be given. Execute this cell with \"SHIFT-ENTER\"\n", + "\n", "# although just writing the variable will show it's value, but this is not the recommended\n", "# way, because per cell only the last one will be printed and stored in the out[]\n", - "# list that the notebook maintains\n", + "# list that the notebook maintains, use the print() function for this, which we will see below.\n", "\n", "a\n", "a+1" @@ -71,6 +73,7 @@ "outputs": [], "source": [ "\n", + "a=111\n", "a=\"1.23\"\n", "print(a,type(a))" ] @@ -94,7 +97,7 @@ }, "outputs": [], "source": [ - "# checking the value of the variable\n", + "# checking the value of the variable again\n", "a\n" ] }, @@ -130,7 +133,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now we can **print(pi)** in python2. The old style would be **print pi**" + "Now we can **print(pi)** in python2. The old style would be **print pi**, which does not work in python3!" ] }, { @@ -161,7 +164,7 @@ }, "outputs": [], "source": [ - "# for reference, here is the old style of printing in python2 \n", + "# for reference, here is the old style of printing in python2 if you want to uncomment this and try it out\n", "# print \"pi=\",pi" ] }, @@ -171,13 +174,13 @@ "source": [ "## Python Control structures\n", "\n", - "Most programming languages have a way to control the flow of the program. The common ones are\n", + "Most programming languages have a few common ways to control the flow of the program. The common ones in python are\n", "\n", "* if/then/else\n", "* for-loop\n", "* while-loop\n", "\n", - "While you are looking at this code, note that white-space (indentation) controls the extent of the code, and unlike other language, there is no special symbol or **end** type statement. This is probably the single most confusing thing about those new to the python language." + "While you are looking at this code, note that white-space (indentation) controls the extent of the code, and unlike other languages, there is no special **{}** symbol or **end** type statement. This forced indention is probably the single most confusing thing about those new to the python language." ] }, { @@ -234,10 +237,14 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], - "source": [] + "source": [ + "a=[1,2,3]\n", + "a[1]=22\n", + "print(a)\n" + ] }, { "cell_type": "code", @@ -262,10 +269,13 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], - "source": [] + "source": [ + "tuple(range(3))\n", + "\n" + ] }, { "cell_type": "code", @@ -275,9 +285,11 @@ }, "outputs": [], "source": [ - "a1=range(3)\n", - "a2=range(1,4)\n", - "print(a1,a2)" + "a1=list(range(3))\n", + "a2=list(range(3,6))\n", + "print(a1,a2)\n", + "#a1=[0,1,2]\n", + "#a2=[3,4,5]" ] }, { @@ -291,6 +303,29 @@ "a1+a2" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Python lists have some interesting slicing operators that allow you to take sub-lists. See if you can figure it out from the following examples:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a=range(10)\n", + "print(a[2])\n", + "print(list(a[2:4]))\n", + "print(list(a[-2:]))\n", + "print(list(a[2:]))\n", + "print(list(a[:2]))" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -340,7 +375,8 @@ }, "outputs": [], "source": [ - "a=np.arange(0,1,0.2)" + "a=np.arange(0,1,0.2)\n", + "a" ] }, { @@ -353,9 +389,20 @@ "source": [ "b = a*a\n", "c = np.sqrt(a)\n", - "print(a,b,c)" + "print(a)\n", + "print(b)\n", + "print(c)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -364,9 +411,16 @@ }, "outputs": [], "source": [ - "## now for some plotting, using matplotlib, neatly integrated with numpy arrays\n", + "# now for some plotting, using matplotlib, neatly integrated with numpy arrays\n", + "# we are going to plot the arrays we just created\n", + "\n", + "# these next two lines are optional, if you want a different backend\n", + "import matplotlib\n", + "matplotlib.use('agg')\n", "\n", - "# %matplotlib inline\n", + "# any command starting with % is special to ipython notebooks, it tells to produce plots inline,\n", + "# and not externally in a different graph (although that's fun too)\n", + "%matplotlib inline\n", "import matplotlib.pyplot as plt" ] }, @@ -404,6 +458,7 @@ }, "outputs": [], "source": [ + "# plt.show() is only needed to finally plot on screen, in the case an external plot was to be produced\n", "plt.show()\n" ] }, @@ -447,7 +502,7 @@ "source": [ "# The bottom line\n", "\n", - "The python **out[]** list contains all the results from each cell. That may come in handy." + "The python **out[]** list contains all the results from each cell. That may come in handy. See what your notebook has produced so far!" ] }, { @@ -460,6 +515,15 @@ "source": [ "print(Out)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] } ], "metadata": {