Skip to content

Commit

Permalink
add some code created during last night demo
Browse files Browse the repository at this point in the history
  • Loading branch information
teuben committed Jul 20, 2016
1 parent b62f020 commit 365ef38
Showing 1 changed file with 83 additions and 19 deletions.
102 changes: 83 additions & 19 deletions notebooks/python-basic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand All @@ -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"
Expand Down Expand Up @@ -71,6 +73,7 @@
"outputs": [],
"source": [
"\n",
"a=111\n",
"a=\"1.23\"\n",
"print(a,type(a))"
]
Expand All @@ -94,7 +97,7 @@
},
"outputs": [],
"source": [
"# checking the value of the variable\n",
"# checking the value of the variable again\n",
"a\n"
]
},
Expand Down Expand Up @@ -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!"
]
},
{
Expand Down Expand Up @@ -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"
]
},
Expand All @@ -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."
]
},
{
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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]"
]
},
{
Expand All @@ -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": {},
Expand Down Expand Up @@ -340,7 +375,8 @@
},
"outputs": [],
"source": [
"a=np.arange(0,1,0.2)"
"a=np.arange(0,1,0.2)\n",
"a"
]
},
{
Expand All @@ -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,
Expand All @@ -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"
]
},
Expand Down Expand Up @@ -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"
]
},
Expand Down Expand Up @@ -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!"
]
},
{
Expand All @@ -460,6 +515,15 @@
"source": [
"print(Out)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 365ef38

Please sign in to comment.