<?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>Rannsaich mo Inntinn &#187; Programming</title>
	<atom:link href="http://blog.tsmacdonald.com/archives/category/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.tsmacdonald.com</link>
	<description>Accessibly Technical</description>
	<lastBuildDate>Fri, 02 Apr 2010 15:13:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Battery Duration Testing Program</title>
		<link>http://blog.tsmacdonald.com/archives/277</link>
		<comments>http://blog.tsmacdonald.com/archives/277#comments</comments>
		<pubDate>Mon, 23 Mar 2009 17:07:40 +0000</pubDate>
		<dc:creator>Timmy</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.tsmacdonald.com/?p=277</guid>
		<description><![CDATA[Today, David Pogue (the NYTimes tech columnist) posted this on Twitter&#8211;a request for a program that would write the time every thirty seconds until the battery powering it died. I rose to the challenge and wrote this, in Python: ''' Copyright 2009 by Timmy Macdonald &#60;timmy@tsmacdonald.com&#62; This program is free software: you can redistribute it [...]<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
]]></description>
			<content:encoded><![CDATA[<p>Today, David Pogue (the NYTimes tech columnist) posted <a href="http://twitter.com/Pogue/status/1376015527" onclick="javascript:pageTracker._trackPageview ('/outbound/twitter.com');">this</a> on Twitter&#8211;a request for a program that would write the time every thirty seconds until the battery powering it died.</p>
<p>I rose to the challenge and wrote this, in Python:</p>
<pre style="color:#000000;background:#ffffff;"><span style="color:#696969; ">'''</span>
<span style="color:#696969; ">Copyright 2009 by Timmy Macdonald </span><span style="color:#0000e6; ">&lt;</span><span style="color:#7144c4; ">timmy@tsmacdonald.com</span><span style="color:#0000e6; ">&gt;</span>
<span style="color:#696969; ">This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.</span>
<span style="color:#696969; ">This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.</span>
<span style="color:#696969; ">You should have received a copy of the GNU General Public License</span>
<span style="color:#696969; ">along with this program.  If not, see &lt;</span><span style="color:#5555dd; ">http://www.gnu.org/licenses</span><span style="color:#696969; ">&gt;.</span>
<span style="color:#696969; ">'''</span>
<span style="color:#800000; font-weight:bold; ">import</span> time as t
f <span style="color:#808030; ">=</span> <span style="color:#e34adc; ">file</span><span style="color:#808030; ">(</span><span style="color:#0000e6; ">'battlog.txt'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'w'</span><span style="color:#808030; ">)</span>
total <span style="color:#808030; ">=</span> <span style="color:#008c00; ">0</span>
opener <span style="color:#808030; ">=</span> <span style="color:#0000e6; ">"Testing begun at"</span> <span style="color:#808030; ">+</span> <span style="color:#e34adc; ">str</span><span style="color:#808030; ">(</span>t<span style="color:#808030; ">.</span>localtime<span style="color:#808030; ">(</span>t<span style="color:#808030; ">.</span>time<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#808030; ">)</span>
<span style="color:#800000; font-weight:bold; ">print</span> opener
f<span style="color:#808030; ">.</span>write<span style="color:#808030; ">(</span>opener<span style="color:#808030; ">)</span>
f<span style="color:#808030; ">.</span>close<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span>
<span style="color:#800000; font-weight:bold; ">while</span> <span style="color:#e34adc; ">True</span><span style="color:#808030; ">:</span>
    f <span style="color:#808030; ">=</span> <span style="color:#e34adc; ">file</span><span style="color:#808030; ">(</span><span style="color:#0000e6; ">'battlog.txt'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'w'</span><span style="color:#808030; ">)</span>
    t<span style="color:#808030; ">.</span>sleep<span style="color:#808030; ">(</span><span style="color:#008c00; ">30</span><span style="color:#808030; ">)</span>
    <span style="color:#800000; font-weight:bold; ">print</span> t<span style="color:#808030; ">.</span>localtime<span style="color:#808030; ">(</span>t<span style="color:#808030; ">.</span>time<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span><span style="color:#808030; ">)</span>
    total <span style="color:#808030; ">+</span><span style="color:#808030; ">=</span> <span style="color:#008000; ">.5</span>
    message <span style="color:#808030; ">=</span> <span style="color:#0000e6; ">"30 seconds passed: it is now %s and this has been running \
for %s minutes."</span><span style="color:#808030; ">%</span><span style="color:#808030; ">(</span>t<span style="color:#808030; ">.</span>localtime<span style="color:#808030; ">(</span>t<span style="color:#808030; ">.</span>time<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#808030; ">,</span> total<span style="color:#808030; ">)</span>
    f<span style="color:#808030; ">.</span>write<span style="color:#808030; ">(</span>message<span style="color:#808030; ">)</span>
    <span style="color:#800000; font-weight:bold; ">print</span> message
    f<span style="color:#808030; ">.</span>close<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span></pre>
<p>Pogue <a href="http://twitter.com/Pogue/status/1376427571" onclick="javascript:pageTracker._trackPageview ('/outbound/twitter.com');">likes it</a>, and since I GPLed it and it could come in handy for someone else, I decided I&#8217;d put it up here, too.</p>
<p>&#8212;&#8211;</p>
<p><em>David Pogue</em> complimented me in front of <em>24,645</em> people!</p>
<p><strong>!</strong></p>
<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Rannsaich%20mo%20Inntinn&amp;siteurl=http%3A%2F%2Fblog.tsmacdonald.com%2F&amp;linkname=Battery%20Duration%20Testing%20Program&amp;linkurl=http%3A%2F%2Fblog.tsmacdonald.com%2Farchives%2F277" target="_blank"><img src="http://blog.tsmacdonald.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.tsmacdonald.com/archives/277/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An Object-Oriented Primer in Python: Part 2</title>
		<link>http://blog.tsmacdonald.com/archives/238</link>
		<comments>http://blog.tsmacdonald.com/archives/238#comments</comments>
		<pubDate>Sat, 21 Feb 2009 00:19:37 +0000</pubDate>
		<dc:creator>Timmy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[object-oriented programming]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.tsmacdonald.com/?p=238</guid>
		<description><![CDATA[In the first installment, we learned a bit about objects (they hold information, and do things), and how to use pre-built objects (object.method() for a method (aka function) and object.variable for a variable). Now let&#8217;s talk about making our own classes. The class statement: The way you make a class in Python is with&#8230;the class [...]<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://blog.tsmacdonald.com/archives/235">first installment</a>, we learned a bit about objects (they hold information, and do things), and how to use pre-built objects (object.method() for a method (aka function) and object.variable for a variable). Now let&#8217;s talk about making our own classes.</p>
<p><strong>The class statement:</strong></p>
<p>The way you make a class in Python is with&#8230;the class statement, the name of the class, and a colon. So our Dog class is:</p>
<pre style="color:#000000;background:#ffffff;"><span style="color:#800000; font-weight:bold; ">
class</span> Dog<span style="color:#808030; ">:
</span></pre>
<p>Hard, isn&#8217;t it? <img src='http://blog.tsmacdonald.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  And, in true Python style, everything that&#8217;s part of that class is indented. (But you already knew that, because of the colon, right? <img src='http://blog.tsmacdonald.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</p>
<p><strong>Methods</strong></p>
<p>Methods are made in a very similar manner to standard functions. There&#8217;s two main differences: They require a parameter called &#8220;self&#8221;, and there&#8217;s a few reserved methods that do special things.</p>
<p>Our Dog class had a method called showBreed(). It looked like this:</p>
<pre style="color:#000000;background:#ffffff;">    <span style="color:#800000; font-weight:bold; ">def</span> showBreed<span style="color:#808030; ">(</span>self<span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
        <span style="color:#800000; font-weight:bold; ">print</span> <span style="color:#0000e6; ">"It's a %s"</span><span style="color:#808030; ">%</span>self<span style="color:#808030; ">.</span>breed</pre>
<p>So we have the def statement, the name of the method, and then &#8220;self&#8221; is the only parameter. Self is kind of a tricky concept: It refers to the object that&#8217;s calling it. So when we print self.breed, we&#8217;re printing the string called breed <em>from whatever happens to be calling it</em>. If it helps, you can think of it as sort of a wildcard: It can be fido.breed, it can be spot.breed, it&#8217;s a placeholder to show that it&#8217;s <em>somebody&#8217;s</em> breed. If you don&#8217;t grok that, read it again: This was probably my biggest stumbling block when I learned it. Now that you have that cleared up, realize that you also need to put self in as the first parameter for every method you define, but that when you actually<em> call</em>, the method, you pretend it isn&#8217;t there. Maybe some error messages would help:</p>
<pre style="color:#000000;background:#ffffff;"><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span> <span style="color:#800000; font-weight:bold; ">class</span> Error<span style="color:#808030; ">:</span>
    <span style="color:#800000; font-weight:bold; ">def</span> cry<span style="color:#808030; ">(</span>self<span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
        <span style="color:#800000; font-weight:bold; ">print</span> <span style="color:#0000e6; ">"Method called"</span>

<span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span> e <span style="color:#808030; ">=</span> Error<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span>
<span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span> e<span style="color:#808030; ">.</span>cry<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span>
Method called
<span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span> e<span style="color:#808030; ">.</span>cry<span style="color:#808030; ">(</span>self<span style="color:#808030; ">)</span>

Traceback <span style="color:#808030; ">(</span>most recent call last<span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
  File <span style="color:#0000e6; ">"&lt;pyshell#6&gt;"</span><span style="color:#808030; ">,</span> line <span style="color:#008c00; ">1</span><span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">in</span> <span style="color:#808030; ">&lt;</span>module<span style="color:#808030; ">&gt;</span>
    e<span style="color:#808030; ">.</span>cry<span style="color:#808030; ">(</span>self<span style="color:#808030; ">)</span>
<span style="color:#e34adc; ">NameError</span><span style="color:#808030; ">:</span> name <span style="color:#0000e6; ">'self'</span> <span style="color:#800000; font-weight:bold; ">is</span> <span style="color:#800000; font-weight:bold; ">not</span> defined
<span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span> <span style="color:#800000; font-weight:bold; ">class</span> OtherError<span style="color:#808030; ">:</span>
    <span style="color:#800000; font-weight:bold; ">def</span> cry<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
        <span style="color:#800000; font-weight:bold; ">print</span> <span style="color:#0000e6; ">"Other method called"</span>

<span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span> o <span style="color:#808030; ">=</span> OtherError<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span>
<span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span> o<span style="color:#808030; ">.</span>cry<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span>

Traceback <span style="color:#808030; ">(</span>most recent call last<span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
  File <span style="color:#0000e6; ">"&lt;pyshell#14&gt;"</span><span style="color:#808030; ">,</span> line <span style="color:#008c00; ">1</span><span style="color:#808030; ">,</span> <span style="color:#800000; font-weight:bold; ">in</span> <span style="color:#808030; ">&lt;</span>module<span style="color:#808030; ">&gt;</span>
    o<span style="color:#808030; ">.</span>cry<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span>
<span style="color:#e34adc; ">TypeError</span><span style="color:#808030; ">:</span> cry<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> takes no arguments <span style="color:#808030; ">(</span><span style="color:#008c00; ">1</span> given<span style="color:#808030; ">)</span></pre>
<p>So, as you can see, calling a method with &#8220;self&#8221; raises one error, and defining a method with no &#8220;self&#8221; raises another one when it&#8217;s called&#8211;when you call it Python automatically includes the &#8220;self&#8221; (cry() takes no arguments  (1 given)).</p>
<p><strong>__init__():</strong></p>
<p>Now we come to one of the most important methods: __init__(): Observe:</p>
<pre style="color:#000000;background:#ffffff;"><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span> <span style="color:#800000; font-weight:bold; ">class</span> Blog<span style="color:#808030; ">:</span>
    <span style="color:#800000; font-weight:bold; ">def</span> <span style="color:#e34adc; ">__init__</span><span style="color:#808030; ">(</span>self<span style="color:#808030; ">,</span> name<span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
        self<span style="color:#808030; ">.</span>name <span style="color:#808030; ">=</span> name

<span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span> RmI <span style="color:#808030; ">=</span> Blog<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Rannsaich mo Inntinn"</span><span style="color:#808030; ">)</span>
<span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">&gt;</span> RmI<span style="color:#808030; ">.</span>name
<span style="color:#0000e6; ">'Rannsaich mo Inntinn'</span></pre>
<p>__init__ (you can call it a constructor if you want) is called as soon as an object is <strong>init</strong>iated. Note how I never explicitly called it. The astute may have noticed that when I created the RmI object, it&#8217;s arguments matched __init__&#8217;s perfectly&#8211;the self is implicit, and the name is &#8220;Rannsaich mo Inntinn&#8221;. The very astute may have noticed that, actually, Blog was defined with no arguments in the beginning (you can have class Blog():, and put stuff in the parentheses, but that&#8217;s called inheritance and is a topic for another day).</p>
<p>You also see __init__ doing something it does a lot, doing self.xxxx = xxxx. This takes the xxxx (alright, in this case it&#8217;s name) parameter, and gives it a home as part of that object. Because if we <em>didn&#8217;t</em> do self.name = name, whatever value name was would get deleted as soon as __init__ was over, and there&#8217;d be no way of using it in other methods. Of course, you can use that elsewhere to, in <a href="http://blog.tsmacdonald.com/archives/235">Part 1</a> we manipulated self.breed directly, and you can also change self.xxxx from other methods. But __init__ is really the best for, um, initializing them.</p>
<p>Once you swallow all of that, go back to that Dog class, and see if <em>you</em> can write it. You know all of the components&#8211;try fitting them together. If you need help, post it in the comments.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>Here&#8217;s my Dog class, which you really shouldn&#8217;t be looking at, but&#8230;</p>
<pre style="color:#000000;background:#ffffff;"><span style="color:#800000; font-weight:bold; ">class</span> Dog<span style="color:#808030; ">:</span>
    <span style="color:#800000; font-weight:bold; ">def</span> <span style="color:#e34adc; ">__init__</span><span style="color:#808030; ">(</span>self<span style="color:#808030; ">,</span> breed<span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
        self<span style="color:#808030; ">.</span>breed <span style="color:#808030; ">=</span> breed
    <span style="color:#800000; font-weight:bold; ">def</span> showBreed<span style="color:#808030; ">(</span>self<span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
        <span style="color:#800000; font-weight:bold; ">print</span> <span style="color:#0000e6; ">"It's a %s"</span><span style="color:#808030; ">%</span>self<span style="color:#808030; ">.</span>breed
    <span style="color:#800000; font-weight:bold; ">def</span> changeBreed<span style="color:#808030; ">(</span>self<span style="color:#808030; ">,</span> breed<span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
        self<span style="color:#808030; ">.</span>breed <span style="color:#808030; ">=</span> breed
-----------</pre>
<p>And so ends the series. There&#8217;s a lot of things I haven&#8217;t covered&#8211;the __del__ method, polymorphism (having the same method name do something different depending on what class it belongs too), inheritance (I couldn&#8217;ve had a Pet superclass, with name, breed, etc. and then Dog and Cat subclasses that <em>inherited</em> methods and attributes from Pet, but had some of their own (i. e. Dog.woof(), Cat.washBody()&#8230;) and a bunch of other stuff. But this is really all you need to know to do well&#8211;everything else is just very important icing. And there&#8217;s certainly enough tutorials out there for that.</p>
<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Rannsaich%20mo%20Inntinn&amp;siteurl=http%3A%2F%2Fblog.tsmacdonald.com%2F&amp;linkname=An%20Object-Oriented%20Primer%20in%20Python%3A%20Part%202&amp;linkurl=http%3A%2F%2Fblog.tsmacdonald.com%2Farchives%2F238" target="_blank"><img src="http://blog.tsmacdonald.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.tsmacdonald.com/archives/238/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>An Object-Oriented Primer in Python: Part 1</title>
		<link>http://blog.tsmacdonald.com/archives/235</link>
		<comments>http://blog.tsmacdonald.com/archives/235#comments</comments>
		<pubDate>Wed, 18 Feb 2009 23:44:43 +0000</pubDate>
		<dc:creator>Timmy</dc:creator>
				<category><![CDATA[Multi-part Series]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.tsmacdonald.com/?p=235</guid>
		<description><![CDATA[I&#8217;ve read several tutorials on Object-Oriented Programming (OOP), and none of them has been as clear as I thought they should be. So it took me a long time to figure out OOP, and now I feel like I should pass on the knowledge. Here it is: Objects One of the main difficulties in OOP [...]<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve read several tutorials on Object-Oriented Programming (OOP), and none of them has been as clear as I thought they should be. So it took me a long time to figure out OOP, and now I feel like I should pass on the knowledge. Here it is:</p>
<p><strong>Objects</strong></p>
<p>One of the main difficulties in OOP is <em>thinking</em> about it. So start trying to wrap your mind around an object. Basically, an object is a specialized data structure than can <strong>hold</strong> information, and <strong>do</strong> things with it. In other words, it&#8217;s a <em>thing</em> represented by code. And you aren&#8217;t limited to just one thing, you can have as many as you want, just as you can have as many numbers, lists or dictionaries as you want.</p>
<p><strong>Holding Information and Doing Things<br />
</strong></p>
<p>You can give objects attributes. What&#8217;s even cooler is that you can access them really specifically:</p>
<pre>fido = Dog("Golden Retriever")
fido.breed</pre>
<p>Assuming you&#8217;ve made a &#8220;Dog&#8221; class, fido.breed will be a string saying &#8220;Golden Retriever.&#8221; You can also change the data (it&#8217;d be pretty pointless if you couldn&#8217;t&#8230;) :</p>
<pre>&gt;&gt;&gt; fido = Dog("Golden Retriever")
&gt;&gt;&gt; fido.showBreed()
It's a Golden Retriever
&gt;&gt;&gt; fido.breed = "Labrador"
&gt;&gt;&gt; fido.showBreed()
It's a Labrador
&gt;&gt;&gt; fido.changeBreed("Dalmatian")
&gt;&gt;&gt; fido.showBreed()
It's a Dalmatian
&gt;&gt;&gt; fido.breed
'Dalmatian'</pre>
<p>Here we make a Golden Retriever named fido. We have it call a method (OOP-speak for function) to say what breed it is, and then change it&#8217;s breed variable to &#8220;Labrador.&#8221; After that, we use a different method (yes, objects can have multiple methods) to change the breed to &#8220;Dalmatian,&#8221; and have fido tell us that he is, in fact, a Dalmatian. If you feel like having to prefix everything with &#8220;fido.&#8221; is annoying, don&#8217;t worry: There&#8217;s a reason. We could just as easily have:</p>
<pre>&gt;&gt;&gt; fido = Dog("Golden Retriever")
&gt;&gt;&gt; spot = Dog("Mutt")
&gt;&gt;&gt; fido.showBreed()
It's a Golden Retriever
&gt;&gt;&gt; spot.showBreed()
It's a Mutt</pre>
<p>If we just used a generic &#8216;showBreed()&#8217; method, Python wouldn&#8217;t know whether we were talking about fido or spot, or some other dog. The &lt;name&gt;&lt;dot&gt; system is a pretty efficient way of telling Python who&#8217;s doing what, or who&#8217;s remembering what.</p>
<p>Look back over this section and make sure you understand it well: We made an <em>instance</em> of the Dog <em>class</em>, which <em>held information</em> (fido.breed) and <em>did things</em> (fido.showBreed()), we also made another instance of the <em>same Dog class</em> (spot), which held different, but similar information (Mutt instead of Golden Retriever), and did similar things (said what breed he, spot, was, as opposed to what fido was).</p>
<p>Obviously stuff can get a lot more complicated, but if you understand that, you&#8217;re doing fine!</p>
<p>~Read <a href="http://blog.tsmacdonald.com/archives/238">Part 2</a> to learn how to <em>make</em> classes~<br />
<script type="text/javascript"><!--
  reddit_url = "http://blog.tsmacdonald.com/archives/235";
// --></script><br />
<script src="http://www.reddit.com/r/programming/button.js?t=3" type="text/javascript"></script></p>
<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Rannsaich%20mo%20Inntinn&amp;siteurl=http%3A%2F%2Fblog.tsmacdonald.com%2F&amp;linkname=An%20Object-Oriented%20Primer%20in%20Python%3A%20Part%201&amp;linkurl=http%3A%2F%2Fblog.tsmacdonald.com%2Farchives%2F235" target="_blank"><img src="http://blog.tsmacdonald.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.tsmacdonald.com/archives/235/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Web Design Woes</title>
		<link>http://blog.tsmacdonald.com/archives/113</link>
		<comments>http://blog.tsmacdonald.com/archives/113#comments</comments>
		<pubDate>Fri, 21 Nov 2008 21:47:54 +0000</pubDate>
		<dc:creator>Timmy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[The Internet]]></category>

		<guid isPermaLink="false">http://tmac.andrewmin.com/blog/?p=113</guid>
		<description><![CDATA[I&#8217;ll tell you right off the bat&#8211;this is not an &#8220;article&#8221;, just a post. Now that we&#8217;re clear: I take back a lot of what I said about HTML, CSS and the W3C. It&#8217;s all a good idea, and I now realize that. That said, I&#8217;m still a bit touchy about the Validator. Want proof? [...]<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll tell you right off the bat&#8211;this is not an &#8220;article&#8221;, just a post. Now that we&#8217;re clear:</p>
<p>I take back a lot of what I said about HTML, CSS and the W3C. It&#8217;s all a good idea, and I now realize that. That said, I&#8217;m still a bit touchy about the Validator. Want proof? <a href="http://www.andrewmin.com/oldsites/version5andrewmin.com/index.php" onclick="javascript:pageTracker._trackPageview ('/outbound/www.andrewmin.com');">This page</a> was validated in 2008, but running it through the Validator again brings up four errors and two warnings. I&#8217;m sure they have their reasons, but I still feel that my anger is justified.</p>
<p>All of that said, I&#8217;ve been redoing <em>my</em> website to:</p>
<ul>
<li>Use HTML for page logic.</li>
<li>Use CSS for page looks (a challenge, considering that as of this morning I didn&#8217;t know CSS at ALL.)</li>
<li>Make the little navbar thingy towards the top a lot easier to maintain.</li>
<li>Pass the Validator (OK, OK, maybe Tidy HTML can help a bit.)</li>
</ul>
<p>I also took advantage of the shuffle to rewrite the content a bit.</p>
<p>So far, I&#8217;ve finished <a href="http://experimental.tmac.andrewmin.com/indexcsstidy.html" onclick="javascript:pageTracker._trackPageview ('/outbound/experimental.tmac.andrewmin.com');">one page</a>, which was based on <a href="http://tmac.andrewmin.com/about.html" onclick="javascript:pageTracker._trackPageview ('/outbound/tmac.andrewmin.com');">this one</a>, but will now double as my index too.</p>
<p>A few notes:</p>
<ul>
<li>All the CSS on it was whatever I could hack in a few hours using Google, part of <a href="http://www.freesoftwaremagazine.com/articles/learning_html_monty_python_style?page=0%2C2" onclick="javascript:pageTracker._trackPageview ('/outbound/www.freesoftwaremagazine.com');">this</a>, scientific experiments with simple pages and Tidy HTML and logic. I have no idea how &#8216;nice&#8217; it is (in terms of coding style) but it appeases my eyes, the Validator, and the end product, so I&#8217;m not complaining. I do not recommend this method of learning for the faint of heart. (Yes, I <em>am</em> going to do a proper tutorial this summer, I just wanted results NOW.)</li>
<li>I hacked together some Javascript for my navbar. This was accomplished by: Looking at sample code, and writing like a Pythoneer while using the Java syntax I&#8217;m learning in school.</li>
<li>So far, the Validator is happy!</li>
</ul>
<p>Some of you might be wondering what happened to my colors. Answer: I&#8217;m colorblind, and as such have absolutely no sense for what looks good on a page. I began to think that all the blue might be a bit overwhelming, so I&#8217;m going old-school with the black-on-white. If you have <em>any</em> opinion on this, <em>please</em> relate it in the comments. I would really appreciate it.</p>
<p>I&#8217;m a bit miffed that this means absolutely nothing to people looking at my site with polite browsers, but I guess this is what they would call a <a href="http://www.rampantgames.com/blog/2004/10/black-triangle.html" onclick="javascript:pageTracker._trackPageview ('/outbound/www.rampantgames.com');">Black Triangle</a>. It makes me feel <em>really</em> proud of myself though, which is one of the reasons I love coding.</p>
<p>Goal: Finish the rest of the pages by the end of this weekend, and move everything from my &#8216;experimental&#8217;* subdomain to the real deal.</p>
<p>*That thing is <em>great</em> for organization.</p>
<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Rannsaich%20mo%20Inntinn&amp;siteurl=http%3A%2F%2Fblog.tsmacdonald.com%2F&amp;linkname=Web%20Design%20Woes&amp;linkurl=http%3A%2F%2Fblog.tsmacdonald.com%2Farchives%2F113" target="_blank"><img src="http://blog.tsmacdonald.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.tsmacdonald.com/archives/113/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python and C&#8230;Wow&#8230;</title>
		<link>http://blog.tsmacdonald.com/archives/77</link>
		<comments>http://blog.tsmacdonald.com/archives/77#comments</comments>
		<pubDate>Wed, 05 Nov 2008 11:37:23 +0000</pubDate>
		<dc:creator>Timmy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[fast]]></category>
		<category><![CDATA[interpret]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://tmac.andrewmin.com/blog/?p=77</guid>
		<description><![CDATA[I&#8217;ve come to the realization that as nice as Python is, I really ought to learn a lower-level language. Even though I own two C++ books, I&#8217;m only reading them to a)Gain a rudimentary knowledge of the C++ syntax, such that I can hack it if I have to b)Improve my conceputal knowledge c)Well, they [...]<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve come to the realization that as nice as Python is, I really ought to learn a lower-level language. Even though I <em>own</em> two C++ books, I&#8217;m only reading them to a)Gain a rudimentary knowledge of the C++ syntax, such that I can hack it if I have to b)Improve my conceputal knowledge c)Well, they <em>are</em> interesting.</p>
<p>Instead, I&#8217;m learning C, because it just seems like more of my cup of tea. And kernel hacking might be fun <img src='http://blog.tsmacdonald.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>So one of the sample programs in the tutorial I&#8217;m using is that which will list all the numbers from 1-2500, and say if they&#8217;re prime or not. I faithfully wrote and compiled it in C, ran it, and <em>WOW!</em> that was fast. I could not detect any sort of delay, or processing or anything, it was just:</p>
<p>&lt;Enter&gt;&lt;A screenfull of numbers, ending in &#8220;2500 is not prime&#8221; appears&gt;</p>
<p>I was so impressed by this, I converted the code to Python. I even removed a function that listed the version number and time compiled. When I ran it, I could see numbers whizzing by the screen for half a second or so, and then it was done.</p>
<p>I know it&#8217;s old news that compiled languages are faster than interpreted ones, and that low-level languages are faster than high-level ones, but it&#8217;s just <em>impressive</em> to tangibly see it.</p>
<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Rannsaich%20mo%20Inntinn&amp;siteurl=http%3A%2F%2Fblog.tsmacdonald.com%2F&amp;linkname=Python%20and%20C%26%238230%3BWow%26%238230%3B&amp;linkurl=http%3A%2F%2Fblog.tsmacdonald.com%2Farchives%2F77" target="_blank"><img src="http://blog.tsmacdonald.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.tsmacdonald.com/archives/77/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Playing Python, Programming Pianos</title>
		<link>http://blog.tsmacdonald.com/archives/63</link>
		<comments>http://blog.tsmacdonald.com/archives/63#comments</comments>
		<pubDate>Wed, 22 Oct 2008 14:53:31 +0000</pubDate>
		<dc:creator>Timmy</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://tmac.andrewmin.com/blog/?p=63</guid>
		<description><![CDATA[New programmers are never sure what it &#8220;takes&#8221; to learn a programming language. This is not helped by the quantity of &#8220;Learn x language in 24 hours/3 days/etc.!&#8221; books that are out there, and by the overabundance of technical information available. So what I propose is an analogy. You might think that the analogy I [...]<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
]]></description>
			<content:encoded><![CDATA[<p>New programmers are never sure what it &#8220;takes&#8221; to learn a programming language. This is not helped by the quantity of <a href="http://norvig.com/21-days.html" onclick="javascript:pageTracker._trackPageview ('/outbound/norvig.com');">&#8220;Learn x language in 24 hours/3 days/etc.!&#8221;</a> books that are out there, and by the overabundance of technical information available.</p>
<p>So what I propose is an analogy.</p>
<p>You might think that the analogy I would make would compare learning a programming language to learning a foreign language (e. g. Spanish.) But after much deliberation, I don&#8217;t think that&#8217;s a valid for several reasons:</p>
<ul>
<li>Foreign languages are almost always more complex than programming languages. Something like French has evolved for the past two millenia, while even &#8216;old&#8217; programming languages have only been around for 50 years. Furthermore, French has the task of concisely communicating any conceivable expression to another human, while C/Java/Delphi only has to boss around some chunks of metal and silicon.</li>
<li>You <em>must</em> memorize every aspect of a foreign language, while with programming languages you have the all-powerful documentation to help you with some of the less common things.</li>
<li>Even if you know that &#8220;Hello&#8221; is the most common greeting in English, you&#8217;ll sound dumb if you don&#8217;t use &#8220;Hi&#8221;, &#8220;Hey&#8221;, and &#8220;What&#8217;s up?&#8221; in the right circumstances. Meanwhile, BASIC will always think &#8220;10 PRINT &#8220;HELLO, WORLD!&#8221; is the best way of doing things.</li>
<li>Similar to the first one, foreign languages have a MUCH bigger vocabulary to learn than programming languages.</li>
</ul>
<p>So I really don&#8217;t think it&#8217;s a valid comparison.</p>
<p>What I consider more vaild is comparing programming languages to musical instruments. First, let&#8217;s think about a story about little Billy.</p>
<blockquote><p>Little Billy decided one morning that, since he really like music, he wanted to learn to play a musical instrument, so he could make his own music. He went to the music store, and was initially awed by the sheer quantity of different types of instruments for sale. After much inner turmoil, he decided to use the piano, since it was common, rationalizing his choice by thinking, &#8220;Since so many people use it, it must be one of the best choices. There&#8217;ll also be more people who can help me. Finally, it sounds good.&#8221; While he was in the music store, he also picked up a tutorial on learning how to play the piano.</p>
<p>He went home, and did a chapter of the book per day, being sure to actually <em>play</em> the piano as much as possible. By the time he finished working through the book, he was a decent piano player, but still had a lot to learn. He kept playing, hired a teacher, and within a few years he was downright adept.</p></blockquote>
<p>Now consider little Billy&#8217;s brother&#8211;Tom.</p>
<blockquote><p>Tom decided one morning that, since he really like computers, he wanted to learn to program, so he could make his own programs. He went online, and was initially awed by the sheer quantity of different types of programming languages available. After much inner turmoil, he decided to use Python, since it was common, rationalizing his choice by thinking, &#8220;Since so many people use it, it must be one of the best choices. There&#8217;ll also be more people who can help me. Finally, the syntax looks good.&#8221; While he was online, he also picked up a tutorial on programming in Python.</p>
<p>He went home, and did a chapter of the tutorial per day, being sure to actually <em>do</em> the programs as much as possible. By the time he finished working through the tutorial, he was a decent programmer, but still had a lot to learn. He kept programming, started working on projects so he could learn from the project chief, and within a few years he was downright adept.</p></blockquote>
<p>Now that you know where I&#8217;m going with this, I can fill in some details:</p>
<p><strong>They both give you &#8216;instant success.&#8217;</strong> Anybody, and I mean anybody, can either make his computer say &#8220;Hello World!&#8221;, or play a few notes on an instrument within ten minutes.</p>
<p><strong>They both have specific technique.</strong> Java programmers know that using a variable without declaring it first is a big no-no. Trumpet players will fail if they just move their fingers&#8211;and don&#8217;t blow.  Another way of saying this is that <strong>they both teach through failure</strong>&#8211;be it a bad sound or a segfault.</p>
<p><strong>They both provide gratification. </strong>People get really excited when they play a tune well, or solve a particularly sticky programming problem.</p>
<p><strong>If you do something again, you&#8217;ll do it better</strong> When an elementary school band plays &#8220;The Star-Spangled Banner&#8221;, it is <em>very</em> apparent that it&#8217;s not the New York Philharmonic playing &#8220;The Star-Spanled Banner.&#8221; A first-year programmer will use a very different method for solving a problem when compared to an old-hand.</p>
<p>So if you&#8217;ve ever learned how to play an instrument, you&#8217;re primed for programming. And if you&#8217;ve ever programmed, you know what it takes to play an instrument.</p>
<p>&#8212;&#8212;-</p>
<p>I&#8217;m kind of interested in the reception this is going to get&#8211;please let me know what you think!</p>
<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Rannsaich%20mo%20Inntinn&amp;siteurl=http%3A%2F%2Fblog.tsmacdonald.com%2F&amp;linkname=Playing%20Python%2C%20Programming%20Pianos&amp;linkurl=http%3A%2F%2Fblog.tsmacdonald.com%2Farchives%2F63" target="_blank"><img src="http://blog.tsmacdonald.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.tsmacdonald.com/archives/63/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Really concise explanation of random integers in Python</title>
		<link>http://blog.tsmacdonald.com/archives/52</link>
		<comments>http://blog.tsmacdonald.com/archives/52#comments</comments>
		<pubDate>Sun, 05 Oct 2008 16:44:54 +0000</pubDate>
		<dc:creator>Timmy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://tmac.andrewmin.com/blog/?p=52</guid>
		<description><![CDATA[I wanted to use Python to generate some random integers, and a few minutes of Googling was not very helpful. I figured it out in the end, but I feel like something this basic should be easy. First you need to import the random module, so: import random Then, to get a random integer from [...]<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
]]></description>
			<content:encoded><![CDATA[<p>I wanted to use Python to generate some random integers, and a few minutes of Googling was not very helpful. I figured it out in the end, but I feel like something this basic should be easy.</p>
<p>First you need to import the random module, so:</p>
<blockquote><p>import random</p></blockquote>
<p>Then, to get a random integer from 1 to x, use</p>
<blockquote><p>random.randrange(x)</p></blockquote>
<p>It&#8217;s as easy as that.</p>
<p>(OK, OK, sorry. I know this probably isn&#8217;t of general interest, and it&#8217;s probably of minimal help, but I was just <em>irritated</em> that it wasn&#8217;t <strong>really</strong> easy to find online.)</p>
<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Rannsaich%20mo%20Inntinn&amp;siteurl=http%3A%2F%2Fblog.tsmacdonald.com%2F&amp;linkname=Really%20concise%20explanation%20of%20random%20integers%20in%20Python&amp;linkurl=http%3A%2F%2Fblog.tsmacdonald.com%2Farchives%2F52" target="_blank"><img src="http://blog.tsmacdonald.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.tsmacdonald.com/archives/52/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TI-BASIC (Round Three) (Control Flow: If-Then-End)</title>
		<link>http://blog.tsmacdonald.com/archives/40</link>
		<comments>http://blog.tsmacdonald.com/archives/40#comments</comments>
		<pubDate>Tue, 30 Sep 2008 15:45:18 +0000</pubDate>
		<dc:creator>Timmy</dc:creator>
				<category><![CDATA[Multi-part Series]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[calculator programming]]></category>
		<category><![CDATA[TI-BASIC]]></category>

		<guid isPermaLink="false">http://tmac.andrewmin.com/blog/?p=40</guid>
		<description><![CDATA[Consider the following code: 1-&#62;A If A = 1 Disp "A=1" Let&#8217;s study it a bit. The first line just stores 1 as A. Standard stuff. The second line is freaky, and the third line just displays &#8220;A=1&#8243;. Standard stuff. Let&#8217;s go back to the freaky part. It introduces one of the more powerful commands [...]<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
]]></description>
			<content:encoded><![CDATA[<p>Consider the following code:</p>
<pre>1-&gt;A</pre>
<pre>If A = 1
Disp "A=1"</pre>
<p>Let&#8217;s study it a bit. The first line just stores 1 as A. Standard stuff. The second line is freaky, and the third line just displays &#8220;A=1&#8243;. Standard stuff. Let&#8217;s go back to the freaky part. It introduces one of the more powerful commands in TI-BASIC: If. What it says, is that <em>if</em> A equals 1, to do something. The syntax isn&#8217;t nearly as remarkable as the code, just If [value][thingy][value]. Thingy can be anything from the &#8216;Test&#8217; menu. =, &lt;, &gt;, etc.</p>
<p><em>Note: Due to the constraints of this keyboard, the less-than-or-equal-to sign will be written as &lt;=, and the greater-than-or-equal-to sign will be written as &gt;=. Also, the not-equal-to sign will be written as !=.</em></p>
<p>If you&#8217;re looking for &#8216;if&#8217;, it&#8217;s under &#8216;Ctl&#8217;, not &#8216;I/O&#8217;. And this is the first time we&#8217;re using &#8216;Ctl&#8217;&#8230;Yay! OK, digression over <img src='http://blog.tsmacdonald.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If&#8217;s power comes from the fact that it introduces uncertainty into the program. This means the program can rocket off in a lot more directions, which means: <em>Sweeeeeeet</em>.</p>
<p>Since you handled that chunk of code so beautifully, let&#8217;s consider this one too.</p>
<pre>Input A
If A=1
Disp "A=1"
Disp "Where is this?"</pre>
<p>I hope you&#8217;re wondering if the &#8216;Where is this?&#8217; line is part of the if statement or not. It&#8217;s not. The only command that&#8217;s conditional is the one immediately after the if line. Which is really useful for a code snippet like this:</p>
<pre>If A=1
Disp "A is 1"
If A=2
Disp "A is 2"
If A&gt;2
Disp "A is pretty big"
If A&lt;1
Disp "A is pretty small"

That's a lot cleaner than the following method, which is also legal.</pre>
<pre>If A=1
Then
Disp "A is 1"
End
If A=2
Then
Disp "A is 2"
End
If A&gt;2
Then
Disp "A is pretty big"
End
If A&lt;1
Then
Disp "A is pretty small"
End</pre>
<p>You&#8217;re probably wondering <em>why</em> that&#8217;s legal. It&#8217;s stinking huge! Which brings us back to our problem&#8211;if only lasts for a line. If-Then-End fixes that problem. Then means that all of the following instructions are only to happen if the statement is true, and End means that an end has been put to the conditional instructions. There&#8217;s no <em>arguments</em> required for Then or End (an argument is something that has to be put with it to make the syntax legal.) So let&#8217;s look at our &#8216;Guess!&#8217; program again. Last time we&#8217;d gotten this far:</p>
<pre>Disp "Guess!","","(GPL) 2008", "T. Macdonald"</pre>
<pre>#Note that going from the outline to the implementation there was a better idea--putting 'Guess!' and the GPL on separate lines.
Input "Guess:".G
#Oh no! We don't know how to make a random number!
#Oh no! We don't know how to say if the number is too big or too small!
#(That's why the tutorial's not done. Stay tuned.)

And now we do know how to say if a number is too big or too small!
So
#I'm not copying all the code, we'll pick it up here:
Input "Guess:", G
If G&lt;R
Disp "Too small!"
If G&gt;R
Disp "Too big!"
If G=R
Disp "You win!"</pre>
<p>Unfortunately, that&#8217;s really all we can do with that, as of right now.<br />
Next time we&#8217;ll talk about Else.</p>
<p>Assignment: Write a program that will ask the user for his age, and then will classify them as &#8220;Kid&#8221; (12 and under), &#8220;Teenager&#8221; (13-19) or &#8220;Adult&#8221; (20+).</p>
<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Rannsaich%20mo%20Inntinn&amp;siteurl=http%3A%2F%2Fblog.tsmacdonald.com%2F&amp;linkname=TI-BASIC%20%28Round%20Three%29%20%28Control%20Flow%3A%20If-Then-End%29&amp;linkurl=http%3A%2F%2Fblog.tsmacdonald.com%2Farchives%2F40" target="_blank"><img src="http://blog.tsmacdonald.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.tsmacdonald.com/archives/40/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TI-BASIC (Round Two-and-a-Half) (More with Input)</title>
		<link>http://blog.tsmacdonald.com/archives/27</link>
		<comments>http://blog.tsmacdonald.com/archives/27#comments</comments>
		<pubDate>Sat, 27 Sep 2008 17:07:42 +0000</pubDate>
		<dc:creator>Timmy</dc:creator>
				<category><![CDATA[Multi-part Series]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[BASIC]]></category>
		<category><![CDATA[calculator programming]]></category>
		<category><![CDATA[TI-BASIC]]></category>

		<guid isPermaLink="false">http://tmac.andrewmin.com/blog/?p=27</guid>
		<description><![CDATA[The somewhat astute among us have noticed that in Round *, there was the following line of code: Input "Guess:",G And that I haven&#8217;t explained how that works. The pretty astute among us have also noticed that when they run a program such as the following: Input G All that&#8217;s shown is a question mark, [...]<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
]]></description>
			<content:encoded><![CDATA[<p>The somewhat astute among us have noticed that in Round *, there was the following line of code:</p>
<pre>Input "Guess:",G</pre>
<p>And that I haven&#8217;t explained how that works.</p>
<p>The pretty astute among us have also noticed that when they run a program such as the following:</p>
<pre>Input G</pre>
<p>All that&#8217;s shown is a question mark, and it&#8217;s not very visually appealing.</p>
<p>The quite astute among us have already realized that the way to get rid of the ugly question mark, and replace it with something more attractive is to put the attractive message in quotes, follow it with a comma, and then tack the variable name to the end of it.</p>
<p>Those who are not feeling very astute should be please that the astute have shared their thoughts, and we&#8217;re all back on a level playing field.</p>
<p>Finally, the EXTREMELY astute people are saying to themselves &#8220;It is <em>such</em> a drag when there&#8217;s like four variables that the user has to put in, and I have to make <em>four</em> Input statements. Four! Can you imagine? Surely there&#8217;s an easier way.&#8221;</p>
<p>Meet Input&#8217;s big brother &#8216;Prompt&#8217;. (TI-BASIC is very family-oriented. Output is Disp&#8217;s big brother, Input is Prompt&#8217;s little brother, Else is If&#8217;s big broth&#8211;oh, we haven&#8217;t gotten there yet. Round three is coming.)</p>
<p>Prompt is <em>great</em> for math. Take the Pythagorean Theorem:</p>
<pre>Prompt A,B
A^2+B^2-&gt;D
sqrt(D)-&gt;C
Disp C

Which will display:

A=?
B=?
(The solution, which depends on the input)</pre>
<p>So Prompt is just a quick and easy way of getting a bunch of variables inputted. Would I use it for games, or polished programs? No. Do I use it for quickly making a program that&#8217;ll do a mathematical formula? Always.</p>
<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Rannsaich%20mo%20Inntinn&amp;siteurl=http%3A%2F%2Fblog.tsmacdonald.com%2F&amp;linkname=TI-BASIC%20%28Round%20Two-and-a-Half%29%20%28More%20with%20Input%29&amp;linkurl=http%3A%2F%2Fblog.tsmacdonald.com%2Farchives%2F27" target="_blank"><img src="http://blog.tsmacdonald.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.tsmacdonald.com/archives/27/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TI-BASIC (Round *) (Special Bonus Edition)</title>
		<link>http://blog.tsmacdonald.com/archives/25</link>
		<comments>http://blog.tsmacdonald.com/archives/25#comments</comments>
		<pubDate>Sat, 27 Sep 2008 16:45:13 +0000</pubDate>
		<dc:creator>Timmy</dc:creator>
				<category><![CDATA[Multi-part Series]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[BASIC]]></category>
		<category><![CDATA[calculator programming]]></category>
		<category><![CDATA[TI-BASIC]]></category>

		<guid isPermaLink="false">http://tmac.andrewmin.com/blog/?p=25</guid>
		<description><![CDATA[I&#8217;ve been thinking, and I realized it&#8217;d be nice to add another goal to the TI-BASIC series. So I&#8217;m adding a sample program. About the program: It&#8217;s called &#8216;Guess!&#8217; (I am really creative), and it&#8217;s been around since I was in the fifth grade, in one form or another. It consists of guessing a number [...]<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been thinking, and I realized it&#8217;d be nice to add another goal to the TI-BASIC series. So I&#8217;m adding a sample program.</p>
<p><strong>About the program</strong>: It&#8217;s called &#8216;Guess!&#8217; (I am <em>really</em> creative), and it&#8217;s been around since I was in the fifth grade, in one form or another. It consists of guessing a number from 1-100, and being told if it&#8217;s too big or too small, and then guessing again, and being told again, etcetera, until you guess the number.</p>
<p><strong>Technical description:</strong> Before you make a program, it&#8217;s a really really really really really really really <em>really</em> good idea to know <em>exactly</em> what you want it to do. So here&#8217;s the &#8216;outline&#8217; (remember that # and everything after it is a comment):</p>
<p>Guess! (GPL 2008 T. Macdonald) #Title and license (GPL is sort of like a Copyright)</p>
<p>Guess: #User guesses a number</p>
<p>Too big! #If the number&#8217;s too big</p>
<p>Too small! #If it&#8217;s too small</p>
<p>You win! #If the user guesses it</p>
<p>And that&#8217;s it. Believe it or not, the minute it took to write that saved a <em>lot</em> of wasted coding.</p>
<p>So let&#8217;s implement what we already know:</p>
<pre>Disp "Guess!","","(GPL) 2008", "T. Macdonald"</pre>
<pre>#Note that going from the outline to the implementation there was a better idea--putting 'Guess!' and the GPL on separate lines.
Input "Guess:".G
#Oh no! We don't know how to make a random number!
#Oh no! We don't know how to say if the number is too big or too small!
#(That's why the tutorial's not done. Stay tuned.)</pre>
<p><!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 34641 -->
<script type="text/javascript">
<!--
var d=document;
d.projectwonderful_adbox_id = "34641";
d.projectwonderful_adbox_type = "1";
d.projectwonderful_foreground_color = "";
d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. --></p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Rannsaich%20mo%20Inntinn&amp;siteurl=http%3A%2F%2Fblog.tsmacdonald.com%2F&amp;linkname=TI-BASIC%20%28Round%20%2A%29%20%28Special%20Bonus%20Edition%29&amp;linkurl=http%3A%2F%2Fblog.tsmacdonald.com%2Farchives%2F25" target="_blank"><img src="http://blog.tsmacdonald.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.tsmacdonald.com/archives/25/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
