<?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>Make Tech Easier&#187; py3k</title>
	<atom:link href="http://maketecheasier.com/tag/py3k/feed" rel="self" type="application/rss+xml" />
	<link>http://maketecheasier.com</link>
	<description>Uncomplicating the complicated, making life easier</description>
	<lastBuildDate>Fri, 25 May 2012 23:58:46 +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>A Basic Introduction To Python 3</title>
		<link>http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25</link>
		<comments>http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25#comments</comments>
		<pubDate>Wed, 25 Mar 2009 00:11:46 +0000</pubDate>
		<dc:creator>Joshua Price</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[py3k]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripts]]></category>

		<guid isPermaLink="false">http://maketecheasier.com/?p=3485</guid>
		<description><![CDATA[A while back I was introduced to what has become my #1 favorite programming/scripting language: Python. It&#8217;s difficult for a language developer to come up with something that&#8217;s both powerful and easy to write, something that flows naturally and allows you to focus on what&#8217;s important in your code. Python... <p><div style="float:left;margin-bottom:10px"><a href="http://api.tweetmeme.com/share?url=http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25&amp;service=bit.ly" target="_blank"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25" style="border:none;margin-right:5px" width="51" height="61"></a><a href="http://digg.com/tools/diggthis/login?url=http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25" target="_blank"><img src="http://images.maketecheasier.com/diggme.png" style="border:none;margin-right:5px" ></a><a href="http://www.facebook.com/sharer.php?u=http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25" target="_blank"><img src="http://images.maketecheasier.com/fb.jpg" style="border:none;margin-right:5px" ></a><a href="http://www.google.com/reader/link?url=http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25&amp;title=A+Basic+Introduction+To+Python+3&amp;srcTitle=MakeTechEasier.com" target="_blank"><img src="http://images.maketecheasier.com/gbuzz-feed.png" style="border:none;margin-right:5px" ></a><a href="http://www.stumbleupon.com/submit?url=http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25" target="_blank"><img src="http://images.maketecheasier.com/stumble.png"></a></div>
<div style="clear:both"></div>
<strong><a href="http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25">A Basic Introduction To Python 3</a></strong> originally published on <a href="http://maketecheasier.com">Make Tech Easier</a> (<a href="http://feedproxy.google.com/MakeTechEasier">RSS</a>)
<br/>
Follow us at <a href="http://www.facebook.com/MakeTechEasier">Facebook</a> | <a href="http://twitter.com/MakeTechEasier">Twitter</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-thumbnail wp-image-3486" src="http://imagecdn.maketecheasier.com/2009/03/280px-python_logo-150x150.png" alt="Python Logo" width="150" height="150" />A while back I was introduced to what has become my #1 favorite programming/scripting language: Python.  It&#8217;s difficult for a language developer to come up with something that&#8217;s both powerful and easy to write, something that flows naturally and allows you to focus on what&#8217;s important in your code.  Python does this beautifully.  It makes just the right kinds of assumptions, allowing you to focus more on what your program should do instead of exactly how it should be done.  There&#8217;s no worrying about memory management, variable typing, or those blasted semicolons, you just keep your mind on the logic of your application.</p>
<p><span id="more-3485"></span>Python takes some pretty big departures from some of the more traditional languages like C/C++, fitting in better with the dynamic languages like Ruby, Smalltalk, and even Javascript.  The ability to quickly and easily finish complex tasks in Python has even been the subject of some great <a href="http://xkcd.com/353/" target="_blank">web</a> <a href="http://xkcd.com/413/" target="_blank">comics</a>.</p>
<blockquote><p>Note &#8211; Python is an interpreted language, the interpreter can be downloaded <a href="http://python.org" target="_blank">here</a>. All examples in this guide are written for Python 3.0 which is <strong>NOT</strong> fully backward-compatible with prior versions. If you&#8217;re having trouble running the examples, check your Python version.</p></blockquote>
<h3>Running the examples</h3>
<p>I&#8217;ll include various examples throughout this guide.  Once you&#8217;ve got the Python 3 interpreter installed (make sure it&#8217;s Python 3), code can be run in two ways:</p>
<h3>Script file</h3>
<p>You can copy/paste the complete text of the example code into a text file.  Python files typically end in .py.  Open your command prompt and run the Python executable followed by the name of the file.  On my Linux machine, I run:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">python3.0 myfile.<span style="color: black;">py</span></pre></div></div>

<p>This should be roughly the same on Windows and Mac as well, though you may have to specify the full path to the Python interpreter, such as</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">C:\Python30\python.<span style="color: black;">exe</span> myfile.<span style="color: black;">py</span></pre></div></div>

<h3>Interactive Interpreter</h3>
<p>Python can also be run in interactive mode, where you can type in commands one at a time to see how it responds.  This can be very useful in troubleshooting, or in trying out new things.  Run the Python executable on its own, without any script file, and it will open up the interactive prompt.</p>
<p><img class="aligncenter size-full wp-image-3735" src="http://imagecdn.maketecheasier.com/2009/03/interpreter.png" alt="Python interactive interpreter" width="515" height="278" /></p>
<p>Ctrl-D will exit the interpreter.</p>
<h3>Whitespace</h3>
<p>One of the most unusual aspects of Python is its use of whitespace to indicate blocks of code. Instead of <em>begin</em> and <em>end</em>, or grouping by brackets, Python uses the amount of indentation to tell how to handle blocks of code for looping and such. To many people coming from other programming languages, this seems like insanity. Once you get used to the idea, however, it becomes quite natural and forces your code to be clear and readable. We all indent code blocks anyway (or at least should) so it just makes sense for the language to understand that statements all lined up together are part of the same block.</p>
<p>As an added benefit, Python is not picky about just how much you like to indent, or even whether you prefer tabs or spaces. You can use a tab, a space, two spaces, 137 spaces, Python doesn&#8217;t care. All you need to do is be consistent. It will check your code and see &#8220;The first code block is indented by 4 spaces, so I&#8217;ll assume each other block is indented by another 4 spaces&#8221; or whatever the case may be. As long as you&#8217;re consistent in the way you indent your code, Python is flexible enough to understand. The following example may help clear things up.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">x = <span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">while</span> x <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">10</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
    x += <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;All done&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><strong>Code Breakdown: </strong>The <em>while</em> loop tells Python to run the following block of code as long as certain conditions are true. In this case, the condition is that x is less than 10. It will continue to loop over that block until x hits 10. The &#8220;x += 1&#8243; translates to &#8220;x = x + 1&#8243; or &#8220;make x larger by 1&#8243;. Notice the final line does not get run until the <em>while</em> loop is finished. Python sees the indented lines, and treats those as the group of code to be run on each trip through the <em>while</em> loop. The final line is not indented with the others, so Python does not act on it until the <em>while</em> loop is finished.</p>
<h3>Dynamic Typing</h3>
<p>Python does not require you to define what type of data will be in a variable.  You can put an integer, a string, a decimal, anything you want into a variable without having to tell Python what it is.  Python will figure out, based on what you assign, what type of data that variable should hold.  The following example should demonstrate:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">x = <span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;x is a: &quot;</span>,<span style="color: #008000;">type</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
x = <span style="color: #ff4500;">3.14</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;x is a: &quot;</span>,<span style="color: #008000;">type</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
x = <span style="color: #483d8b;">&quot;Hello&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;x is a: &quot;</span>,<span style="color: #008000;">type</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
x = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span>,<span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;x is a: &quot;</span>,<span style="color: #008000;">type</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Which gives us the output below</p>
<p><img class="aligncenter size-full wp-image-3643" src="http://imagecdn.maketecheasier.com/2009/03/type-example.png" alt="Example of Python's dynamic typing" width="236" height="141" /></p>
<h3>Data Structures</h3>
<p>The three data structures you&#8217;ll most commonly use in python are</p>
<ul>
<li>Lists</li>
<li>Tuples</li>
<li>Dictionaries</li>
</ul>
<h3>Lists</h3>
<p>are a lot like arrays in some other languages.  They&#8217;re a one-dimensional sequence of items (though technically you can give them as many dimensions as you like).  Each item in that list can be changed, moved, and removed at will without having to recreate the list, and without causing any change to the other items.  Lists can contain any Python object, whether it be a number, a string, even other lists.  The following code shows some basic usage of lists.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#create a list with a few sample items</span>
myList = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span>,<span style="color: #ff4500;">4</span>,<span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#len() gives a count of how many items our list holds</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;myList has &quot;</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>myList<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot; items.&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#The items in a list don't have to be of the same type</span>
myList.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;six&quot;</span><span style="color: black;">&#41;</span>
myList.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;seven&quot;</span><span style="color: black;">&#41;</span>
myList.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#We've appended three new items to the end of the list</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;myList now has&quot;</span>,<span style="color: #008000;">len</span><span style="color: black;">&#40;</span>myList<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot; items.&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Now let's see item number 0 (first item)</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;first item:&quot;</span>, myList<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#and now the fourth item</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;fourth item:&quot;</span>,mylist<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Then pop the last item out of the list</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;and ends with&quot;</span> , myList.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;myList now has&quot;</span>,<span style="color: #008000;">len</span><span style="color: black;">&#40;</span>myList<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot; items.&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#And see what we've done</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;The complete contents are:&quot;</span>, myList<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<h3>Tuples</h3>
<p>I won&#8217;t be covering tuples much, as they won&#8217;t be used it our example program and they&#8217;re similar to lists in many ways.  Tuples, like lists, are a series of items grouped together.  The difference is that tuples are not mutable.  You cannot make in-place changes to the items in a tuple, you must re-create the tuple.  This means no &#8220;append&#8221; or &#8220;pop&#8221; or other things that directly make changes to the contents of the tuple.  If it helps, you can think of tuples as a read-only list (though that&#8217;s really not very accurate).</p>
<h3>Dictionaries</h3>
<p>These, I love.  When I was first taught about dictionaries, I remember thinking something along the lines of &#8220;Well.. I GUESS that could be useful&#8230; sometimes&#8221;.  Within a week, I used them every chance I got.</p>
<p>In Python, dictionaries are key:value pairs.  It&#8217;s kinda like a list except that each item has two parts, a key and a value.  In the following example, I&#8217;m going to make a dictionary to hold information about myself.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">myDict = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#       Key             Value</span>
myDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Name&quot;</span><span style="color: black;">&#93;</span>       = <span style="color: #483d8b;">&quot;Josh&quot;</span>
myDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Occupation&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;Computer Geek&quot;</span>
myDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;FavFood&quot;</span><span style="color: black;">&#93;</span>    = <span style="color: #483d8b;">&quot;Hani Special (no lettuce)&quot;</span>
myDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;FavBand&quot;</span><span style="color: black;">&#93;</span>    = <span style="color: #483d8b;">&quot;Anything but the B-52s&quot;</span>
myDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Heroes&quot;</span><span style="color: black;">&#93;</span>     = <span style="color: #483d8b;">&quot;Tom Waits, Kurt Vonnegut, SpaceBat&quot;</span>
myDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;FavNumber&quot;</span><span style="color: black;">&#93;</span>  = <span style="color: #ff4500;">3.141592</span>
myDict<span style="color: black;">&#91;</span><span style="color: #ff4500;">42</span><span style="color: black;">&#93;</span>           = <span style="color: #483d8b;">&quot;It's also a good number&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;My name:&quot;</span>,            myDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Name&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;My heroes:&quot;</span>,          myDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Heroes&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;My favorite number:&quot;</span>, myDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;FavNumber&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;What do I think of 42?&quot;</span>, myDict<span style="color: black;">&#91;</span><span style="color: #ff4500;">42</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># Now I'll change my favorite number without creating a</span>
<span style="color: #808080; font-style: italic;"># whole new dictionary</span>
myDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;FavNumber&quot;</span><span style="color: black;">&#93;</span> += <span style="color: #ff4500;">100</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;My NEW favorite number:&quot;</span>,myDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;FavNumber&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>A few things should be clear from the example.  First, dictionaries can mix and match data of any type.  Your keys and values can be of anything.  You can even get really crazy and put things like functions inside dictionaries, but that&#8217;s way beyond the scope of this guide.</p>
<p>Second, dictionaries are mutable.  You can add and remove items on the fly without recreating the dictionary or affecting other items.</p>
<p>If you&#8217;re still not sure of the usefulness of dictionaries, consider using them to track a user&#8217;s settings.  You could have a dictionary called <em>settings</em> and store things like username, ip address, and screen resolution.  Any time you need to reference that data, you can just pull it from <em>settings["username"]</em>, or whatever other key you&#8217;ve specified.</p>
<h3>Bring it all home</h3>
<p>Now we&#8217;re going to get to the real action, creating a useful Python 3.0 program.  What this program will do is take a number representing money, and tell you how much pocket change would make that amount.  It&#8217;s a pretty common coding exercise and is a good way to demonstrate the concepts we&#8217;ve covered so far.</p>
<p>I should tell you now that this program is NOT going to be written in the &#8220;best&#8221; way, my aim is to write it using the most basic concepts and operations possible.  There are several &#8220;better&#8221; ways to write this, such using functions and the modulus operator, and including error checking, but this version should be pretty easy to understand.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#Get the cash amount</span>
total = <span style="color: #008000;">input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Enter cash amount in dollars:  $&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Reading from text input assumes text, so convert</span>
<span style="color: #808080; font-style: italic;">#to decimal and multiple by 100 so we can count pennies</span>
<span style="color: #808080; font-style: italic;">#(simpler math that way)</span>
pennies = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>total<span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">100</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#create dictionary to hold our change values</span>
change = <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;quarters&quot;</span> : <span style="color: #ff4500;">0</span>,
          <span style="color: #483d8b;">&quot;dimes&quot;</span>    : <span style="color: #ff4500;">0</span>,
          <span style="color: #483d8b;">&quot;nickels&quot;</span>  : <span style="color: #ff4500;">0</span>,
          <span style="color: #483d8b;">&quot;pennies&quot;</span>  : <span style="color: #ff4500;">0</span><span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Loop until all money has been accounted for</span>
<span style="color: #ff7700;font-weight:bold;">while</span> pennies <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:
  <span style="color: #808080; font-style: italic;">#Subtract each coin from the total, add 1 to count</span>
  <span style="color: #808080; font-style: italic;">#for each coin, and restart loop after counting</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> pennies <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">25</span>:
    change<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;quarters&quot;</span><span style="color: black;">&#93;</span> += <span style="color: #ff4500;">1</span>
    pennies -= <span style="color: #ff4500;">25</span>
    <span style="color: #ff7700;font-weight:bold;">continue</span>
  <span style="color: #ff7700;font-weight:bold;">elif</span> pennies <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">10</span>:
    change<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;dimes&quot;</span><span style="color: black;">&#93;</span> += <span style="color: #ff4500;">1</span>
    pennies -= <span style="color: #ff4500;">10</span>
    <span style="color: #ff7700;font-weight:bold;">continue</span>
  <span style="color: #ff7700;font-weight:bold;">elif</span> pennies <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">5</span>:
    change<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;nickels&quot;</span><span style="color: black;">&#93;</span> += <span style="color: #ff4500;">1</span>
    pennies -= <span style="color: #ff4500;">5</span>
    <span style="color: #ff7700;font-weight:bold;">continue</span>
  <span style="color: #ff7700;font-weight:bold;">else</span>:
    change<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;pennies&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>pennies<span style="color: black;">&#41;</span>
    pennies = <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Finally, print our results</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Q:&quot;</span>,change<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;quarters&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;D:&quot;</span>,change<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;dimes&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;N:&quot;</span>,change<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;nickels&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;P:&quot;</span>,change<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;pennies&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><strong>Code Breakdown:</strong> We&#8217;re using <em>input</em> to get an amount of money in from the command line.  Python assumes that what is being entered is a string of text, as opposed to a number, so we&#8217;ve got to tell it to convert that input into a usable number.  We could have left the number alone (ie 15.95) but instead we converted it to pennies (multiplying by 100) to make the maths simpler so we wouldn&#8217;t have to worry about decimal points.</p>
<p>Next, we create a dictionary to hold the results of our computing.  If this were a more complex program, we could pass that dictionary around to our functions, classes, etc without worrying about keeping track of separate variables for each type of coin.</p>
<p>After that comes the real work &#8211; the act of splitting our money total into individual coins.  This program uses a <em>while</em> loop to keep cycling until we have no money left from our original input.  Each trip through the loop looks at the amount of money, subtracts the largest coin it can, and restarts the loop.  Instead of doing subtractions over and over, this program would likely be much more efficient if we had used the modulus operator, but the subtraction is easier to understand.</p>
<p>Once we&#8217;re finished with the loop, all that&#8217;s left to do is display our results.</p>
<p>Python is capable of FAR more than I could possibly cover here, but I hope I&#8217;ve been able to demonstrate the basics of how it works, and how it can be used to quickly and easily create tools that would be much more complicated in a less intuitive language.</p>
<blockquote><p>In case anyone is as curious as I was about how to properly pronounce &#8220;tuple&#8221;, I was fortunate enough to have the opportunity to ask <a href="http://www.python.org/~guido/" target="_blank">Guido van Rossum</a> himself if it was &#8220;toople&#8221; or &#8220;tupple&#8221;, and he gave the somewhat unsatisfying answer &#8220;It&#8217;s whatever you want.&#8221;</p></blockquote>
<p><!--adsense#468x60--><br />
<h3 class='related_post_title'>Related Posts:</h3>
<ul class='related_post'>
<li><a href='http://maketecheasier.com/run-startup-scripts-in-kdm-before-kde-starts/2011/12/15' title='How to Run Startup Scripts in KDM before KDE Starts'>How to Run Startup Scripts in KDM before KDE Starts</a></li>
<li><a href='http://maketecheasier.com/write-linux-shell-scripts/2011/06/30' title='The Beginner Guide to Writing Linux Shell Scripts'>The Beginner Guide to Writing Linux Shell Scripts</a></li>
<li><a href='http://maketecheasier.com/easily-create-quicklist-for-ubuntu-unity-launcher/2011/06/06' title='How to Easily Create Quicklist For Ubuntu Unity Launcher'>How to Easily Create Quicklist For Ubuntu Unity Launcher</a></li>
<li><a href='http://maketecheasier.com/draw-arrows-in-gimp/2010/12/25' title='How to Draw Arrows in Gimp'>How to Draw Arrows in Gimp</a></li>
</ul>
<p><div style="float:left;margin-bottom:10px"><a href="http://api.tweetmeme.com/share?url=http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25&amp;service=bit.ly" target="_blank"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25" style="border:none;margin-right:5px" width="51" height="61"></a><a href="http://digg.com/tools/diggthis/login?url=http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25" target="_blank"><img src="http://images.maketecheasier.com/diggme.png" style="border:none;margin-right:5px" ></a><a href="http://www.facebook.com/sharer.php?u=http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25" target="_blank"><img src="http://images.maketecheasier.com/fb.jpg" style="border:none;margin-right:5px" ></a><a href="http://www.google.com/reader/link?url=http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25&amp;title=A+Basic+Introduction+To+Python+3&amp;srcTitle=MakeTechEasier.com" target="_blank"><img src="http://images.maketecheasier.com/gbuzz-feed.png" style="border:none;margin-right:5px" ></a><a href="http://www.stumbleupon.com/submit?url=http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25" target="_blank"><img src="http://images.maketecheasier.com/stumble.png"></a></div>
<div style="clear:both"></div>
<strong><a href="http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25">A Basic Introduction To Python 3</a></strong> originally published on <a href="http://maketecheasier.com">Make Tech Easier</a> (<a href="http://feedproxy.google.com/MakeTechEasier">RSS</a>)
<br/>
Follow us at <a href="http://www.facebook.com/MakeTechEasier">Facebook</a> | <a href="http://twitter.com/MakeTechEasier">Twitter</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maketecheasier.com/a-basic-introduction-to-python-3/2009/03/25/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic (Feed is rejected)
Page Caching using disk: enhanced
Content Delivery Network via Amazon Web Services: CloudFront: cdn.maketecheasier.com

Served from: maketecheasier.com @ 2012-05-25 20:37:11 -->
