{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Running Python\n", "\n", "1. Normal\n", "\n", " ```bash\n", " $ python foo.py\n", " ```\n", "\n", "1. Interactive\n", "\n", " ```bash\n", " $ python\n", " ```\n", "\n", "1. iPython (recommended)\n", "\n", " ```bash\n", " $ ipython\n", " ```\n", "\n", "1. Jupyter / iPython notebook (also recommended)\n", "\n", " ```\n", " $ jupyter notebook\n", " ```\n", " This is similar to iPython but browser based. It has some very cool features. For development, I have a slight preference for the simpler command-line iPython. \n", " \n", "1. IDEs: PyCharm or Visual Studio Code\n", "\n", " You are on your own to figure this out. It may be very nice once setup, but you also may have some startup costs to configure it to work nicely with anaconda, matplotlib, etc. I will not support issues with IDEs.\n", " \n", "# A typical setup\n", "\n", "Windows open:\n", "\n", "* Favorite text editor, e.g. sublime (write programs)\n", "* Terminal running iPython (type commands)\n", "* Pop up figures with matplotlib\n", "\n", "This mimics MATLAB or R desktop environments.\n", "\n", "Plan\n", "* Example session\n", "* iPython\n", "* %run foo.py\n", "* Some pitfalls of coding interactively\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple iPython notebook session" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Running a cell\n", "A cell is a block of code. Click the cell below and press the \"play\" button above or type Shift+Enter. The code will run, print output, and then advance to the next cell. Type Ctrl+Enter to run but not advance." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "128\n" ] } ], "source": [ "x = 2*x\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Running code in another file\n", "Create a file in the same directory called ``hello.py`` with this line\n", "\n", "```python\n", "print 'hello world'\n", "```\n", "\n", "Now you can run it from jupyter:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello CS335\n" ] } ], "source": [ "%run hello.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a plot using matplotlib" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAD8CAYAAABw1c+bAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3Xl8VPW9//HXh52ENQtrEoKKIKiARtTSWqyiiChtr49Wu1xtbWl7u1zv7WbbqyjW/rqv9NZrq1bvtdZuFkRc0Gpd6hYsKmERZA1BEhLWJCxJPr8/coJDnCSTzCRnlvfz8cgjM+d8Z87ncPQ9J9/PmRlzd0REJHP0CrsAERHpWQp+EZEMo+AXEckwCn4RkQyj4BcRyTAKfhGRDKPgFxHJMAp+EZEMo+AXEckwfcIuIJq8vDwvLi4OuwwRkZSxcuXK3e6eH8vYpAz+4uJiSktLwy5DRCRlmNnWWMdqqkdEJMMo+EVEMoyCX0Qkwyj4RUQyjIJfRCTDdBj8ZlZoZk+a2VozKzOzfw+W55jZCjPbEPwe3sbjrw7GbDCzqxO9AyIi0jmxnPE3AF9291OAc4DPm9lk4HrgCXefADwR3D+OmeUAC4GzgRnAwrZeIEREpGd0GPzuvtPdXwluHwDWAmOB+cDdwbC7gfdHefjFwAp3r3H3PcAKYE4iChcRSSfPbtjNXc9tpqGxqdu31ak5fjMrBqYDLwIj3X0nNL84ACOiPGQssD3ifnmwLNpzLzCzUjMrraqq6kxZIiIp7XBDIzcsWc09z2+lsQe+Bz3m4DezQcCfgevcfX+sD4uyLOpeufvt7l7i7iX5+TG961hEJC385pnNbN5dy02XT6F/n97dvr2Ygt/M+tIc+ve6+1+CxbvMbHSwfjRQGeWh5UBhxP0CoKLr5YqIpJcde+tZ/LeNXDxlJO89uWdOemO5qseAO4C17v7jiFVLgZardK4GlkR5+KPARWY2PGjqXhQsExER4NaH1u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pQAEww8xObTUkJY5LDPvxIFDs7qcDj/P2GXNSMbN5QKW7r2xvWJRlCTsm6Rr85UDkq30BUBFSLXFx9/0tf+K6+3Kgr5nlhVxWVGbWl+agvNfd/xJlSMocl472JZWOSwt33ws8BcxpterYcTGzPsBQknj6sa39cPdqdz8c3P01cGYPlxarmcDlZrYF+D3wPjP7v1ZjuvWYpGvwLwX+NbiK5Bxgn7vvDLuorjCzUS1ze2Y2g+ZjVh1uVe8U1HgHsNbdf9zGsJQ4LrHsSwodl3wzGxbcHghcCKxrNWwpcHVw+wrgbx50FZNFLPvRql90Oc29maTj7t9w9wJ3L6a5cfs3d/9Yq2Hdekz6JOqJepKZ3UfzVRUr6pzVAAAAsklEQVR5ZlYOLKS52YO73wYsp/kKko1AHfCJcCrtWAz7cgXwOTNrAOqBK5Ptf8rATODjwOvBPCzAN4EiSLnjEsu+pMpxGQ3cbWa9aX5x+oO7LzOzRUCpuy+l+UXuf81sI81nlVeGV26bYtmPL5nZ5UADzftxTWjVdkFPHhO9c1dEJMOk61SPiIi0QcEvIpJhFPwiIhlGwS8ikmEU/CIiGUbBLyKSYRT8IiIZRsEvIpJh/j8YnPzEZxLHvAAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "x = [1, 2, 3, 4]\n", "y = [2.3, 3.5, 4.8, 20]\n", "plt.plot(x, y)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Basic Data Types\n", "==============" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numerical and Logical Types\n", "---" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1: X 5\n", "2: X= 5.0\n", "3: X= True\n", "4: X= 500000000000000000000000000000000000000000000000000000000000000\n" ] } ], "source": [ "X=5 #Set X to integer object equal to 5\n", "print(\"1: X\",X)\n", "\n", "X=5.0 #Set X to float object equal to 5.0\n", "print(\"2: X=\",X)\n", "\n", "X=True #Set X to boolean True\n", "print(\"3: X=\",X)\n", "\n", "X=500000000000000000000000000000000000000000000000000000000000000 # Python 3 integers have arbitrary precision\n", "print(\"4: X=\",X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings\n", "--" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is a string\n" ] } ], "source": [ "X=\"This is a string\" #Set X to a string object defined using double quotes\n", "print(X)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "So is this!\n" ] } ], "source": [ "Y='So is this!' #Set X to a string object defined using single quotes\n", "print(Y)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "T\n" ] } ], "source": [ "print(X[0])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This \n" ] } ], "source": [ "print(X[0:5] )" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['This', 'is', 'a', 'string']\n" ] } ], "source": [ "print(X.split())" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is a stringSo is this!\n", "This is a string. So is this!\n" ] } ], "source": [ "print(X + Y)\n", "print(X + '. ' + Y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Containers\n", "========" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuples\n", "--" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "X = (1,2,3,4,5) #Define tuple\n", "print(type(X))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X= (1, 2, 3, 4, 5)\n" ] } ], "source": [ "print(\"X=\", X) #Print the tuple " ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X[0]= 1\n" ] } ], "source": [ "print(\"X[0]=\", X[0]) #Print the first element of the tuple" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "5\n" ] } ], "source": [ "print( X[4] ) #Print the last element of the tuple\n", "print( X[-1] ) #Print the last element of the tuple" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(len(X)) #Pring length of tuple" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mX\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m5\u001b[0m \u001b[0;31m#Set element of tuple ! ERROR (tuples are immutable)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "X[0]=5 #Set element of tuple ! ERROR (tuples are immutable)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists\n", "--" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "X = [1,2,3,4,5] #Define list\n", "print(type(X)) #Show type of X" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5]\n" ] } ], "source": [ "print(X) #Print the list" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(X[0]) #Print the first element of the list" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "5\n" ] } ], "source": [ "print(X[4])\n", "print(X[-1]) #Print the last element of the list" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(len(X))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 2, 3, 4, 5]\n" ] } ], "source": [ "X[0] = 5 #Change the list. \n", "print(X)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 2, 3, 4, 5, 6]\n" ] } ], "source": [ "X.append(6) # Append *one* item to list\n", "print(X)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8]\n" ] } ], "source": [ "X = [1, 2, 3, 4, 5, 6]\n", "X.extend([7, 8]) # add elements 7, 8 to end of X\n", "print(X)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8]\n", "[1, 2, 3, 4, 5, 6, 7, 8, 7, 8]\n" ] } ], "source": [ "Y = X + [7,8] # concatenate and retern a *new* list\n", "print(X)\n", "print(Y)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]\n" ] } ], "source": [ "X = [1, 2, 3, 4, 5]\n", "Y=X*2 # concatenate two copies of X (think: X+X)\n", "print(Y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionaries\n", "--" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "type(X) \n" ] } ], "source": [ "X = {\"Bob\": 192731, \"Mary\": 281927, \"Joe\": 103124} #Define a dictionary of key-value pairs\n", "print(\"type(X)\",type(X))" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X= {'Bob': 192731, 'Mary': 281927, 'Joe': 103124}\n" ] } ], "source": [ "print(\"X=\", X) #Print the dictionary" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X[\"Joe\"]= 103124\n" ] } ], "source": [ "print('X[\"Joe\"]=', X[\"Joe\"]) #Print the value for Joe" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "len(X) 3\n" ] } ], "source": [ "print('len(X)', len(X)) #Print the number of items" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X.keys()= dict_keys(['Bob', 'Mary', 'Joe'])\n" ] } ], "source": [ "print('X.keys()=',X.keys()) #Print the list of keys" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X.values()= dict_values([192731, 281927, 103124])\n" ] } ], "source": [ "print('X.values()=',X.values()) #Print the list of values\n" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X= {'Bob': 192731, 'Mary': 281927, 'Joe': 772345}\n" ] } ], "source": [ "X[\"Joe\"]=772345 #Change an element\n", "print(\"X=\", X)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X= {'Bob': 192731, 'Mary': 281927, 'Joe': 772345, 'Alice': 111111}\n" ] } ], "source": [ "X[\"Alice\"]=111111 #Add a new element\n", "print(\"X=\", X) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Containers with Nesting and Mixed Elements\n", "--" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X= [1, 'Foo', False, [1, 2, 3], (1, True)]\n" ] } ], "source": [ "X = [1,\"Foo\",False,[1,2,3],(1,True)] #Lists can contain mixed types!\n", "print(\"X=\", X) #Print the list" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "#How can we get the second item in the tuple at the end of the list?\n", "print(X[-1][1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assignment Dynamics\n", "==" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A= 5 B= 5\n" ] } ], "source": [ "A = 5 #Assign the object \"5\" to the name A\n", "B = A #Assign the object that A refers to to the name B\n", "print(\"A=\",A,\"B=\",B )" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A= 5 B= 7\n" ] } ], "source": [ "B = 7 \n", "print(\"A=\",A,\"B=\",B)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A= [1, 2, 3, 4, 5] B= [1, 2, 3, 4, 5]\n" ] } ], "source": [ "A = [1,2,3,4,5]\n", "B = A\n", "print(\"A=\",A,\"B=\",B )" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A= [1, 2, 3, 4, 5] B= [5, 4, 3, 2, 1]\n" ] } ], "source": [ "A = [1,2,3,4,5]\n", "B = A\n", "B = [5,4,3,2,1] \n", "print (\"A=\",A,\"B=\",B ) " ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A= [1, 2, 3, 4, 5, 6, 7] B= [1, 2, 3, 4, 5, 6, 7]\n" ] } ], "source": [ "A = [1,2,3,4,5]\n", "B=A\n", "A.append(6)\n", "B.append(7)\n", "print (\"A=\",A,\"B=\",B )" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A= [-1, 2, 3, 4, 5] B= [-1, 2, 3, 4, 5]\n" ] } ], "source": [ "A = [1,2,3,4,5]\n", "B = A\n", "B[0]=-1\n", "print (\"A=\",A,\"B=\",B)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Basic Control Flow\n", "==\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If-Elif-Else\n", "--" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X is positive\n" ] } ], "source": [ "#Basic If-Elif-Else\n", "X=5\n", "if(X<0):\n", " print(\"X is negative\")\n", "elif(X==0):\n", " print(\"X is zero\")\n", "else:\n", " print(\"X is positive\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For and While\n", "--" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 1\n", "1 2\n", "2 3\n", "3 4\n", "4 5\n" ] } ], "source": [ "#Basic for loop using range()\n", "A = [1, 2, 3, 4, 5]\n", "for i in range(len(A)): \n", " print(i, A[i])" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "4\n", "3\n", "2\n", "1\n" ] } ], "source": [ "#Basic while loop\n", "x=5 \n", "while(x > 0):\n", " print(x),\n", " x-=1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mathematical and Logical Operators\n", "==" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2**4= 16\n" ] } ], "source": [ "print (\"2**4=\", 2**4 ) # Computing Powers" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2/4= 0.5\n" ] } ], "source": [ "print (\"2/4=\", 2/4 ) # \"True division\" of int by int\n", "\n", "# Note: the / operator for ints was \"floor division\" \n", "# in Python 2.x, so would give a result of 0 for this example" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2//4= 0\n" ] } ], "source": [ "print (\"2//4=\", 2//4 ) # \"Floor division\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (\"2/4.=\", 2/4.) # Division of int by float (True division)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (\"2./4=\", 2./4) # Division of float by int" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (\"True or False=\", True or False) #Logical or" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (\"True and False=\", True and False) #Logical and" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (\"not(False)=\", not(False)) #Logical not" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions\n", "==" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f(2)= 4\n" ] } ], "source": [ "def f(a): #Define a basic function of 1-argument\n", " return a**2\n", "\n", "print (\"f(2)=\",f(2))\n" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "g(f,2,3)= 13\n" ] } ], "source": [ "def g(h,a,b): #Define a function of 3-arguments where h is a function and a and b are numbers\n", " return h(a)+h(b)\n", "\n", "print (\"g(f,2,3)=\",g(f,2,3))\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "NumPy Arrays\n", "--" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X=\n", " [[1 2]\n", " [3 4]\n", " [5 6]]\n" ] } ], "source": [ "import numpy as np\n", "X = np.array([[1,2],[3,4],[5,6]])\n", "print (\"X=\\n\",X)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X.shape= (3, 2)\n" ] } ], "source": [ "print (\"X.shape=\", X.shape)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X[0,2]= 2\n" ] } ], "source": [ "print (\"X[0,2]=\", X[0,1])" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X[0,:]= [1 2]\n" ] } ], "source": [ "print (\"X[0,:]=\", X[0,:])" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2,)\n" ] } ], "source": [ "Y = X[0,:]\n", "print(Y.shape)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X[:,1]= [2 4 6]\n" ] } ], "source": [ "print (\"X[:,1]=\", X[:,1])" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2*X=\n", " [[ 2 4]\n", " [ 6 8]\n", " [10 12]]\n" ] } ], "source": [ "print (\"2*X=\\n\", 2*X)" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3: 5+X=\n", " [[ 6 7]\n", " [ 8 9]\n", " [10 11]]\n" ] } ], "source": [ "print (\"3: 5+X=\\n\", 5+X)\n" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Y=\n", " [[-1 1 -1]]\n" ] } ], "source": [ "Y = np.array([[-1,1,-1]])\n", "print (\"Y=\\n\",Y)" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6: Y.T=\n", " [[-1]\n", " [ 1]\n", " [-1]]\n" ] } ], "source": [ "print (\"6: Y.T=\\n\", Y.T)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (\"Y.dot(X)=\\n\", Y.dot(X))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (\"np.dot(Y, X)=\\n\", np.dot(Y, X))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (\"Y.T + X =\\n\", Y.T + X)" ] } ], "metadata": { "anaconda-cloud": {}, "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 }