<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Computer Science Circles</title>
	<atom:link href="http://cscircles.cemc.uwaterloo.ca/feed/" rel="self" type="application/rss+xml" />
	<link>http://cscircles.cemc.uwaterloo.ca</link>
	<description>01000011 01010011 01000011</description>
	<lastBuildDate>Thu, 17 May 2012 00:14:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>wpui test</title>
		<link>http://cscircles.cemc.uwaterloo.ca/2012/02/wpui-test/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=wpui-test</link>
		<comments>http://cscircles.cemc.uwaterloo.ca/2012/02/wpui-test/#comments</comments>
		<pubDate>Mon, 13 Feb 2012 19:59:01 +0000</pubDate>
		<dc:creator>Dave Pritchard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cemclinux1.math.uwaterloo.ca/~cscircles/swordpress/?p=3185</guid>
		<description><![CDATA[Horizontal Tabs Vertical Tabs]]></description>
			<content:encoded><![CDATA[<style>.wp-tabs img {display: block; margin: auto; border: none; height: 202px;}</style>
<p><b> Horizontal Tabs </b><br />
<div id="wp-tabs-1" class="wp-tabs wpui-blue wpui-styles"><h3 class="wp-tab-title">Memory</h3><div class="wp-tab-content"><div class="wp-tab-content-wrapper">We&#8217;ll use a table to represent the variables and values in Python&#8217;s memory. For example, after running the code
<pre>city = "Moose Factory"<br/>population = 2458<br/>employer = city</pre>this table (with the black border) shows what Python&#8217;s memory looks like:<br/><img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide1.png" alt="" />The first variable&#8217;s name is <code>city</code> and its value is the string <code>"Moose Factory"</code>. The second variable&#8217;s name is <code>population</code> and its value is the integer <code>2458</code>. The third variable&#8217;s name is <code>employer</code> and its value is the string <code>"Moose Factory"</code>.</div></div><!-- end div.wp-tab-content --><br/><h3 class="wp-tab-title">Lists in memory</h3><br/><div class="wp-tab-content"><div class="wp-tab-content-wrapper">Next, we show what a <em>list</em> looks like in memory. For example, take the code fragment<pre>myList = ["Moose Factory", 2458]</pre>This will just create one variable, named <code>myList</code>. A list is created, and the value of <code>myList</code> is set equal to &#8220;point&#8221; or &#8220;refer&#8221; to that list. We represent the list using a box, and the values of the list&#8217;s entries are shown inside the box next to their corresponding indices. The list is shown in blue.<br/><img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide2.png" alt="" />The arrow shows that <code>myList</code> refers to this new list. The element at index 0 of the list is the string <code>"Moose Factory"</code>, and the element at index 1 is the integer <code>2458</code>. For example, if you <code>print(myList[1])</code> then Python will output <code>2458</code>.</div></div><!-- end div.wp-tab-content --><br/><h3 class="wp-tab-title">Replacing a list value</h3><br/><div class="wp-tab-content"><div class="wp-tab-content-wrapper">Now we add one more line to the previous example, just for illustrative purposes. A baby is born, so we run the code fragment
<pre>myList = ["Moose Factory", 2458]<br/>myList[1] = myList[1]+1</pre>Python calculates <code>2458+1</code> which equals <code>2459</code>, and this replaces the value at index 1 of the list. After the update, we have the diagram shown below.<br/><img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide3.png" alt="" />(The <span style="text-decoration: line-through;">2458</span> isn&#8217;t part of Python&#8217;s memory, it is only shown to emphasize the change.)<br/></div></div><!-- end div.wp-tab-content --><br/><h3 class="wp-tab-title"><code>oldSize</code> and <code>newSize</code></h3><br/><div class="wp-tab-content"><div class="wp-tab-content-wrapper">Now we get back to the main example. The first line was<pre>oldSize = ["letter", 8.5, 11]</pre>and so Python&#8217;s memory looks like the diagram below after this line is executed. We created a list of length 3.<br/><img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide4.png" alt="" /></div></div><!-- end div.wp-tab-content --><br/><h3 class="wp-tab-title">Main problem</h3><br/><div class="wp-tab-content"><div class="wp-tab-content-wrapper">In our program, the second line is<pre>newSize = oldSize</pre>and here we actually find the main issue: in the second line, <strong><code>=</code> does not duplicate the list!</strong> Instead, it just copied a new reference (arrow) pointing to the same list (box). This is quite different from copying numbers or strings (compare with what happened in slide number 1).<img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide5.png" alt="" />As the diagram shows, we have two variables, which both refer to the same list.<br/></div></div><!-- end div.wp-tab-content --><br/><h3 class="wp-tab-title">Updating</h3><br/><div class="wp-tab-content"><div class="wp-tab-content-wrapper">Next, when the program reaches the line<pre>newSize[1] = newSize[1]*2.54</pre>Python looks at <code>newSize</code>, looks up the value of its index 1 (8.5) and multiplies it by 2.54, and then replaces the value, as shown. However, since <code>oldSize</code> referred to the same list, a side effect is that we also affected <code>oldSize</code>!<br/><img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide6.png" alt=""/>(Again <span style="text-decoration: line-through;">8.5</span> isn&#8217;t part of Python&#8217;s memory, and is shown to emphasize the change.)<br/></div></div><!-- end div.wp-tab-content --><br/><h3 class="wp-tab-title">Result</h3><br/><div class="wp-tab-content"><div class="wp-tab-content-wrapper">The next line is similar,<pre>newSize[2] = newSize[2]*2.54</pre>which affects the other value in the list. After this line executes, Python&#8217;s memory looks like the diagram shown below.<img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide7.png" alt="" />Now when we print either <code>newSize</code> <strong>or</strong> <code>oldSize</code>, Python outputs <code>["letter", 21.59, 27.94]</code>.</div></div><!-- end div.wp-tab-content --></div><!-- end div.wp-tabs --></p>
<p><b> Vertical Tabs </b><br />
<div id="wp-tabs-2" class="wp-tabs wpui-blue wpui-styles wpui-tabs-vertical"><h3 class="wp-tab-title">Memory</h3><div class="wp-tab-content"><div class="wp-tab-content-wrapper">We&#8217;ll use a table to represent the variables and values in Python&#8217;s memory. For example, after running the code
<pre>city = "Moose Factory"<br/>population = 2458<br/>employer = city</pre>this table (with the black border) shows what Python&#8217;s memory looks like:<br/><img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide1.png" alt="" />The first variable&#8217;s name is <code>city</code> and its value is the string <code>"Moose Factory"</code>. The second variable&#8217;s name is <code>population</code> and its value is the integer <code>2458</code>. The third variable&#8217;s name is <code>employer</code> and its value is the string <code>"Moose Factory"</code>.</div></div><!-- end div.wp-tab-content --><br/><h3 class="wp-tab-title">Lists in memory</h3><br/><div class="wp-tab-content"><div class="wp-tab-content-wrapper">Next, we show what a <em>list</em> looks like in memory. For example, take the code fragment<pre>myList = ["Moose Factory", 2458]</pre>This will just create one variable, named <code>myList</code>. A list is created, and the value of <code>myList</code> is set equal to &#8220;point&#8221; or &#8220;refer&#8221; to that list. We represent the list using a box, and the values of the list&#8217;s entries are shown inside the box next to their corresponding indices. The list is shown in blue.<br/><img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide2.png" alt="" />The arrow shows that <code>myList</code> refers to this new list. The element at index 0 of the list is the string <code>"Moose Factory"</code>, and the element at index 1 is the integer <code>2458</code>. For example, if you <code>print(myList[1])</code> then Python will output <code>2458</code>.</div></div><!-- end div.wp-tab-content --><br/><h3 class="wp-tab-title">Replacing a list value</h3><br/><div class="wp-tab-content"><div class="wp-tab-content-wrapper">Now we add one more line to the previous example, just for illustrative purposes. A baby is born, so we run the code fragment
<pre>myList = ["Moose Factory", 2458]<br/>myList[1] = myList[1]+1</pre>Python calculates <code>2458+1</code> which equals <code>2459</code>, and this replaces the value at index 1 of the list. After the update, we have the diagram shown below.<br/><img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide3.png" alt="" />(The <span style="text-decoration: line-through;">2458</span> isn&#8217;t part of Python&#8217;s memory, it is only shown to emphasize the change.)<br/></div></div><!-- end div.wp-tab-content --><br/><h3 class="wp-tab-title"><code>oldSize</code> and <code>newSize</code></h3><br/><div class="wp-tab-content"><div class="wp-tab-content-wrapper">Now we get back to the main example. The first line was<pre>oldSize = ["letter", 8.5, 11]</pre>and so Python&#8217;s memory looks like the diagram below after this line is executed. We created a list of length 3.<br/><img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide4.png" alt="" /></div></div><!-- end div.wp-tab-content --><br/><h3 class="wp-tab-title">Main problem</h3><br/><div class="wp-tab-content"><div class="wp-tab-content-wrapper">In our program, the second line is<pre>newSize = oldSize</pre>and here we actually find the main issue: in the second line, <strong><code>=</code> does not duplicate the list!</strong> Instead, it just copied a new reference (arrow) pointing to the same list (box). This is quite different from copying numbers or strings (compare with what happened in slide number 1).<img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide5.png" alt="" />As the diagram shows, we have two variables, which both refer to the same list.<br/></div></div><!-- end div.wp-tab-content --><br/><h3 class="wp-tab-title">Updating</h3><br/><div class="wp-tab-content"><div class="wp-tab-content-wrapper">Next, when the program reaches the line<pre>newSize[1] = newSize[1]*2.54</pre>Python looks at <code>newSize</code>, looks up the value of its index 1 (8.5) and multiplies it by 2.54, and then replaces the value, as shown. However, since <code>oldSize</code> referred to the same list, a side effect is that we also affected <code>oldSize</code>!<br/><img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide6.png" alt=""/>(Again <span style="text-decoration: line-through;">8.5</span> isn&#8217;t part of Python&#8217;s memory, and is shown to emphasize the change.)<br/></div></div><!-- end div.wp-tab-content --><br/><h3 class="wp-tab-title">Result</h3><br/><div class="wp-tab-content"><div class="wp-tab-content-wrapper">The next line is similar,<pre>newSize[2] = newSize[2]*2.54</pre>which affects the other value in the list. After this line executes, Python&#8217;s memory looks like the diagram shown below.<img class="alignnone" src="http://cemclinux1.math.uwaterloo.ca/~cscircles/files/L17/slide7.png" alt="" />Now when we print either <code>newSize</code> <strong>or</strong> <code>oldSize</code>, Python outputs <code>["letter", 21.59, 27.94]</code>.</div></div><!-- end div.wp-tab-content --></div><!-- end div.wp-tabs --></p>
]]></content:encoded>
			<wfw:commentRss>http://cscircles.cemc.uwaterloo.ca/2012/02/wpui-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

