{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# C - Tuples and lists\n", "\n", "![lec-0C-ga](lec-0C-ga.png)\n", "\n", "* To try it on *python tutor live*, [click here][preloaded ptl C]\n", "\n", "[preloaded ptl C]: http://www.pythontutor.com/live.html#code=%23%20%3D%3D%3D%20Lecture%20zero,%20part%20C%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%20B%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%20%23%20as%20before,%20this%20is%20a%20scalar%20value%0A%23%20---%20tuples%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Atuple_T%20%3D%20(273,%20298%29%20%23%20this%20is%20a%20tuple%0A%23%20---%20lists%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Alist_T%20%3D%20%5B273,%20298%5D%20%23%20now%20this%20is%20a%20list%0Aprint(list_T%5B0%5D%29%20%23%20we%20can%20print%20each%20element%20in%20the%20list%20by%20its%20index,%20starting%20from%20zero%0Aprint(list_T%5B1%5D%29%0A%23Lists%20and%20tuples%20are%20like%20arrays.%20Tuples%20are%20immutables.%20Lists%20are%20mutables%20so%20they%20can%20be%20extended%20or%20reduced%20at%20will.%0A%23%20---%20item%20assignment%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Alist_T%5B1%5D%20%3D%20600%20%23this%20will%20overwrite%20the%20value%20of%20%22600%22%20as%20a%20last%20element%20in%20the%20list%20which%20is%20referenced%20both%20by%20the%20name%20list_T%20and%20by%20the%20name%20list_T1%0Aprint(list_T%29%20%23this%20cannot%20be%20done%20with%20tuples%0A%23%20---%20concatenations%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Alist_T%20%2B%3D%20%5B373%5D%20%23%20this%20concatenates%20two%20lists%0Aprint(list_T%29%20%23this%20cannot%20be%20done%20with%20tuples%0A%23%20---%20using%20lists%20in%20function%20calls%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Ai%3D0%20%23assing%20%220%22%20to%20'i'%0Aprint(function_V(dummy_T%3Dlist_T%5Bi%5D,dummy_P%3Dmain_P%29%29%20%23%20call%20function_V%20with%20dummy_T%20being%20taken%20for%20the%20value%20at%20list_T%5B0%5D,%20therefore,%20273%0Ai%3D1%0Aprint(function_V(dummy_T%3Dlist_T%5Bi%5D,dummy_P%3Dmain_P%29%29%0Ai%3D2%0Aprint(function_V(dummy_T%3Dlist_T%5Bi%5D,dummy_P%3Dmain_P%29%29%0A%23%20---%20pointers%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Alist_T1%20%3D%20list_T%20%23%20with%20lists,%20%22%3D%22%20does%20not%20make%20an%20assignment%20of%20values,%20but%20rather%20an%20assignment%20of%20pointers%20to%20memory%20references%0Alist_T1%20%2B%3D%20%5B600%5D%20%23this%20will%20append%20a%20value%20of%20%22600%22%20as%20a%20last%20element%20in%20the%20list%20which%20is%20referenced%20both%20by%20the%20name%20list_T%20and%20by%20the%20name%20list_T1%0Aprint(list_T%29%0Aprint(list_T1%29%0Alist_T1%5B3%5D%20%2B%3D%20100%20%23%20this%20increments%20the%20value%20of%20the%20element%20with%20index%203%20(that%20will%20be%20the%20fourth%29%20by%20100%20of%20the%20list%20which%20is%20referenced%20both%20by%20the%20name%20list_T%20and%20by%20the%20name%20list_T1%0Aprint(list_T%29%0Aprint(list_T1%29%0A%23%20---%20pointers%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Atuple_T1%20%3D%20tuple_T%20%23%20with%20tuples,%20%22%3D%22%20does%20not%20make%20an%20assignment%20of%20values,%20but%20rather%20an%20assignment%20of%20pointers%20to%20memory%20references,%20creating%20a%20new%20view%20of%20the%20same%20data&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-live.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "273\n", "298\n", "[273, 600]\n", "[273, 600, 373]\n", "0.02269722\n", "0.049884\n", "0.03101122\n", "[273, 600, 373, 600]\n", "[273, 600, 373, 600]\n", "[273, 600, 373, 700]\n", "[273, 600, 373, 700]\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 C ========================================================================================================================================================================\n", "\n", "# --- Remember from part B -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\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", "# as before, this is a scalar value\n", "main_P=1e5\n", "\n", "# --- tuples -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "tuple_T = (273, 298) # this is a tuple\n", "\n", "# --- lists -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "list_T = [273, 298] # now this is a list\n", "print(list_T[0]) # we can print each element in the list by its index, starting from zero\n", "print(list_T[1])\n", "#Lists and tuples are like arrays. Tuples are immutables. Lists are mutables so they can be extended or reduced at will.\n", "# --- item assignment -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "list_T[1] = 600 #this will overwrite the value of \"600\" as a last element in the list which is referenced both by the name list_T and by the name list_T1\n", "print(list_T) #this cannot be done with tuples\n", "# --- concatenations -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "list_T += [373] # this concatenates two lists\n", "print(list_T) #this cannot be done with tuples\n", "# --- using lists in function calls -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "# we can define the values in the list at one point, as we did few lines ago\n", "\n", "# and then call the function using each value from the list by its index\n", "\n", "i=0 #assing \"0\" to 'i'\n", "print(function_V(dummy_T=list_T[i],dummy_P=main_P)) # call function_V with dummy_T being taken for the value at list_T[0], therefore, 273\n", "\n", "i=1\n", "print(function_V(dummy_T=list_T[i],dummy_P=main_P))\n", "\n", "i=2\n", "print(function_V(dummy_T=list_T[i],dummy_P=main_P))\n", "\n", "# now we might automate the recursion with \"for\" syntax -- see Lecture zero, part D\n", "\n", "# --- pointers -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "list_T1 = list_T # with lists, \"=\" does not make an assignment of values, but rather an assignment of pointers to memory references\n", "\n", "list_T1 += [600] #this will append a value of \"600\" as a last element in the list which is referenced both by the name list_T and by the name list_T1\n", "\n", "print(list_T)\n", "print(list_T1)\n", "\n", "list_T1[3] += 100 # this increments the value of the element with index 3 (that will be the fourth) by 100 of the list which is referenced both by the name list_T and by the name list_T1\n", "\n", "print(list_T)\n", "print(list_T1)\n", "\n", "# --- pointers -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "tuple_T1 = tuple_T # with tuples, \"=\" does not make an assignment of values, but rather an assignment of pointers to memory references, creating a new view of the same data'''\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 }