{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# A - Variables and operators\n", "\n", "![lec-0A-ga](lec-0A-ga.png)\n", "\n", "* To try it on *python tutor live*, [click here][preloaded ptl A]\n", "\n", "[preloaded ptl A]: http://www.pythontutor.com/live.html#code=%23%20%3D%3D%3D%20Lecture%20zero,%20part%20A%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---%20Assignments%20-------------------------------------------------------------------------------------------------------------------------------------------------------------%0AR%20%3D%208.314%20%23%20Read%20as%20%22%20The%20value%20%228.314%22%20is%20assigned%20(%3D%29%20to%20the%20variable%20%22R%22.%20%22%0A%23%20---%20print%20-------------------------------------------------------------------------------------------------------------------------------------------------------------%0Aprint(R%29%20%23%20print%20the%20value%20assigned%20to%20R%0AT%20%3D%20273%0AP%20%3D%201e5%20%23%201e5%20means%201%20x%2010%5E5%0A%23%20---%20Operators%20-------------------------------------------------------------------------------------------------------------------------------------------------------------%0AV%20%3D%20R%20*%20T%20/%20P%20%23%20%22*%22%20means%20multiplication,%20%22/%22%20means%20division%0Aprint(T,P,V%29%20%23%20print%20the%20values%20assigned%20to%20T,%20P%20and%20V%20(%20separate%20each%20object%20with%20commas%20%29%0A%23%20%3D%3D%3D%20Notes%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---%20re%20assignment%20----------------------------------------------------------------------------------------------------------------------------------------------------------------%0AT%20%3D%20298%20%23%20assigns%20the%20new%20value%20%22298%22%20to%20the%20variable%20%22T%22%0Aprint(T,P,V%29%20%23%20V%20does%20not%20change,%20the%20calculation%20we%20performed%20before%20was%20a%20one%20time%20assignment,%20not%20a%20function%0AV%20%3D%20R%20*%20T%20/%20P%20%23%20perform%20the%20calculation%20again%0Aprint(T,P,V%29%20%23%20now%20V%20did%20change%0A%23%20---%20case%20sensibility%20--------------------------------------------------------------------------------------------------------------------------------------------------------%0At%20%3D%20373%20%23%20small%20t%20is%20a%20new%20independent%20variable%0Aprint(t,T%29%20%23case%20sensibility%20makes%20t%20and%20T%20two%20different%20variables%0A%23%20---%20memory%20independence%20--------------------------------------------------------------------------------------------------------------------------------------------------------%0AT1%20%3D%20273%20%23%20assign%20the%20value%20273%20to%20T1%0AT2%20%3D%20T1%20%23%20assign%20the%20value%20of%20T1%20to%20T2%0Aprint(T1,T2%29%20%23%20the%20value%20assigned%20to%20both%20of%20them%20is%20273%0AT2%20%2B%3D%2025%20%23%20increment%20the%20value%20of%20T2%20by%2025%0Aprint(T1,T2%29%20%23%20the%20value%20assigned%20to%20T1%20is%20still%20273,%20the%20value%20assigned%20to%20T2%20is%20273%20%2B%2025,%20therefore%20298&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-live.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8.314\n", "273 100000.0 0.02269722\n", "298 100000.0 0.02269722\n", "298 100000.0 0.02477572\n", "373 298\n", "273 273\n", "273 298\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 A ========================================================================================================================================================================\n", "\n", "# --- Assignments -------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "R = 8.314\n", "\n", "# Read as \" The value \"8.314\" is assigned (=) to the variable \"R\". \"\n", "\n", "# Note the 'global frame' table\n", "\n", "# --- print -------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "# print the value assigned to R\n", "\n", "print(R)\n", "\n", "# Note the 'print output' field\n", "\n", "T = 273\n", "# Note a new line on the 'global frame' table\n", "\n", "P = 1e5 # 1e5 means 1 x 10^5\n", "# Note a new line on the 'global frame' table\n", "\n", "# assignment to the result of an operation\n", "\n", "# --- Operators -------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "V = R * T / P # \"*\" means multiplication, \"/\" means division\n", "# Note a new line on the 'global frame' table\n", "\n", "# print the values assigned to T, P and V ( separate each object with commas )\n", "print(T,P,V)\n", "# Note the 'print output' field\n", "\n", "# === Notes ========================================================================================================================================================================\n", "\n", "# --- re assignment ----------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "# assigns the new value \"298\" to the variable \"T\" \n", "\n", "T = 298\n", "# Note the value on the second column in the line on the 'global frame' table corresponding to the variable T now changes \n", "\n", "print(T,P,V) # V does not change, the calculation we performed before was a one time assignment, not a function\n", "# Note the 'print output' field\n", "\n", "V = R * T / P # perform the calculation again\n", "\n", "print(T,P,V) # now V did change\n", "# Note the 'print output' field\n", "\n", "# we can automate that with functions -- see Lecture zero, part B\n", "\n", "# --- case sensibility --------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "t = 373 # small t is a new independent variable\n", "# Note a new line on the 'global frame' table\n", "\n", "print(t,T) #case sensibility makes t and T two different variables\n", "# Note the 'print output' field\n", "\n", "# --- memory independence --------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "# say\n", "T1 = 273 # assign the value 273 to T1\n", "T2 = T1 # assign the value of T1 to T2\n", "print(T1,T2) # the value assigned to both of them is 273\n", "\n", "T2 += 25 # increment the value of T2 by 25\n", "print(T1,T2) # the value assigned to T1 is still 273, the value assigned to T2 is 273 + 25, therefore 298\n" ] } ], "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 }