{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "## Lecture 01: Data Structures\n", "J.R. Gladden, University of Mississippi, Dept. of Physics Physics 530, Spring 2018" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### Strings\n", "* Strings are text characters enclosed by single or double quotes. \n", "* Triple quotes return exactly what is typed in the code (newlines, spaces, tabs,...)\n", "* type(X) returns the data type of *X* \n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n", "\n", "This is\n", "some text in triple quotes.\n", "\n" ] } ], "source": [ "s=\"Hello World\"\n", "print(s)\n", "type(s)\n", "longtext = '''\n", "This is\n", "some text in triple quotes.\n", "'''\n", "print(longtext)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Strings as a sequence\n", "* Strings are treated like a sequence of characters.\n", "* You can extract parts by *slicing* (this will be important for lists and arrays as well)\n", "* s[0:2] returns \"He\" - ** NOTE** counting ALWAYS starts at 0 in python and last number is \"up to but not including\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "He\n" ] }, { "data": { "text/plain": [ "2" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(s[0:2])\n", "s.count('o')" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### Integers\n", "* Integers are whole numbers (no decimals)\n", "* Note there is an upper magnitude limit of +/- 2147483647\n", "* If larger integers are needed, one can employ * long integers \n", "* Usually represented by variables *i,j,k,l*\n", "* ** Careful**: dividing integers will return an integer with the decimal stripped off! Solve by adding a decimal to force it to be a floating point number. This is \"fixed\" in Python 3." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1001, 1002233228882288222882221L)\n", "0\n", "0.5\n" ] } ], "source": [ "i=1001\n", "i_long = 1002233228882288222882221L\n", "print(i,i_long)\n", "print(1/2)\n", "print(1.0/2)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### Basic Math Operations\n", "Python has very basic math operations: + -> add; - -> subtract; / -> divide; * -> multiply; and % modulus (remainder)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "-2.0\n", "1.66666666667\n", "1650.0\n", "2\n" ] } ], "source": [ "print(3+2)\n", "print(3-5.0)\n", "print(5.0/3.0)\n", "print(500.*3.3)\n", "print(5%3)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "More sophisticated opertions can be used by importing the 'math' module (a.k.a. library) with the *import* command." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.22464679915e-16\n" ] }, { "data": { "text/plain": [ "362880.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "print(math.sin(math.pi))\n", "math.gamma(10.0)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### Import\n", "* Importing is done in almost EVERY python program. The base language is very \"light\". So you only load the modules you need\n", "* It can be done in a variety of ways:\n", " * import math \n", " * import math as m\n", " * from math import sin\n", " * from math import sin, cos, tan, exp, log\n", " * from math import * (imports everything in math - sloppy and lazy and in lots of my code!)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.65358979335e-06\n", "2.65358979335e-06\n" ] } ], "source": [ "import math as m\n", "almost_pi=3.14159\n", "print(m.sin(almost_pi))\n", "from math import sin\n", "print(sin(almost_pi))" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### Lists\n", "* Lists are sequences of objects which can be stored and manipulated\n", "* Lists are defined by brackets \"[ ]\"\n", "* Lists can be expanded by adding more elements\n", "* Extract parts of a list using slicing\n", "* Extract a single element using indexing\n", "* Very flexible - can contain a mix of data types ( not common )" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "mylist=[\"Tom\", \"Dick\",\"Susan\"]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "'Tom'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist[0]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "['Tom', 'Dick']" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist[0:2]" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "mylist.append(2)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "['Tom', 'Dick', 'Susan', 2]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "mylist.insert(1,\"Bob\")" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "['Tom', 'Bob', 'Dick', 'Susan', 2]" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist.pop()" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "'Susan'" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist.pop()" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "['Tom', 'Bob', 'Dick']" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### Tuples\n", "Tuples are very similar to lists but are *immutable* - once created they can not be changed. They are often used as a container for a set of parameters. Tuples are defined by parentheses \" ( )\" " ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "params = (3.14159, 2.2,-3.5)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "-3.5" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params[2]" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### Dictionaries\n", "Dictionaries are sometimes called \"named lists\". Elements are identified by a name (key) rather than a number. They are periodically useful in scientific computing, but not that common. The *key* is assoicated with a *value*. Dictionaries are defined by braces \" { } \"" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "rgb={'red':(1,0,0),'green':(0,1,0),'blue':(0,0,1)}" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "['blue', 'green', 'red']" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rgb.keys()" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "[(0, 0, 1), (0, 1, 0), (1, 0, 0)]" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rgb.values()" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "(0, 0, 1)" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rgb['blue']" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "deletable": true, "editable": true }, "source": [ "## Arrays\n", "A great thing about Python is that you can create new datatypes. All those above are \"built in\" to standard Python. Arrays are not - they require the \"numpy\" (numerical python) module which is the backbone of all scientific applications of python. We'll be using arrays and numpy a lot this semester - this is just a brief introduction.\n", "\n", "Arrays are like lists except:\n", "1. they are much more efficient for large sets of numbers\n", "2. they have more methods (like mean, min, max, ...)\n", "3. functions can easily be mapped on to every element in the array\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6]\n" ] } ], "source": [ "import numpy as n\n", "times = n.array([1,2,3,4,5,6]) #note argument is a list\n", "print(times)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1, 6, 3.5)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "times.min(), times.max(), times.mean()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [], "source": [ "times=n.arange(0,100,2) #ceate a range (min,max,increment). Note 'max' in non-inclusive" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32,\n", " 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66,\n", " 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "times" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions\n", "Functions are a way to compartmentalize a chunk of code that accomplishes a specific task. Functions are defined and then called elsewhere in the code. Typcially (not always) functions require input which are called arguments.\n", "\n", "** NOTE: ** Indentation is very important in python! Each level of indentation is a \"block\" of code." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First number is: 1\n", "Second number is: 2\n", "3\n" ] } ], "source": [ "def add2(arg1,arg2):\n", " print \"First number is: \",arg1\n", " print \"Second number is: \",arg2\n", " return arg1 + arg2\n", "\n", "thissum=add2(1,2)\n", "print(thissum)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "canopy_exercise": { "cell_type": "solution" }, "collapsed": true, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.13" } }, "nbformat": 4, "nbformat_minor": 2 }