{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# An Introduction to Coding\n",
    "\n",
    "This tutorial will review coding basics for people who are new to coding. \n",
    "\n",
    "\n",
    "We understand that many of you already know many of the basics, but please bear with us while we explain them to those who don't.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Assigning Values\n",
    "\n",
    "You can assign a variable a value or a list of values using the equal = sign. \n",
    "\n",
    "For example,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = 9\n",
    "# also, this is how you make a comment!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can make variable names more specific and use underscores _ to do this. This is different from a - sign, which Python recognizes as subtraction. \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x_new = 16"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Importing Modules\n",
    "Periods are used to define the package from which you are pulling functions or information from. \n",
    "\n",
    "Packages store modules which are files that contain useful functions. \n",
    "\n",
    "One such file that we will use often is NumPy. This stands for Numbers in Python and it contains many functions that are helpful in doing math like the square root function, or converting a list of values to an array type. \n",
    "\n",
    "To be able to access the functions that are in NumPy, you will need to import numpy. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We import numpy as np so when we want to reference numpy again, we can just type \"np\" instead of \"numpy\" when we want to access functions inside numpy. Now, we can access functions within numpy. \n",
    "\n",
    "To find the square root of x, we can use the square root function. The square root function takes one input within parenthesis ( ), but other functions may take more inputs. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sqrt_9 = np.sqrt(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Printing\n",
    "\n",
    "Once we set a variable to a certain value, we want to display it so it is useful and not only stored without being shown to the user. \n",
    "\n",
    "We can do this with a print function. Print functions can include strings (text) within parenthesis and can also include variable values by using either a comma , or a plus +. Sometimes you will have to convert a varaible to a string by using the str() function. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The square root of 9 is 3.0\n",
      "The square root of 9 is 3.0\n"
     ]
    }
   ],
   "source": [
    "print('The square root of 9 is '+str(sqrt_9))\n",
    "\n",
    "print('The square root of 9 is', sqrt_9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Arrays and Lists\n",
    "\n",
    "Python has no native array type. Instead, it has lists, which are defined using [ ]:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a = [0,1,2,3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python has a number of helpful commands to modify lists, and you can read more about them [here](https://docs.python.org/2/tutorial/datastructures.html).\n",
    "In order to use lists as arrays, numpy (numpy provides tools for working with **num**bers in **py**thon) provides an array data type that is defined using ( ). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "a_array = np.array(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get an element of an array use the following syntax (remember that Python indexing starts at 0):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_array[2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loops\n",
    "For and while loops are useful for iterating through lists or until a condition is met"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is an example of a for loop which iterates through each element of a list:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for num in a_array:\n",
    "    print(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And this is an example of a for loop which iterates from 0 to 4:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Element 0 of the array is 0\n",
      "Element 1 of the array is 1\n",
      "Element 2 of the array is 2\n",
      "Element 3 of the array is 3\n"
     ]
    }
   ],
   "source": [
    "for i in range(0,4):\n",
    "    print('Element ' + str(i) + ' of the array is ' + str(a_array[i]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A while loop could be used to create the same output:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Element 0 of the array is 0\n",
      "Element 1 of the array is 1\n",
      "Element 2 of the array is 2\n",
      "Element 3 of the array is 3\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "while i < 4:\n",
    "    print('Element ' + str(i) + ' of the array is ' + str(a_array[i]))\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Functions\n",
    "When it becomes necessary to do the same calculation multiple times, it is useful to create a function to facilitate the calculation in the future.\n",
    "- Function blocks begin with the keyword def followed by the function name and parentheses ( ).\n",
    "- Any input parameters or arguments should be placed within these parentheses.\n",
    "- The code block within every function starts with a colon (:) and is indented.\n",
    "- The statement return [expression] exits a function and returns an expression to the user. A return statement with no arguments is the same as return None.\n",
    "- (Optional) The first statement of a function can the documentation string of the function or docstring, writeen with apostrophes ' '.\n",
    "\n",
    "Below is an example of a function that takes three inputs, pressure, volume, and temperature, and returns the number of moles."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Creating a function is easy in Python\n",
    "def whoami(name):\n",
    "    return print('Hi, my name is ' + name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can try using this function to say your own name, following the example below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi, my name is Fletcher\n"
     ]
    }
   ],
   "source": [
    "whoami('Fletcher')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Try this:\n",
    "\n",
    "Create a function that initializes an empty array, iterates from 1 to 10, adding i to the array each iteration, and  prints the contents of the array each iteration."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now try running the function for n = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## aide_design\n",
    "\n",
    "aide_design is the name of a package that we use in order to access important files concerning fluids functions and pipe diameters that will prove useful in solving design challenges. \n",
    "\n",
    "First, we will need to import aide_design, and from it, import files like physchem and units."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from aide_design import physchem as pc\n",
    "\n",
    "from aide_design.units import unit_registry as u"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Units\n",
    "\n",
    "The units file will allow us to add units to variables we create. You add units by multiplying units, called by u. with the variable or value. Let's use x_new from above. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16 meter\n"
     ]
    }
   ],
   "source": [
    "x_new_units = x_new * u.m\n",
    "\n",
    "print(x_new_units)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I can covert these units through the .to function. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "16000.0 millimeter"
      ],
      "text/latex": [
       "$16000.0\\ \\mathrm{millimeter}$"
      ],
      "text/plain": [
       "<Quantity(16000.0, 'millimeter')>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_new_units.to(u.mm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Temperatures require the use of the u.Quantity function to enter the value and the units of temperature separated by a ',' rather than by a multiplication symbol. This is because it doesn't make sense to multiply by a temperature unit because temperatures (that aren't absolute temperatures) have both a slope and a nonzero intercept."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# example of defining a temperature\n",
    "temp = u.Quantity(15,u.degC)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Physchem\n",
    "\n",
    "Physchem contains many useful functions relating to hydraulics and fluids. One such function that is useful is the flow through an orifice, or a small hole. This function is called flow_orifice(Diam, Height, RatioVCOrifice) and it takes three inputs. The three inputs will be diameter, height of water, and a vena contracta ratio. \n",
    "- You can run a function using no units. Default SI units will be applied (m, m^3, s,...)\n",
    "- You can run a function using any units you want. The function will convert the units for you. \n",
    "- You can run a function using previously defined variables as well as with direct values. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.00021565369636983403 meter ** 3 / second\n",
      "1.3913113874996217e-05 meter ** 3 / second\n",
      "0.00105234805568273 meter ** 3 / second\n"
     ]
    }
   ],
   "source": [
    "flow_with_values = pc.flow_orifice(0.01, 1, 0.62)\n",
    "print(flow_with_values)\n",
    "\n",
    "flow_with_many_units = pc.flow_orifice (0.1 * u.inch, 0.001 * u.km, 0.62)\n",
    "print(flow_with_many_units)\n",
    "\n",
    "Diam = 5 * u.cm\n",
    "Height = 1/8 * u.feet\n",
    "flow_with_variables = pc.flow_orifice(Diam, Height, 0.62)\n",
    "print(flow_with_variables)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Significant Figures\n",
    "\n",
    "In the last cell, many significant figures were printed out. This is ugly to look at so let's only print 3 significant figures. To do this, we will need one more module, utility, which contains a significant figures function. The significant figures function takes two inputs, the variable or value, and the number of significant figures to be displayed. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from aide_design import utility as ut"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The flow with 3 significant figures is 1.39e-5 m³/s\n"
     ]
    }
   ],
   "source": [
    "print('The flow with 3 significant figures is '+ut.sig(flow_with_many_units,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Now Try This:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Create a function which solves the ideal gas law for number of moles\n",
    "\n",
    "In case you forgot the ideal gas law is PV = nRT. Note the universal gas constant is available by calling u.R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now calculate the number of moles of methane in a 40 L container at 25 psi above atmospheric pressure with a temperature of 10 C. (Be sure to use units in your function call)\n",
    "\n",
    "Print the result with 3 significant figures."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
