{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook will introduce the basic concept of Jupyter notebooks.\n", "\n", "First, a look at a simple code block:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a= 2\n", "b= 3\n", "c= 5\n", "now c= 6\n" ] } ], "source": [ "a=2\n", "print 'a= ',a\n", "b=3\n", "print 'b= ',b\n", "c=a+b\n", "print 'c= ',c\n", "c+=1\n", "print 'now c= ',c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will also use various Python \"modules\". Our first is the NumPy module. \n", "\n", "First of all - how do you pronounce \"NumPy\". According to Important People at Enthought (e.g, Robert Kern), it should be pronounced \"Num\" as in \"Number\" and \"Pie\" as in, well, pie, or Python. Oops. It is way more fun to say Numpee!\n", "\n", "So, that out of the way, what can NumPy do for us? Turns out, a whole heck of a lot! But for now we will just scratch the surface. It can, for example, give us the value of $\\pi$ as numpy.pi. Note how the module name comes first, then the name of the function we wish to use. In this case, the function just returns the value of $\\pi$.\n", "\n", "To use NumPy functions, we must first \"import\" the module with the command \"import\". There are several ways to do this, but I recommend the following. Note: the first time you do this after installing Python, it may take a while, but after that it should be very quick.\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can use the numpy functions in our programs. Here are a few examples:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.pi" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sqrt(4.0)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a,b,c=2,-12,16 # note all on one line and that # means comment follows.\n", "(-b+np.sqrt(b**2-4.*a*c))/(2.*a) # quadratic equation" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.49999999999999994" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y=np.sin(np.pi/6.)\n", "y" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }