{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# E - 'if' conditionals\n", "\n", "![lec-0E-ga](lec-0E-ga.png)\n", "\n", "* To try it on *python tutor live*, [click here][preloaded ptl E]\n", "\n", "[preloaded ptl E]: http://www.pythontutor.com/live.html#code=%23%20%3D%3D%3D%20Lecture%20zero,%20part%20E%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%23%20---%20Remember%20from%20part%20D%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Amain_R%20%3D%208.314%0A%23%20---%20just%20another%20function%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Adef%20function_P_sat(dummy_T%29%3A%20%23%20Let's%20%20use%20this%20approximate%20correlation%20for%20water%20saturation%20pressure%0A%09return%201e5%20*%20(%2010**(%205.40221%20-%20(%201838.675%20/%20(%20dummy_T%20-%2031.737%20%29%20%29%20%29%20%29%0Amain_Rho_sat%20%3D%201000%20%23%20kg%20/%20m%5E3%20%0Amain_mol_weight%20%3D%2018%20%23%20g%20/%20mol%0A%23%20---%20fix%20function_V%20with%20an%20%22if%22%20syntax%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Adef%20function_V(dummy_T,dummy_P%29%3A%20%23%20now%20if%20a%20volume%20at%20pressure%20lower%20than%20saturation%20pressure%20is%20inquired,%20a%20ideal%20gas%20phase%20volume%20is%20provided%0A%09if%20(dummy_P%29%20%3C%20function_P_sat(dummy_T%29%3A%0A%09%09return%20main_R%20*%20dummy_T%20/%20dummy_P%0A%09else%3A%20%20%23%20and%20if%20a%20volume%20at%20pressure%20higher%20than%20saturation%20pressure%20is%20inquired,%20a%20liquid%20phase%20volume%20is%20provided%0A%09%09return%20main_mol_weight%20/%20(%20main_Rho_sat%20*%201000%20%29%0Amain_P%3D1e5%0Alist_T%20%3D%20%5B273,%20298,%20373,%20500,%20600%5D%0A%23%20---%20for%20loop%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Afor%20i%20in%20range(3%29%3A%20%23%20the%20function%20range%203%20says%20i%20should%20loop%203%20times%20with%20index%20starting%20from%20zero%20and%20increasing%20in%201%20after%20each%20iteration%0A%09print(function_V(dummy_T%3Dlist_T%5Bi%5D,dummy_P%3Dmain_P%29%29&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-live.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.8e-05\n", "1.8e-05\n", "0.03101122\n" ] } ], "source": [ "# The following code should be studied on the platform provided by http://www.pythontutor.com/live.html#mode=edit\n", "# This platform graphically depicts the behavior of variables, assignments, functions and pointers,\n", "# which is the major source of confusion among beginners in programming\n", "\n", "# === Lecture zero, part E ========================================================================================================================================================================\n", "\n", "# --- Remember from part D -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "main_R = 8.314\n", "\n", "# --- just another function -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "def function_P_sat(dummy_T): # Let's use this approximate correlation for water saturation pressure\n", "\treturn 1e5 * ( 10**( 5.40221 - ( 1838.675 / ( dummy_T - 31.737 ) ) ) )\n", "\t\n", "main_Rho_sat = 1000 # kg / m^3 \n", "main_mol_weight = 18 # g / mol\n", "\n", "# --- fix function_V with an \"if\" syntax -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "def function_V(dummy_T,dummy_P): # now if a volume at pressure lower than saturation pressure is inquired, a ideal gas phase volume is provided\n", "\tif (dummy_P) < function_P_sat(dummy_T):\n", "\t\treturn main_R * dummy_T / dummy_P\n", "\telse: # and if a volume at pressure higher than saturation pressure is inquired, a liquid phase volume is provided\n", "\t\treturn main_mol_weight / ( main_Rho_sat * 1000 )\n", "\n", "main_P=1e5\n", "\n", "list_T = [273, 298, 373, 500, 600]\n", "\n", "# --- for loop -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "for i in range(3): # the function range 3 says i should loop 3 times with index starting from zero and increasing in 1 after each iteration\n", "\tprint(function_V(dummy_T=list_T[i],dummy_P=main_P))" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda root]", "language": "python", "name": "conda-root-py" }, "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.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }