{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# D - 'for' loops\n", "\n", "![lec-0D-ga](lec-0D-ga.png)\n", "\n", "* To try it on *python tutor live*, [click here][preloaded ptl D]\n", "\n", "[preloaded ptl D]: http://www.pythontutor.com/live.html#code=%23%20%3D%3D%3D%20Lecture%20zero,%20part%20D%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%20C%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Amain_R%20%3D%208.314%0Adef%20function_V(dummy_T,dummy_P%29%3A%0A%09return%20main_R%20*%20dummy_T%20/%20dummy_P%0Amain_P%3D1e5%0Alist_T%20%3D%20%5B273,%20298,%20373%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": [ "0.02269722\n", "0.02477572\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 D ========================================================================================================================================================================\n", "\n", "# --- Remember from part C -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "main_R = 8.314\n", "\n", "def function_V(dummy_T,dummy_P):\n", "\treturn main_R * dummy_T / dummy_P\n", "\n", "main_P=1e5\n", "\n", "list_T = [273, 298, 373]\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))\n", "\n", "# we might want to keep track of the results in each iteration and take decisions -- for that see \"if\" syntax in Lecture zero, part E" ] } ], "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 }