<?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>The Maemo &#187; rm42</title>
	<atom:link href="http://www.themaemo.com/author/ruben/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.themaemo.com</link>
	<description>Maemo News , Hacks , Themes , Wallpapers and More</description>
	<lastBuildDate>Tue, 25 May 2010 10:36:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The Nightmare of Pre Ordering the N900 through Dell</title>
		<link>http://www.themaemo.com/dell-pre-order-nightmare/</link>
		<comments>http://www.themaemo.com/dell-pre-order-nightmare/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 17:20:31 +0000</pubDate>
		<dc:creator>rm42</dc:creator>
				<category><![CDATA[Devices]]></category>
		<category><![CDATA[Editorial]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Nokia N900]]></category>

		<guid isPermaLink="false">http://www.themaemo.com/?p=358</guid>
		<description><![CDATA[When Dell representatives were presented with this evidence they simply said that they would have to “investigate” the matter and that they would get back in touch soon.  They also tried to pass the blame saying that they simply send orders automatically to Nokia for fulfillment as soon as the purchase takes place.  According to them, it was Nokia that was holding back deliveries to those that had pre-ordered from Dell.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.themaemo.com%2Fdell-pre-order-nightmare%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.themaemo.com%2Fdell-pre-order-nightmare%2F" height="61" width="51" /></a></div>
<div id="attachment_362" class="wp-caption alignnone" style="width: 610px"><img src="http://www.themaemo.com/wp-content/uploads/2009/12/Dell_Nightmare.jpg" alt="Dell Pre Order Nightmare" title="Dell_Nightmare" width="600" height="289" class="size-full wp-image-362" />
<p class="wp-caption-text">Dell Pre Order Nightmare</p>
</div>
<p>Picture this scene.  It is a Saturday afternoon of one of the busiest holidays in the country.  The week before, you found some coupons for a very nice discount at your family&#8217;s favorite restaurant.  Since you know that it is going to be difficult to get a table in such a busy holiday, you decide to get there earlier than usual.  You gather your kids and your wife and arrive to the restaurant nice and early.</p>
<p>Unfortunately, in spite of your foresight, the place is already full when you get there.  The hostess informs you that there is a waiting list and she asks if you would like to be put on it. You are told that the wait is expected to be about forty five minutes. You decide to agree and proceed to wait.  After some thirty five minutes have passed, you observe that some newly arrived clients are taken immediately to a table instead of being put on the waiting list. Then, you observe this happen a few more times. You approach some of these clients to ask them if maybe they had reserved over the phone, but they tell you that they didn&#8217;t.  They simply arrived as usual and were taken to their tables without any trouble. You go to the hostess to ask for an explanation and you are simply told that they are sorry but all the tables are being used.  You ask, “well how much longer do we have to wait?”  She says, “well, it looks like its going to take at least another forty five minutes.”  By now all the other restaurants are packed.  If you leave, the wait somewhere else will likely be even worst.  How would you feel then?</p>
<p>Well, a similar nightmare scenario has happened to many customers of Dell.  On November 5th, Dell issued a coupon that could be used in the purchase of the <a href="http://www.themaemo.com/nokias-maemo-os-the-next-big-wave/">highly anticipated</a> <a href="http://www.themaemo.com/tag/n900/" target="_blank">Nokia N900</a> mobile device.  The word on the internet got out and it didn&#8217;t take long before Dell had to remove the item from the website.  Everyone that managed to place an order felt very fortunate.  The item was expected to ship in a little over a month, but everyone decided that it was worth the wait.  Besides, every one thought that the device would likely end up shipping before then, as it often happens.</p>
<p>Sure enough, around November 21st , word began spreading about some orders starting to ship.  Of course, everyone got very excited.  Like when they announce that you plane is starting to board.  People who received their devices began posting their experiences, and everyone else waited excitedly for theirs to arrive.  Unfortunately, the days began to pass without their orders being shipped.</p>
<p>And then, some customers began reporting that orders they had just placed where actually shipping too.  That is right, orders that were being placed weeks after the original customers had pre-ordered, started to get their devices shipped!  You can imagine the confusion that arose.  People began to call Dell to ask them what was going on.  They simply answered that the device was not in stock.  Customers began consulting with one another to try to figure out what was really going on.</p>
<blockquote style="font-weight: bold"><p>That is right, orders that were being placed weeks after the original customers  had pre-ordered, started to get their devices shipped!</p></blockquote>
<p>Several of the customers that got their devices without having pre-ordered them were happy to help the rest.  They provided their order dates and delivered dates, and some customers were even able to get a few of the late ordering order numbers so that they could be verified.  When Dell representatives were presented with this evidence they simply said that they would have to “investigate” the matter and that they would get back in touch soon.  They also tried to pass the blame saying that they simply send orders automatically to Nokia for fulfillment as soon as the purchase takes place.  According to them, it was Nokia that was holding back deliveries to those that had pre-ordered from Dell.  When asked if there was a way for the customers to contact Nokia in order to get to the bottom of this failure to honor order dates, the answer was that unfortunately that was not possible.  Then, all of the sudden, everyone that had ordered on November 5th saw a change in their online order page.  The expected delivery date was moved to January 20th, 2010.</p>
<p>I personally was one of those customers.  It was at that time that I decided to do something about this.  I contacted Dell and asked for an explanation.  I received the same canned response, they would have to “investigate” the matter, but nothing could be done.  Outraged, I sent an email to Dell telling them that I was considering placing a complaint with the Better Business Bureau.  This was their response:</p>
<blockquote><p>Monday, December 14, 2009, 6:03 PM </p>
<p>Sir,</p>
<p>All efforts has been made to have the item shipped out but due to highly constrained backlog and a very limited inventory into the distribution centers the distribution centers will ship to the customers within their respective regions only.</p>
<p>As much as we wanted to ship the item out soon but we really cannot guarantee you since it will depend on the availability of the item. Since you are in a hurry to get this item, the only option here is to cancel order and you can just buy one from a local store. Thank you.</p>
<p>Regards,</p>
<p>[***********]<br />
Dell Inc. | Americas Customer Care<br />
Work Days: Mon &#8211; Thurs<br />
Work Hours: 08:00a.m.-7:00p.m.CST </p></blockquote>
<p>To that, I replied:</p>
<blockquote><p>
Monday, December 14, 2009 7:25 PM</p>
<p>Dear Ms *********,</p>
<p>Thank you for your response. I do not want to cancel my order. You see, the issue is not that I am in a hurry to get the phone. What is upsetting is the fact so many people who ordered much latter than me got their phone. Look at this two orders for example:</p>
<table border=0>
<tr>
<td>OrderNumber</td>
<td>OrderDate</td>
<td>Status</td>
</tr>
<tr>
<td>999999999</td>
<td>11/19/2009</td>
<td>Shipped</td>
</tr>
<tr>
<td>999999999</td>
<td>11/25/2009</td>
<td>Shipped</td>
</tr>
</table>
<p>I will wait as long as I have to wait, but I am not happy about the situation. There is obviously something very wrong about the way the orders are being processed. My desire is for this situation to be rectified so that in the future other customers do not have to deal with this sort of issues. I thank you for continuing to try to resolve this situation and to rectify what caused it in the first place.</p>
<p>Sincerely,</p>
<p>[rm42] </p></blockquote>
<p>Several days went by with no word from Dell.  By now, I had put my self in contact with some of the customers that were in the same situation that I was and offered them to place a join-complain to the Better Business Bureau, thinking that maybe that way it would be taken more seriously (since then I have been informed that it is actually better to have each individual file his own complaint).  Several customers agreed and gave me their information.  I also contacted some of the users that claimed to have had their devices shipped without having to pre-order, and they too were willing to share their order numbers, order dates, and delivery dates with me.  Armed with that, I wrote another email to Dell.</p>
<blockquote><p>
Wednesday, December 16, 2009 11:50 PM<br />
Dear Ms E*********,</p>
<p>I take it that you have no news regarding the shipment of the N900 since I have not heard from you yet. That is OK. I am resigned to wait it out. However, would you not agree that I am entitled to some form of compensation for all the trouble and frustration that Dell&#8217;s mismanagement of my order has caused me? </p>
<p>Think of it this way. Imagine that you go out with your family to a restaurant for dinner. Upon arriving you are told that there is a waiting list and you are asked if you would like to be put on it. You are told that the wait is expected to be about half an hour. You decide to agree and proceed to wait. After 20 minutes have passed, you observe that some newly arrived clients are taken immediately to a table instead of being put on the waiting list. You observe this happen a few more times. You approach some of this clients to ask them if they had maybe reserved over the phone, but they tell you that they simply arrived and were taken to their tables without any trouble. You go to the receptionist to ask for an explanation and you are simply told that they are sorry but all the tables are being used. How would you feel then?</p>
<p>Well isn&#8217;t that what is happening in my and many others cases? Here is a list of people that are in the same situation that I am:</p>
<table border=0>
<tr>
<td>Name</td>
<td>OrderNumber</td>
<td>OrderDate</td>
</tr>
<tr>
<td>**** *****</td>
<td>999999999</td>
<td>11/5/2009</td>
</tr>
<tr>
<td>**** *****</td>
<td>999999999</td>
<td>11/5/2009</td>
</tr>
<tr>
<td>**** *****</td>
<td>999999999</td>
<td>11/5/2009</td>
</tr>
<tr>
<td>**** *****</td>
<td>999999999</td>
<td>11/5/2009</td>
</tr>
<tr>
<td>**** *****</td>
<td>999999999</td>
<td>11/5/2009</td>
</tr>
<tr>
<td>**** *****</td>
<td>999999999</td>
<td>11/5/2009</td>
</tr>
<tr>
<td>**** *****</td>
<td>999999999</td>
<td>11/5/2009</td>
</tr>
</table>
<p>And here are just some of the orders that were fulfilled with devices that should really have been shipped to one of us instead.</p>
<table border=0>
<tr>
<td>OrderNumber</td>
<td>OrderDate</td>
<td>Status (by 12/8/09)</td>
</tr>
<tr>
<td>999999999</td>
<td>11/26/09</td>
<td>Shipped</td>
</tr>
<tr>
<td>999999999</td>
<td>11/25/09</td>
<td>Shipped</td>
</tr>
<tr>
<td>999999999</td>
<td>11/19/09</td>
<td>Shipped</td>
</tr>
</table>
<p>So, tell me, how is Dell going to try to repair our relationship as customers?</p>
<p>Sincerely,</p>
<p>[rm42]</p></blockquote>
<p>As I write these words, I have not yet received any response from Dell.  Interestingly, one customer was able to get through to Nokia to have this matter addressed.  This is the email he received from Nokia:</p>
<blockquote><p>Thank you for e-mailing the Nokia Care Contact Center.</p>
<p>With regard to your inquiry about the delivery date, basically it is between you and Dell, since you placed the pre order with Dell and not with Nokia. We deliver the units to Dell and Dell delivers it in return to their customers. We will have no business delivering it to their customers, since it is not our customers. We will not know who placed the pre order with them, that is basically internal process with Dell. We suggest that you contact Dell, however it is evident that they point it to Nokia to avoid blame from customers, they might have preferred customers.</p>
<p>If you have any additional questions, please don’t hesitate to contact us again. To ensure proper handling of your case, kindly continue using the current subject line.</p>
<p>Thank you very much for your email. Have a great day!</p>
<p>Kind regards,<br />
Rosalie L.<br />
E-mail Specialist<br />
Nokia Inc. </p></blockquote>
<p>So now I ask you, what do you think is going to be Dell&#8217;s next move, if any?  What do you think their next move should be?  What would you do in our situation?<img src="http://www.themaemo.com/?ak_action=api_record_view&#038;id=358&#038;type=feed" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.themaemo.com/dell-pre-order-nightmare/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python for Newbies &#8211; Tutorial &#8211; Part Two</title>
		<link>http://www.themaemo.com/python-for-newbies-part-two/</link>
		<comments>http://www.themaemo.com/python-for-newbies-part-two/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 20:47:37 +0000</pubDate>
		<dc:creator>rm42</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Devices]]></category>
		<category><![CDATA[Editorial]]></category>
		<category><![CDATA[Nokia N900]]></category>
		<category><![CDATA[n900]]></category>
		<category><![CDATA[nokia]]></category>
		<category><![CDATA[nokia n900]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.themaemo.com/?p=214</guid>
		<description><![CDATA[This is part two of the Python for Newbies tutorial. Part One can be found here. The content of this tutorial is provided under the Creative Commons Attribution-Share Alike 3.0 license.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.themaemo.com%2Fpython-for-newbies-part-two%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.themaemo.com%2Fpython-for-newbies-part-two%2F" height="61" width="51" /></a></div>
<p>This is part two of the <a href="http://www.themaemo.com/python-for-newbies/" target="_blank">Python for Newbies tutorial</a> . Part One can be found <a href="http://www.themaemo.com/python-for-newbies/">here</a>. The content of this tutorial is provided under the Creative Commons Attribution-Share Alike 3.0 license:</p>
<p>http://creativecommons.org/licenses/by-sa/3.0/</p>
<p>http://creativecommons.org/licenses/by-sa/3.0/legalcode</p>
<p><a name="Functions"></a></p>
<h2>Functions</h2>
<p>Functions are simply a set of commands or statements grouped together as a unit with a name that can be called on by other parts of the program (or even by other programs) to accomplish a certain task. Since the statements inside a function do not have to be rewritten every time the steps they perform are needed, our programs are smaller and easier to maintain. We have already used several functions that are built in to the Python core. For example, we have used the del function to delete items from Python lists and dictionaries, the tuple function to convert a string into a tuple, several math functions, and others. However, we are now going to learn how to create our own functions. Functions are simple, but there are a few new associated concepts that you will have to understand.<span id="more-214"></span></p>
<p><a name="Defining a function"></a></p>
<h3>Defining a function</h3>
<p>Here is a brief example that we can examine:<br />
<code language="python"><br />
&gt;&gt;&gt; def average(num1, num2):<br />
</code><br />
<code language="python">...     avrg = (num1 + num2) / 2</code></p>
<p><code language="python">...     return avrg</code></p>
<p>This is a small little function that calculates the average between two numbers and returns the result Notice that, as all the compound statements that we have seen, a function definition has a header that ends with a colon. The header begins with the def statement, followed by the name of the function to be. After that, we have something that you should be able to recognize, a tuple, nothing else, nothing more. That tuple is used to contain the parameters of your function. What are parameters? Well, parameters are values that can be passed to your function (and that your function will expect when called) for it to do something with them. In Python, those parameters can be anything you want: a number, a string, a list, a dictionary, a file, etc. Parameters can have default values too. For example, we could define a function with a header like this:<br />
<code language="python"><br />
def personal_info(name, phone, country = 'USA'):<br />
</code><br />
The third parameter of this function has a default value, the string &#8216;USA&#8217;. That means that when calling this function the third parameter is optional. We could call the function like this:<br />
<span style="font-family: Courier New,monospace;"><br />
personal_info(Joe, 1234-5678)<br />
</span><br />
The function would accept the call and use &#8216;USA&#8217; as the value for the third parameter. Of course, we would also have the option of using a different third parameter, like this:<br />
<code language="python"><br />
personal_info(Bob, 987-6543, 'UK')<br />
</code><br />
So, defaults can come in very handy.</p>
<p>Notice that on the first function we defined above (average) the result was returned to the calling program with a specific statement, the <span style="font-family: Courier New,monospace;">return</span> statement. Why is that? Why couldn&#8217;t we just be satisfied with having the variable avrg as the container of the result and use it in other parts of the program? In one word: namespaces.</p>
<p><a name="Namespaces and Scope"></a></p>
<h3>Namespaces and Scope</h3>
<p>Python, as most programming languages, stores names defined inside a function in a separate place from where it stores other names. This different spaces are called, appropriately so, namespaces or scopes. You can think of namespaces as Dictionaries that contain names and its values as key-value pairs. Whenever a function is defined an associated namespace is created. For reasons beyond the scope of this tutorial (pun intended), in Python, names are only accessible to your code if they are defined in one of the following four scopes:</p>
<ul>
<li>The first one is called the Built-in scope. This scope is where all the core functions and constants of Python are defined. It is always available to you from anywhere in your code.</li>
<li>The second one is the Global scope. This is the namespace at the top level of a Python module. (You can see the Modules section if you want, or, for now, think of a module as a file with Python code.) In other words, any name defined outside a function or a class (we will cover classes latter as well) is in the Global scope. Names defined in this scope, like those in the built-in scope, are always available from anywhere in the module that contains it.</li>
<li>The third one is the local namespace. This is where names defined in a function reside. As mentioned before, names defined inside a function are only accessible from within that function. They are automatically destroyed once the function stops running.</li>
<li>The last namespace is called the nested scope. This namespace is defined as the namespace of the enclosing scope in which it was defined. That is, if inside a function you make reference to a name that Python does not find in the local namespace, it will then look to see if the name is defined in the enclosing namespace &#8211; the nested namespace.</li>
</ul>
<p>Here is an example of how the nested namespace would work:<br />
<span style="font-family: Courier New,monospace;"><br />
operation = 1     # Variable defined in the Global namespace. Always accessible.</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; def some_function(operation, num1, num2):</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     if operation == 1:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;         x=num1 &nbsp;&nbsp;&nbsp;&nbsp;#Variables defined in some_function&#8217;s local namespace.</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;         y=num2</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;         def multiply():</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;             print x * y &nbsp;&nbsp;&nbsp;&nbsp;    # x and y are not in the local namespace of multiply.</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;         multiply()<br />
</span><br />
Without nested scopes, this function would report an error, because x and y would not be in the local namespace of multiply, or in the global namespace, or in the built-in namespace. Of course, one could pass the value of x and y as parameters to the multiply function, but certain styles of programming (most specifically functional style programming) are much happier with access to nested scopes. (In other words you may never need to use nested scopes, but at least you now know they are there).</p>
<p>Since variable assignment inside a function takes place in the local namespace, one cannot reassign (change) the value of a Global variable inside a function. Let&#8217;s look at an example.<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; value= 1</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; def change_value():</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     value = 2</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     print value</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; change_value()</span></p>
<p><span style="font-family: Courier New,monospace;">2</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; print value</span></p>
<p><span style="font-family: Courier New,monospace;">1<br />
</span><br />
As you can see, when we assigned <span style="font-family: Courier New,monospace;">2</span> as the value of <span style="font-family: Courier New,monospace;">value</span> inside the <span style="font-family: Courier New,monospace;">change_value</span> function, the assignment took place only in the local namespace of the function. The Global variable <span style="font-family: Courier New,monospace;">value</span> remained unchanged. There is, however, a dirty little workaround that should probably be avoided as much as possible in truly efficient and well designed code. Some claim that global variables should only be changed by direct assignment at the global level, whether through direct assignment, or by the value returned from a function. (For example: <span style="font-family: Courier New,monospace;">value=change_value()</span>) In any case the workaround is the use of the <span style="font-family: Courier New,monospace;">global</span> statement. Let&#8217;s try it.<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; var = 1</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; def func(val):</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     global var</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     var =val*2</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; func(3)   </span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; print var   </span></p>
<p><span style="font-family: Courier New,monospace;">6<br />
</span><br />
So, thanks to the use of the global statement, our function successfully changed the value of a variable defined in the Global namespace. If <span style="font-family: Courier New,monospace;">var</span> did not exist before the function call, the function would simply create it.</p>
<p>To clarify the scope order, know that Python will look for names according the LNGB rule. It will first look for the name definition in the local namespace, if not found, it will then look for it in the nested scope, then in the global scope, and finally in the built-in scope. If not found, it will return an error.</p>
<p><a name="Modules"></a></p>
<h2>Modules</h2>
<p>We mentioned modules in the previous section. Basically, Python modules are files with Python code. Modules are the natural way to group related code. So far, all of the built-in Python functions we have seen are included as part of the core Python functionality. However, there are a lot more functions and capabilities included in a Python distribution. Most of them are included as separate modules that one has to &#8220;import&#8221; in order to use. Let&#8217;s look at an example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; import os</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; os.getcwd()</span></p>
<p><code language="python">'/home/me'<br />
</code><br />
The first line, above, uses the import statement to gain access to all the classes, functions and variables defined in a module called os. (As you can imagine the os module contains code related to the use of the operating system.) Notice that to access the getcwd function (get current working directory), all we had to do was type the name of the module, add a period, and type the name of the function. Let&#8217;s look at another function in this module:<br />
<code language="python"><br />
&gt;&gt;&gt; os.chdir('/home/me/MyDocs')</code></p>
<p><code language="python">&gt;&gt;&gt; os.getcwd()</code></p>
<p><code language="python">'/home/me/MyDocs'<br />
</code><br />
Here we used the chdir function to change our current working directory to &#8216;/home/me/MyDocs&#8217;. There are a couple of things to pay attention to here. First, notice that the chdir function expects a string as a parameter. That is why we used quotes to surround the path. Notice too that we used a forward slash rather that the backslash commonly used in windows. The reason for this is that in Python the backslash is a character with a special meaning &#8211; it is the &#8216;escape&#8217; character. Besides, in Linux, the OS of the N900, the forward slash is the standard path separator. (If you want your code to be portable between your N900 and Windows use os.sep instead of /.)</p>
<p>To remove a module that we have imported we again can use the handy del function:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; del(os)<br />
</span><br />
We could also import one or more specific items from a module. This is how we would do that:<br />
<code language="python"><br />
&gt;&gt;&gt; from os import listdir, getcwd</code></p>
<p><code language="python">&gt;&gt;&gt; listdir(getcwd())</code></p>
<p><code language="python">['somefile.txt', 'Dir', 'Dir2', 'File', 'File.txt']<br />
</code><br />
The listdir function is imported directly into the local scope. This means that we can use it directly without reference to the module it was originally in. This can be convenient sometimes, but is not considered a &#8216;best practice&#8217; way of doing things. Nevertheless, there are some modules designed specifically to be imported into the local space. For example some Graphical User Interface (GUI) related modules are like that.</p>
<p>Here is another example, one we made reference to earlier.<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; from __future__ import division</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; 7/2</span></p>
<p><span style="font-family: Courier New,monospace;">3.5<br />
</span><br />
Here you see how to make the N900&#8217;s version of Python handle division as Python 3.x does.</p>
<p>There are dozens of modules included in a Python distribution. This collection of modules is called Python&#8217;s Standard Library. It is a very good idea to become familiar with their names and what they are for, at least it is good to know what are the most commonly used ones. Probably the best way to do this is by looking at the documentation that is installed with Python. In particular, looking at the &#8220;<a href="http://www.python.org/doc/2.5/modindex.html" target="New">Global Module Index</a>&#8221; can be very useful. It contains links to detail explanations for each module of the Standard Python Library. There are also lots of third party developed modules and packages, such as the Windows extension modules that give access to all the win32 API, and to several Windows specific features. Others are the PyQt package and the PyGTK packages. These are the packages used for building GUI interfaces on the N900. One more that I you look at is the py2exe package. This one allows you to distribute your Python programs as executable files to users that do not have Python installed. Of course, you shouldn&#8217;t need that for your N900 programs.</p>
<p>Naturally, we can make our own modules simply by creating a file with Python code in it. When we import that module, all its code is executed, whether it is code that defines a function, or simple Python expressions. For example add the following commands into a regular text file, and name the file test.py:</p>
<p><code language="python">def greet(name):</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;print "Hello there, %s" %(name)</code></p>
<p><code language="python">greet('John')<br />
</code><br />
Now, in the Python interpreter, import your test module. Note that, since your module is likely not in the Python path, you may have to change your current working directory to the folder where you saved test.py so that Python can find it (The Python path is where Python searches, in addition to the current working directory, when trying to import a module.):<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; import test</span></p>
<p><span style="font-family: Courier New,monospace;">Hello there, John<br />
</span><br />
Two things happened when we imported our module: The <code language="python">greet</code> function was defined, and the <code language="python">greet('John')</code> function call executed. Now, we can continue to use the <code language="python">greet</code> function with other function calls. For example:<br />
<code language="python"><br />
&gt;&gt;&gt; test.greet('Marla')</code></p>
<p><code language="python">Hello there, Marla</code></p>
<p><code language="python">&gt;&gt;&gt; test.greet('Michael')</code></p>
<p><code language="python">Hello there, Michael<br />
</code><br />
Now, open a Terminal session and go to the directory that contains your module. Then, try running test.py as a script. For example:<br />
<code language="python"><br />
/home/m/MyDocs/Test&gt; python test.py</code></p>
<p><code language="python">Hello there, John<br />
</code><br />
The same two things happened. But, of course, since Python terminates when it is finished running the script, we can not continue using it to call the <code language="python">greet</code> function.</p>
<p>We can also make our scripts accept command line parameters so that it uses them at run time. For example, save this as test.py:<br />
<code language="python"><br />
import sys</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;for item in sys.argv:</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print item<br />
</code><br />
We could then run this script with a call like this:<br />
<code language="python"><br />
/home/m/MyDocs/Test&gt;python test.py First "Second" "And third"</code></p>
<p><code language="python">test.py</code></p>
<p><code language="python">First</code></p>
<p><code language="python">Second</code></p>
<p><code language="python">And third<br />
</code><br />
The parameters we give to our scripts when calling them are stored in a list called <span style="font-family: Courier New,monospace;">argv</span> accessible through the <span style="font-family: Courier New,monospace;">sys</span> module. The first parameter (<span style="font-family: Courier New,monospace;">argv[0]</span>) is the name of the script itself. So, in our example above, <span style="font-family: Courier New,monospace;">argv[1]</span> would be the parameter <span style="font-family: Courier New,monospace;">&#8220;First&#8221;</span>.</p>
<p>Generally, the conceptual purpose of a module is to store variables, function definitions and classes for use in other Python programs when needed. But, as you can see, there is no real difference between a module and a regular Python script. You can import any of your Python programs as a module and you can then use the functions and classes defined in them on any program where you need them. However, there is a right way to organize your code inside a module for this to be effective.  To illustrate this, lets take a look at another sample Python file, lets call it unames.py.<br />
<code language="python"><br />
import sys</code></p>
<p><code language="python">def full_name(first, second):</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;full = first + " " + second</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;return full</code></p>
<p><code language="python">def greet(name):</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;print "Hello there, %s" %(name)</code></p>
<p><code language="python">fname = full_name(sys.argv[1], sys.argv[2])</code></p>
<p><code language="python">greet(fname)<br />
</code><br />
What we have here is a couple function definitions, a function call that results in variable assignment, and another function call. Now, say that you are creating a new program and want to use those two functions. You could simply <code language="python">import unames</code> and access them. However, you would then have a new <code language="python">fname</code> variable and the <code language="python">greet(fname)</code> function call executed, and that may not be what you want. So, in order to make sure our Python modules are reusable, we need to have a way of keeping our function and class definitions separate from the calls and we need to have a way of avoiding those calls from executing when the module is imported. Fortunately, there is a very easy way to do this. Just reorganize the code like this:<br />
<code language="python"><br />
def full_name(first, second):</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;full = first + " " + second</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;return full</code></p>
<p><code language="python">def greet(name):</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;print "Hello there, %s" %(name)</code></p>
<p><code language="python">if __name__ =='__main__':</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;import sys</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;fname = full_name(sys.argv[1], sys.argv[2])</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;greet(fname)<br />
</code><br />
Notice that the import statement and the two function calls are only executed when a special condition is met &#8211; that the file is being run as a script. This allows you to place all your function and class definitions at the top of your modules and at the very end you can place any code that is to run when the file is run as a script, and that is not to run when the file is simply imported as a module.</p>
<p>One last thing to remember about modules is that if you are using a module and make changes to the code while you are using them in an interactive Python session, you will only be able to use the changes if you reload the module. To do this you have to either delete the module from Python&#8217;s memory (using the del function) and import it again, or use the reload function like this:<br />
<code language="python"><br />
&gt;&gt;&gt; reload(test)<br />
</code><br />
Now that we are talking about files, why don&#8217;t we take a look at how we work with files from Python code.</p>
<p><a name="Working with files"></a></p>
<h2>Working with files</h2>
<p>Working with files implies being able to open, read, and write files. Python has everything needed to do that and be able to process them with ease. In Python, files are a built in object type (like lists, dictionaries, etc). We create file objects using the open function. Depending on how we do this, we can either open an existing file or create a new one. Let&#8217;s first create a new file:<br />
<code language="python"><br />
&gt;&gt;&gt; f = open('MyFile.txt', 'w')<br />
</code><br />
At this point the variable <span style="font-family: Courier New,monospace;">f</span> represents a new file object that has been created in the file system at the current working directory. Notice that the <span style="font-family: Courier New,monospace;">open</span> function expects two parameters. The first parameter is the path and file name, provided as a string. The second parameter is also a string. It can be either <code language="python">'w'</code>, for write, or <code language="python">'r'</code>, for read. Let&#8217;s now write something into our file:<br />
<code language="python"><br />
&gt;&gt;&gt; f.write('This is a line inside the file.\nThis is another line.\n')</code></p>
<p><code language="python">&gt;&gt;&gt; f.write('This is the third line.')</code></p>
<p><code language="python">&gt;&gt;&gt; f.close()<br />
</code><br />
Now, if with a text editor we take a look at the file we created, the file should look like this:<br />
<code language="python"><br />
This is a line inside the file.</code></p>
<p><code language="python">This is another line.</code></p>
<p><code language="python">This is the third line.<br />
</code><br />
As you have probably figured out already, the <code language="python">'\n'</code> combination represents the &#8216;new line&#8217; character. The write method of the file object allows us to add strings to our files. And the close method simply closes it. Easy, right? Now what if you wanted to change something in the file we just created? Well, here is an example:<br />
<code language="python"><br />
&gt;&gt;&gt; f = open('MyFile.txt', 'r')</code></p>
<p><code language="python">&gt;&gt;&gt; file_contents = f.readlines()     #The readlines method returns a list of lines.</code></p>
<p><code language="python">&gt;&gt;&gt; f.close()</code></p>
<p><code language="python">&gt;&gt;&gt; file_contents     #Just to show you the contents of file_contents.</code></p>
<p><code language="python">['This is a line inside the file.\012', 'This is another line.\012', 'This is the third line.']</code></p>
<p><code language="python">&gt;&gt;&gt; f = open('MyFile.txt', 'w')     #Now we open it again to write our changes.</code></p>
<p><code language="python">&gt;&gt;&gt; for line in file_contents:</code></p>
<p><code language="python">...     new_line = line.replace("line", "set of words")</code></p>
<p><code language="python">...     f.write(new_line)</code></p>
<p><code language="python">...</code></p>
<p><code language="python">&gt;&gt;&gt; f.close()<br />
</code><br />
As you can see, to edit a file we first have to read its contents, close the file, and open it again to write. If we open an existing file with write mode, we really are just over-writing it with a new blank file of the same name. So, after executing the commands above, we have new file, in the same location, with the same name, and almost the same contents. The only difference is that we replaced the occurrences of &#8220;line&#8221; with &#8220;set of words&#8221;.</p>
<p>Those are the basics of file manipulation, and that is all we are going to cover. Now, through this tutorial you may have notice that we referred several times to something called classes and objects. Let&#8217;s take a very brief look at this subject.</p>
<p><a name="Classes and Objects"></a></p>
<h2>Classes and Objects</h2>
<p>There are several theories of what is the best way to program. One method of programming is called Object Oriented Programming (OOP). Python was designed from the ground up, with OOP concepts in mind. Yet, Python doesn&#8217;t force us to program this way. OOP in Python is optional. Stil, it may be a good thing to give you a glimpse of how this works.</p>
<p>We have already seen how we can save statements variables and functions inside a module, and then access them in our code by importing the module. Another way to do this is by using the class statement. Let&#8217;s look at an example:<br />
<code language="python"><br />
&gt;&gt;&gt; class Address:</code></p>
<p><code language="python">...     def setStreet(self, value):</code><br />
<code language="python">...         self.Street = value</code></p>
<p><code language="python">...     def setCity(self, value):</code><br />
<code language="python">...         self.City = value</code></p>
<p><code language="python">...     def display(self):</code><br />
<code language="python">...         print "The address is:"</code><br />
<code language="python">...         print self.Street</code><br />
<code language="python">...         print self.City<br />
</code><br />
As you can see we have defined the class Address. In it we have defined three methods (method is how a function inside a class is generally called). You probably notice that the first parameter for each of the methods is the word &#8220;self&#8221;. Why? Well, to understand that, you have to understand what classes are for. A class is defined so that we can produce objects based on them. You can think of the class definition as the DNA of its objects. We can create as many objects as we want from a class, and they will all be like little clones of each other. Each object we create is referred to as an instance object. Once an instance is created, it can start to have its own particular characteristics, different values, etc. Each object is now an independent &#8217;self&#8217;. So, the self we use when defining the methods inside a class refer to the particular instance the actions we are defining should apply to. If this is &#8220;all too wonderful&#8221; for you, don&#8217;t worry. Just remember that you have to use self as the first parameter of your method definitions. If you don&#8217;t, Python will kindly remind you with an error message when you try to use the method.</p>
<p>Let&#8217;s now create an instance (object) from our class:<br />
<code language="python"><br />
&gt;&gt;&gt; address = Address()<br />
</code><br />
That is all there is to it. At this point address is an instance object based on the clsAddress class. It can now start accepting its own personal data. Let&#8217;s give it some now:<br />
<code language="python"><br />
&gt;&gt;&gt; address.setStreet('157 Royal Road')</code></p>
<p><code language="python">&gt;&gt;&gt; address.setCity('NY')<br />
</code><br />
Notice that we have access to the methods defined in our class. Let&#8217;s display the contents of our object now:<br />
<code language="python"><br />
&gt;&gt;&gt; address.display()</code></p>
<p><code language="python">The address is:</code></p>
<p><code language="python">157 Royal Road</code></p>
<p><code language="python">NY<br />
</code><br />
You may remember, from our earlier analysis of scopes, that functions have their own local scope. Variables inside a function are destroyed as soon as the function stops running. The same is true of methods (since they are simply functions too). That is why when assigning a value to a variable inside a method, if we want it to be available latter, we qualify the variable with, again, the word self. This is as if we told Python to store the value of the variable in the global scope of our object. That&#8217;s right, objects have their own internal global namespaces. The value of variables defined this way can be retrieved by making direct reference to the particular object namespace like this:<br />
<code language="python"><br />
&gt;&gt;&gt; address.Street</code></p>
<p><code language="python">'157 Royal Road'</code></p>
<p><code language="python">&gt;&gt;&gt; address.City</code></p>
<p><code language="python">'NY'<br />
</code><br />
Of course, we have been using this feature all along in this tutorial, but now you should have a better idea of why. Here is another way to look at the contents of an object:<br />
<code language="python"><br />
&gt;&gt;&gt; dir(address)</code></p>
<p><code language="python">['Street', 'City']<br />
</code><br />
The dir function returns a list of all the names defined in the object. Finally, here is another little trick to help inspect objects:<br />
<code language="python"><br />
&gt;&gt;&gt; address.__dict__</code></p>
<p><code language="python">{'Street':'157 Royal Road', 'City':'NY'}<br />
</code><br />
Every object has to have a built-in location to store its instance names and values. That place is the __dict__ dictionary. And, yes, it is just a regular dictionary object itself.</p>
<p>So, just remember, in Python, everything is an object. Even class definitions are objects:<br />
<code language="python"><br />
&gt;&gt;&gt; Address.__dict__</code></p>
<p><code language="python">{'display': , 'setStreet': , '__doc__': None, 'setCity': , '__module__': '__main__'}<br />
</code><br />
There is much more we could talk about in this subject, but this is all we are going to cover. Time is precious, and we still have a few more things to cover. For example, in this section we made reference to the fact that Python has the capacity of displaying errors. Errors are simply unavoidable in programming. So, let&#8217;s take a quick look at them now.</p>
<p><a name="Errors and Error Handling"></a></p>
<h2>Errors and Error Handling</h2>
<p>What we commonly refer to as &#8216;errors&#8217; in programming are the accidental mistakes we make in our code that yield either the wrong result, or no result at all. Some programming languages leave it at that. They make no attempt at trying to explain why the program did not do what you expected. That is why people have developed special programs called &#8216;debuggers&#8217; to provide some help in finding where those errors occur. Thankfully, Python is very helpful in this regard. When you commit an error in programming that would result in a break of the flow of execution, Python displays messages to help you try to identify the type and location of the error. For example:<br />
<code language="python"><br />
&gt;&gt;&gt; print something</code></p>
<p><code language="python">Traceback (most recent call last):</code></p>
<p><code language="python">File "", line 1, in ?</code></p>
<p><code language="python">NameError: There is no variable named 'something'<br />
</code><br />
The error message, in this case, consists of three lines:</p>
<ol>
<li>The first line simply tells you the way the error message is organized (most recent call last). This is because some errors occur, for example, when your program makes a function call, that itself calls another function, that calls another certain function (where the error resides). Python will trace back the error, letting you know what the original call was, what the next call was, and what the final call (where the error occurred) was.</li>
<li>The second line tells you the file and line number of where the error was found. In this case, since we are using the Interactive Command Prompt, it refers to the file as stdin (standard input &#8211; right now, the keyboard input).</li>
<li>Finally, the last line tells us the type of error (<span style="font-family: Courier New,monospace;">NameError</span>), and what the error was.</li>
</ol>
<p>So, as you can see, it is very easy to track down your errors, most of the time. Of course, if the error in your program is found in its logic, rather than in its semantics, there is very little Python can do to help you.  There are some other packages designed for that.  For example, there is a module called <span style="font-family: Courier New,monospace;">unittest</span> that allows one to do something called &#8220;Unit Testing&#8221;. This is a very popular to check for errors in Python programmnig.  But, that is beyond the scope of this tutorial.</p>
<p>The errors that Python catches and displays, as we saw above, are really a subcategory of the larger category called exceptions. Some of the most common standard exceptions in Python are the following (there is a complete list in the manual):</p>
<table border="1">
<tbody>
<tr>
<th>Error Type</th>
<th>Description</th>
</tr>
<tr>
<td>IOError</td>
<td>generated when an I/O operation fails.</td>
</tr>
<tr>
<td>ImportError</td>
<td>generated when a module import fails.</td>
</tr>
<tr>
<td>IndexError</td>
<td>generated when an attempt is made to access a non-existent element index.</td>
</tr>
<tr>
<td>KeyError</td>
<td>generated when an attempt is made to access a non-existent dictionary key.</td>
</tr>
<tr>
<td>MemoryError</td>
<td>generated when an out-of-memory error occurs.</td>
</tr>
<tr>
<td>NameError</td>
<td>generated when an attempt is made to access a non-existent variable.</td>
</tr>
<tr>
<td>SyntaxError</td>
<td>generated when the interpreter finds a syntax error.</td>
</tr>
<tr>
<td>TypeError</td>
<td>generated when an attempt is made to run an operation on an incompatible object type.</td>
</tr>
<tr>
<td>ValueError</td>
<td>generated when function receives an argument that has an inappropriate value.</td>
</tr>
<tr>
<td>ZeroDivisionError</td>
<td>generated when an attempt is made to divide by zero.</td>
</tr>
</tbody>
</table>
<p>In Python exceptions can be intercepted quite easily. The way this is done is by the try/except/else construct. For example:<br />
<code language="python"><br />
&gt;&gt;&gt; try:</code></p>
<p><code language="python">...     print something</code></p>
<p><code language="python">... except NameError:</code></p>
<p><code language="python">...     print "You should have defined it first!"</code></p>
<p><code language="python">...</code></p>
<p><code language="python">You should have defined it first!<br />
</code><br />
As you can see, we were able to trap the error and tell Python what to do about it. We could exit the program at this time, but in this case we just printed a message. The flow of execution would continue outside of the try statement.</p>
<p>When we add an else block to a try construct, the code inside the else block is executed only if no exception is generated by the try construct. Here is an example:<br />
<code language="python"><br />
&gt;&gt;&gt; try:</code></p>
<p><code language="python">...     numb1 = int(raw_input("Type a number"))</code></p>
<p><code language="python">... except ValueError:</code></p>
<p><code language="python">...     print "You should have typed a number."</code></p>
<p><code language="python">... else:</code></p>
<p><code language="python">...     print "Good job %i is a number." %(numb1)<br />
</code><br />
The first line in the try statement tries to convert the input from the raw_input function into an integer number. It will only work if the input received is in the form of a number. Anything else will generate a ValueError, which we can dutifully intercept. If no error is found the else block is executed and the flow of the program continues outside the try statement.</p>
<p>A variation of the previous statement is the try/finally statement (no else allowed on this one). It consists of one try block followed by a finally block. The finally block always executes no matter what happens. So, this construct is useful when there is something that you want to make sure happens before the program exits. For example, you may want to make sure that some files are closed, or that some data is saved. If an exception is raised during the execution of the try statement, the flow of execution jumps to the finally block, and then it is returned the normal exception handling mechanism. For example, if we had something like this:<br />
<span style="font-family: Courier New,monospace;"><br />
try:</span></p>
<p><span style="font-family: Courier New,monospace;"> </span></p>
<p><span style="font-family: Courier New,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;try:</span></p>
<p><span style="font-family: Courier New,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#60;statements&#62;</span></p>
<p><span style="font-family: Courier New,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;finally:</span></p>
<p><span style="font-family: Courier New,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#60;statements&#62;</span></p>
<p><span style="font-family: Courier New,monospace;">except:</span></p>
<p><span style="font-family: Courier New,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&#60;statements&#62;<br />
</span><br />
In this example, if an exception is found in the try/finally statement, once the finally block is executed, the exception would be propagated to the try/except statement for it to be handled. If not trapped by the except statement, it would be propagated further up until it finally is handled by the Python&#8217;s standard exception handling mechanism, thus ending the program.</p>
<p>There is a lot more we could learn about errors or exceptions, but this at least gives you a glimpse into the basics. Let&#8217;s now try to gather a few miscellaneous tidbits that will be useful to you as write your own programs.</p>
<p><a name="Other common built-in functions"></a></p>
<h2>Other common built-in functions</h2>
<p>In this section we are going to cover a few functions that we did not cover before. These are some of the most commonly used built-in functions from the Python core.</p>
<p>Note:<br />
To see a list of all the built-in functions in the Python core use this command: <span style="font-family: Courier New,monospace;">dir(__builtins__)</span></p>
<p>Let&#8217;s first look at the len function. Its purpose is to tell us the length of a sequence. In other words, how many items are in it. This is how it works:<br />
<code language="python"><br />
&gt;&gt;&gt; L = [1, 2, 3, 4]</code></p>
<p><code language="python">&gt;&gt;&gt; len(L)</code></p>
<p><code language="python">4<br />
</code><br />
So, as you can imagine, the len function is very useful when dealing with sequences. For example, we could use it like this in a script:<br />
<code language="python"><br />
#Script to process a Social Security Number</code></p>
<p><code language="python">ssn = ""</code></p>
<p><code language="python">while not ssn:     #Empty strings evaluate to false.</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;ssn = raw_input("Enter your Social Security Number: ")</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;ssn = ssn.replace("-", "") #Eliminate the dashes, if present.</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;if len(ssn) != 9:     #Checks the number of digits.</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "This is an invalid number."</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Try again please."</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssn = "" &nbsp;&nbsp;&nbsp;&nbsp;#Set ssn to false</code></p>
<p><code language="python">print "Your Social Security Number is: " + ssn<br />
</code><br />
On this example, we created a while loop that continues to loop until a Social Security Number of nine digits is entered.</p>
<p>Another common function is the range function. This function is used to create a list of successively higher numbers. It&#8217;s most common use is in conjunction with for loops, allowing us to repeat a block of statements a certain number of times. It can take up to three parameters. Here is what it does:<br />
<code language="python"><br />
&gt;&gt;&gt; range(5)     #One parameter, returns list of numbers from 0 to n-1.</code></p>
<p><code language="python">[0, 1, 2, 3, 4]</code></p>
<p><code language="python">&gt;&gt;&gt; range(2, 5)     #First parameter is lower bound.</code></p>
<p><code language="python">[2, 3, 4]</code></p>
<p><code language="python">&gt;&gt;&gt; range(0, 10, 2)     #Third parameter provides a step.</code></p>
<p><code language="python">[0, 2, 4, 6, 8]<br />
</code><br />
To use this in a for loop you can do something like:<br />
<code language="python"><br />
def doit():</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;print "Done it."</code></p>
<p><code language="python">num=int(raw_input("How many times do you want to do this? "))</code></p>
<p><code language="python">for i in range(num):</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;doit()<br />
</code><br />
This little script will simply execute the doit function however many times the user requests. So, if we execute it from the command line, it will look something like:<br />
<code language="python"><br />
/home/me/MyDocs&gt; python test.py</code></p>
<p><code language="python">How many times do you want to do this? 4</code></p>
<p><code language="python">Done it.</code></p>
<p><code language="python">Done it.</code></p>
<p><code language="python">Done it.</code></p>
<p><code language="python">Done it.<br />
</code><br />
As we have seen already there are times in which Python will complain if a certain input it receives is not of the correct type. For example, the range function accepts only integers as input. The raw_input function returns its input as a string, that is why we use the int function to try to convert its output into an integer.</p>
<p>The last function we are going to look at is the type function. All it does is return the type of its input. For example:<br />
<code language="python">&gt;&gt;&gt; type(5)</code><br />
<code language="python">&#60;type 'int'&#62;<br />
</code></p>
<p><code language="python">&gt;&gt;&gt; type('5')</code><br />
<code language="python">&#60;type 'string'&#62;<br />
</code></p>
<p>So we could use it, for example when we want to make sure that a certain object we are about to do something to is of the correct type. For example, let&#8217;s say that we wanted to write a function that displays the methods available to a certain object. Let&#8217;s first create a class with one variable and one method:<br />
<code language="python"><br />
&gt;&gt;&gt; class Obj:</code></p>
<p><code language="python">...     def __init__(self):     #This is a special method that runs when instances are created.</code></p>
<p><code language="python">...         self.data = 1     #In this case it just initializes a variable to 1</code></p>
<p><code language="python">...     def add(self, num):     #This is a method that will be available to our instances.</code></p>
<p><code language="python">...          self.data = self.data + num<br />
</code><br />
Now, let&#8217;s create an instance object based on this class:<br />
<code language="python"><br />
&gt;&gt;&gt; ob = Obj()</code></p>
<p><code language="python">&gt;&gt;&gt; ob.data</code></p>
<p><code language="python">1</code></p>
<p><code language="python">&gt;&gt;&gt; ob.add(2)</code></p>
<p><code language="python">&gt;&gt;&gt; ob.data</code></p>
<p><code language="python">3</code></p>
<p><code language="python">&gt;&gt;&gt; dir(ob)</code></p>
<p><code language="python">['data']</code></p>
<p><code language="python">&gt;&gt;&gt; ob.__dict__</code></p>
<p><code language="python">{'data': 3}<br />
</code><br />
As you can see, objects that we create ourselves, as opposed to the built-in objects, do not automatically display the methods they have at their disposal. (There is a not so hard way to make them do that, but we are not going to get into that.) So, now let&#8217;s create a function that lets us know what methods are available to our homemade objects.<br />
<code language="python"><br />
import types</code></p>
<p><code language="python">def find_methods(instance):</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;clss = instance.__class__ &nbsp;&nbsp;#We find the class object of the instance</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;methods = []</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;for name in dir(clss):     #Get the named items from the class object</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attribute = clss.__dict__[name] #Get the value of items from the __dict__</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if type(attribute) == types.FunctionType:     #is it a function</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;methods.append(attribute)</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;return methods<br />
</code><br />
Now, we can use this function to find the methods in our little object:<br />
<code language="python"><br />
&gt;&gt;&gt; find_methods(ob)</code></p>
<p><code language="python">[&#60;function __init__ at 007CCD7C&#62; , &#60;function add at 007CC87C&#62;]<br />
</code><br />
Note:<br />
In Python, classes can inherit methods and variables from one or more classes too. This is called multiple inheritance. Our function would have to be a little different to be able to read functions inherited from other classes.</p>
<p>Well as we said before, time is precious. So, why don&#8217;t we finish this part of the tutorial looking at the time module.</p>
<p><a name="Time"></a></p>
<h2>Time</h2>
<p>Computers perform time calculations in reference to an &#8220;epoch&#8221; (like a pivot date). In the Unix family of operating systems the &#8220;epoch&#8221; is January 1, 1970, 0 hours, 0 seconds, UTC. Python follows that same method of handling time. The time module has several functions that we can use to handle time related tasks. In its most basic form, Python expresses the current time as the number of seconds since the &#8220;epoch&#8221;. Here is how you do it:<br />
<code language="python"><br />
&gt;&gt;&gt; import time</code></p>
<p><code language="python">&gt;&gt;&gt; time.time()</code></p>
<p><code language="python">1006015080.406<br />
</code><br />
Not very easy to read. However, other functions allow us to manipulate this output with ease. For example the localtime function.<br />
<code language="python"><br />
&gt;&gt;&gt; time.localtime(time.time())</code></p>
<p><code language="python">(2001, 11, 17, 11, 40, 37, 5, 321, 0)<br />
</code><br />
The localtime function returns a tuple representing: Year, month, day, hour, minute, second, weekday (0-6), Julian day (1-366), and the daylight savings time flag (-1 (force disable), 0 (default), or 1 (force enable)). There are also many other functions in this module that you should become familiar with. But, even with just this two functions we can do something useful. Let&#8217;s create a little timer class that we can use to check the time when a certain event in a program takes place and to time how long it takes our program to do its tasks. (Remember that you have to include an import time statement somewhere for this to work).<br />
<code language="python"><br />
class Timer:</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.start_time = time.time()</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;def cur_time(self):</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;now = time.time()</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tt=time.localtime(now)</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if tt[3] &gt; 12:</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hr = int(tt[3]) - 12</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ampm = 'p.m.'</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hr = int(tt[3])</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ampm = 'a.m.'</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ct = "%s:%s:%s %s %s/%s/%s" %(str(hr), tt[4], tt[5], ampm, tt[1], tt[2], tt[0])</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ct</code></p>
<p><code language="python">&nbsp;&nbsp;&nbsp;&nbsp;def elapsed(self):</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;now = time.time()</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minutes  =(now - self.start_time) / 60</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secs = (now - self.start_time) % 60     #The remainder of the division</code><br />
<code language="python">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 'Elapsed: %s min %s sec' % (int(minutes), int(secs))<br />
</code><br />
If we save that into a file called timer.py we can then import it into any of our programs. Let&#8217;s import it into the Python Command Line to play with it a little.<br />
<code language="python"><br />
&gt;&gt;&gt; import timer</code></p>
<p><code language="python">&gt;&gt;&gt; mytimer = timer.Timer()<br />
</code><br />
As soon as the instance is created it records the time in the start_time variable. We can verify that like this:<br />
<code language="python"><br />
&gt;&gt;&gt; mytimer.start_time</code></p>
<p><code language="python">1006016982.671<br />
</code><br />
Now let&#8217;s check the current time.<br />
<code language="python"><br />
&gt;&gt;&gt; mytimer.cur_time()</code></p>
<p><code language="python">'12:10:49 a.m. 11/17/2001'<br />
</code><br />
And finally, check how much time has elapsed since the instance was created.<br />
<code language="python"><br />
&gt;&gt;&gt; mytimer.elapsed()</code></p>
<p><code language="python">'Elapsed: 1 min 38 sec'<br />
</code><br />
Well, that is it for this section. It didn&#8217;t take too much time now did it? <img src='http://www.themaemo.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>We are going to wrap up this part of the tutorial here.  The remaining parts of the tutorial will be quite interesting.  Hopefully it won&#8217;t be too long before they are completed.  Come back and check in a little while.  And remember that if you would like to contribute your help to this tutorial you are very welcome.  Just leave us a note and tell us how to contact you.  Happy coding to all.<img src="http://www.themaemo.com/?ak_action=api_record_view&#038;id=214&#038;type=feed" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.themaemo.com/python-for-newbies-part-two/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Python for Newbies &#8211; Tutorial</title>
		<link>http://www.themaemo.com/python-for-newbies/</link>
		<comments>http://www.themaemo.com/python-for-newbies/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 16:23:24 +0000</pubDate>
		<dc:creator>rm42</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Devices]]></category>
		<category><![CDATA[Editorial]]></category>
		<category><![CDATA[Nokia N900]]></category>
		<category><![CDATA[maemo]]></category>
		<category><![CDATA[maemo5]]></category>
		<category><![CDATA[n900]]></category>
		<category><![CDATA[nokia n900]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.themaemo.com/?p=183</guid>
		<description><![CDATA[This is a Python tutorial that tries to touch on most areas of the Python language, but attempts to be very simple so that people who have never done any sort of programming can grasp it.  Special attention will be given to its use as a Maemo OS development platform.]]></description>
			<content:encoded><![CDATA[<a href='http://www.themaemo.com/python-for-newbies/' class='retweet vert' >Python for Newbies &#8211; Tutorial</a><div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.themaemo.com%2Fpython-for-newbies%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.themaemo.com%2Fpython-for-newbies%2F" height="61" width="51" /></a></div>
<p>This is a <a href="http://www.themaemo.com/tag/python/" target="_blank">Python</a> tutorial that tries to touch on most areas of the <a href="http://www.python.org">Python language</a>, but attempts to be very simple so that people who have never done any sort of programming can grasp it.  Special attention will be given to its use as a <a href="http://www.themaemo.com/tag/maemo/" target="_blank">Maemo OS </a>development platform.  The content of this tutorial is provided under the Creative Commons Attribution-Share Alike 3.0 license:</p>
<p><a href="http://creativecommons.org/licenses/by-sa/3.0/">http://creativecommons.org/licenses/by-sa/3.0/</a></p>
<p><a href="http://creativecommons.org/licenses/by-sa/3.0/legalcode">http://creativecommons.org/licenses/by-sa/3.0/legalcode</a></p>
<p>The versions of Python and other libraries targeted by the tutorial will be initially the ones available on <a href="http://www.themaemo.com/tag/maemo5/" target="_blank">Maemo 5</a> at the time of the <a href="http://www.themaemo.com/and-now-for-something-completely-different-the-n900-and-its-killer-feature/">N900</a> release. That means Python 2.5.4. As Maemo starts moving to more recent versions of Python we will attempt to keep this tutorial in sync.</p>
<p>If you have any other comments or suggestions please let me know.  We have a Google Wave available for those that would like to contribute to the content.</p>
<p>Due to its length, we have divided the tutorial in two parts (maybe three by the time we are done).  There are a few sections still missing at the end of the tutorial.  We hope to add them in the near future.  But, again, if someone would like to help, let us know.</p>
<h2>Table of Contents</h2>
<h3>Part One</h3>
<ul>
<li><a href="#What is Python">What is Python?</a></li>
<li><a href="#Why Python">Why Python?</a></li>
<li><a href="#Installing Python">Installing Python</a></li>
<li><a href="#How Python works">How Python works</a></li>
<li><a href="#The building blocks of a Python program">The building blocks of a Python program</a>
<ul>
<li><a href="#Variables">Variables</a></li>
<li><a href="#Expressions">Expressions</a></li>
<li><a href="#Numbers">Numbers</a></li>
<li><a href="#Strings">Strings</a></li>
<li><a href="#Lists">Lists</a></li>
<li><a href="#Tuples">Tuples</a></li>
<li><a href="#Dictionaries">Dictionaries</a></li>
</ul>
</li>
<li><a href="#How to program in Python">How to program in Python</a>
<ul>
<li><a href="#for Loops">&#8220;for&#8221; loops</a></li>
<li><a href="#truth Tests">&#8220;truth&#8221; tests</a></li>
<li><a href="#while Loops">&#8220;while&#8221; Loops</a></li>
<li><a href="#if Statements">&#8220;if&#8221; statements</a></li>
</ul>
</li>
<h3><a href="http://www.themaemo.com/python-for-newbies-part-two/">Part Two</a></h3>
<li><a href="http://www.themaemo.com/python-for-newbies-part-two/#Functions">Functions</a>
<ul>
<li><a href="http://www.themaemo.com/python-for-newbies-part-two/#Defining a function">Defining a function</a></li>
<li><a href="http://www.themaemo.com/python-for-newbies-part-two/#Namespaces and Scope">Namespaces and Scope</a></li>
</ul>
</li>
<li><a href="http://www.themaemo.com/python-for-newbies-part-two/#Modules">Modules</a></li>
<li><a href="http://www.themaemo.com/python-for-newbies-part-two/#Working with files">Working with files</a></li>
<li><a href="http://www.themaemo.com/python-for-newbies-part-two/#Classes and Objects">Classes and Objects</a></li>
<li><a href="http://www.themaemo.com/python-for-newbies-part-two/#Errors and Error Handling">Errors and Error Handling</a></li>
<li><a href="http://www.themaemo.com/python-for-newbies-part-two/#Other common built-in functions">Other common built-in functions</a></li>
<li><a href="http://www.themaemo.com/python-for-newbies-part-two/#Time">Time</a></li>
<h3><span id="more-183"></span></h3>
<h3>Part Three</h3>
<li>GUI &#8211; Qt development</li>
<li>GUI &#8211; GTK development</li>
<li>Maemo/N900 specific modules</li>
<li>Bibliography (attributions)</li>
</ul>
<p><a name="What is Python"></a></p>
<h2>What is Python?</h2>
<p>Python is a computer programming language created initially by Guido Van Rossum, who released it to the world as an “Open Source” “Free”program. It has since then been progressively improved by the contributions of hundreds of computer programmers around the world. The Python package can be downloaded gratis from the internet for every major computing platform in existence. It can be used and even modified for just about any purpose.</p>
<p><a name="Why Python"></a></p>
<h2>Why Python?</h2>
<p>There are many computer programming languages in existence. They all have different advantages and disadvantages. The brain of a computer is its processor. No processor understands English. The processor needs to receive input in a format that it understands. Most processors are made to understand commands formatted as a series of zeros “0” and ones “1”. This format is called binary. It is possible to write programs by hand using this format of zeros and ones, but it is very difficult, time consuming, and error prone. That is why computer programming languages have been invented.</p>
<p>Programming languages are frequently categorized as “low level” or “high level”. Low level languages were the first ones to appear. They simplified the process of writing instructions to the computer by allowing the use of normal words to be used in writing the program. A translator program, a “compiler”, translates the program’s instructions into a binary file that can then be understood by the processor. However, low level programming languages are still quite difficult to master. They force the programmer to understand the way the computer works internally. The programmer has to give instructions for every step that the computer must take to handle a certain task, which can easily lead to errors and serious problems. On the other hand, low level programs tend to be quite fast and efficient (if done correctly).</p>
<p>High level languages are designed to make it easier to program on. The instructions look more like normal language. And, a lot of the details are handled by the compiler. This makes for much shorter development time, but can also mean that the program is not as fast and efficient as a program would be if done in a lower level programming language.</p>
<p>Python is a very high level programming language. Many consider it the easiest programming language to learn and to program on. Many common tasks that would normally have to be programmed by the programmer are already built in to the language. Python is very useful as an easy and quick way to program anything from small little scripts to large scale client/server systems. This is especially true with today’s ultra fast computers that make up for the speed disadvantage inherent to high level programming languages. Programmers tend to like Python because of its clean and logical syntax, which shortens development cycles tremendously. It also forces programmers to use code indentation correctly. At first, some programmers see this as a challenge and unnecessary restriction. However, not long after beginning to use it, most programmers realize that it is easy to adjust to it and that it results in very clean code which is easily maintained (even if the code was written by someone else).</p>
<p><a name="Installing Python"></a></p>
<h2>Installing Python</h2>
<p>To install Python on the <a href="http://www.themaemo.com/tag/n900/" target="_blank">N900</a>, just install an aplication that uses Python. For example, pyGTKeditor and/or (any one knows of a good PyQt app for Maemo?).</p>
<p>If you are using Linux, Python should already be installed. On Windows, just go to www.python.org/download/ and get yourself a download of Python 2.6.4, not the latest 3.x, since Python 3.x code is not compatible with the one on the N900. (It is really not too different, but different enough to break things.)</p>
<p><a name="How Python works"></a></p>
<h2>How Python works</h2>
<p>Python programs run with the aid of an “interpreter”. The interpreter is a program that converts the code into the binary instructions needed by the processor, at run time. There is an interpreter for each different platform where Python runs. This allows Python code written in a Windows machine, to run unchanged, in a Linux machine, a Macintosh machine, or even a smart phone. Of course, Python also has a lot of “platform specific” features that are optimized for the platform for which they are written. (Avoid these if you don’t want to tie your program down to a specific platform.) The interpreter must, either be installed on the computer that needs to run the Python program, or the Python program must be packaged in conjunction with the interpreter so that it can be run by computers without Python.</p>
<p>The interpreter can be run directly in what is called the “Interactive Command Line”. Python commands can be typed directly into this session and they are processed immediately showing the results of the command. This unique feature allows for quick and easy testing of ideas and code.</p>
<p>Most Python programs, however, are written with some type of text editor, saved as plain text files with a .py extension, and run as an independent process. Since there is no need to “compile” a Python program before running it, a programmer can test its changes to the code immediately after saving them.</p>
<p><a name="The building blocks of a Python program"></a></p>
<h2>The building blocks of a Python program</h2>
<p>First of all, what is a “program”? Well, “a program is a sequence of instructions that specifies how to perform a computation.” *i</p>
<p>Enter Python&#8217;s interactive mode ( On the N900, open the Terminal application and enter the following command: python), and let&#8217;s do our first program as it is traditionally done:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; print &#34;Hello World&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">Hello World<br />
</span><br />
Here, the word “<span style="font-family: Courier New,monospace;">print</span>” is a built in command or &#8220;statement&#8221; that sends the given argument to the screen. The second part of the program, what follows the print statement, is the argument itself, in this case, the string “<span style="font-family: Courier New,monospace;">Hello World</span>”. The quote marks tell Python to treat the words inside of them as a string, rather than as a command or a variable. (A &#8220;string&#8221; is a sequence of characters, like a word, a sentence, etc. They are further explained latter in this section.)</p>
<p>Note:<br />
Typically, in Python manuals and tutorials, when showing python code entered in an interactive session the &gt;&gt;&gt; represents the Python Command Line prompt. When a compound statement is being defined (one that takes more than one line), Python switches to &#8216;&#8230;&#8217; as a prompt.</p>
<p>Note:<br />
Text typed after a # is referred to as a comment, and is ignored by the Python interpreter.</p>
<p><a name="Variables"></a></p>
<h3>Variables</h3>
<p>What is a variable? A variable is a name (a word) that refers to a certain value. We can use almost any word and assign a value to it. For example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; hi = &#34;Hello World&#34;<br />
</span><br />
The word <span style="font-family: Courier New,monospace;">hi</span> is now the name of a variable who’s value is the string <span style="font-family: Courier New,monospace;">&#34;Hello World&#34;</span>. Let&#8217;s use the <span style="font-family: Courier New,monospace;">print</span> statement to send the value of <span style="font-family: Courier New,monospace;">hi</span> to the screen:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; print hi</span></p>
<p><span style="font-family: Courier New,monospace;">Hello World<br />
</span><br />
In Python, variables are very flexible. You don’t have to declare them first, like in other languages. You can assign any value to them, even if they already have a value of a different type. For example, let&#8217;s change the value of <span style="font-family: Courier New,monospace;">hi</span> to <span style="font-family: Courier New,monospace;">3.0</span>:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; hi = 3.0</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; print hi</span></p>
<p><span style="font-family: Courier New,monospace;">3.0<br />
</span><br />
We can also assign a variable as the value of a variable. For example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; num = hi</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; print num</span></p>
<p><span style="font-family: Courier New,monospace;">3.0<br />
</span><br />
Another neat feature of Python’s interactive shell is that you don’t even have to use the print statement to see the values of variables and other objects. You can simply type their name and Python will display it.<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; num</span></p>
<p><span style="font-family: Courier New,monospace;">3.0<br />
</span><br />
<a name="Expressions"></a></p>
<h3>Expressions</h3>
<p>At the interactive command prompt one can type expressions and see their result interactively. “An expression is a combination of variables, operators and values that represents a single result value”i. For example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; num + 2</span></p>
<p><span style="font-family: Courier New,monospace;">5.0</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; ((5 &#8211; 1) + num) * 2</span></p>
<p><span style="font-family: Courier New,monospace;">14.0<br />
</span><br />
We can assign the value of an expression to a variable. Such as:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; numb = ((5 &#8211; 1) + num) * 2</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; numb</span></p>
<p><span style="font-family: Courier New,monospace;">14.0<br />
</span><br />
<a name="Numbers"></a></p>
<h3>Numbers</h3>
<p>Notice on this last example that the number returned by Python is a number with a decimal point. In computer terminology this is called a floating point number. Since the expression included a floating point number (3.0), the expression’s value was automatically converted into a floating point number. The reason for the distinction in number types is beyond the scope of this tutorial. In most computer programming languages, one has to be careful to use the right type of number for each application. In Python, it is good to know that there are different types of numbers, but Python will usually deal with the details quite well. The only caveat to currently watch out for is division. Notice this:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; 7 / 2</span></p>
<p><span style="font-family: Courier New,monospace;">3<br />
</span><br />
Since both numbers in the division are integers (a type of number without decimal places) Python performs an integer division. This may not be what someone new to programming expects. Currently, to work around this one can do things like this:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; 7.0 / 2     # or float(7) / 2</span></p>
<p><span style="font-family: Courier New,monospace;">3.5<br />
</span><br />
In more recent versions of Python (version 3.x), Python&#8217;s designers have decided to change the way this works so that it does what new users would expect. With the current version of Python in Maemo 5 one can already import this behavior from the &#8220;<span style="font-family: Courier New,monospace;">__future__</span>&#8221; module. We will see more about &#8216;importing&#8217; latter.</p>
<p>Some common operators associated with numbers are:</p>
<table border="1">
<tbody>
<tr>
<th>Operator</th>
<th>Description</th>
</tr>
<tr>
<td>+</td>
<td>Addition</td>
</tr>
<tr>
<td>-</td>
<td>Subtraction</td>
</tr>
<tr>
<td>*</td>
<td>Multiplication</td>
</tr>
<tr>
<td>/</td>
<td>Division</td>
</tr>
<tr>
<td>%</td>
<td>Remainder (e.g. 9%2=1; 8%2=0)</td>
</tr>
<tr>
<td>**</td>
<td>Power (4**2=16)</td>
</tr>
</tbody>
</table>
<p>There are also many math functions in Python for doing just about any operation needed. Here is a small sample (see the documentation for more). To access them you need to first import them from the math module, like this for example:<br />
<span style="font-family: Courier New,monospace;"><br />
from math import acos<br />
</span></p>
<table border="1">
<tbody>
<tr>
<th>Function</th>
<th>Description</th>
</tr>
<tr>
<td>acos(x)</td>
<td>Return the arc cosine of x</td>
</tr>
<tr>
<td>cos(x)</td>
<td>Return the cosine of x</td>
</tr>
<tr>
<td>log(x)</td>
<td>Return the natural logarithm of x</td>
</tr>
<tr>
<td>sqrt(x)</td>
<td>Return the square root of x</td>
</tr>
<tr>
<td>tanh(x)</td>
<td>Return the hyperbolic tangent of x</td>
</tr>
</tbody>
</table>
<p>A handy tip is that in the Interactive Command Prompt you can recall the last printed expression with the underscore character “_”. So for example, you can use Python as a handy calculator this way:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; 2 + 2</span></p>
<p><span style="font-family: Courier New,monospace;">4</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; _ + 6</span></p>
<p><span style="font-family: Courier New,monospace;">10<br />
</span><br />
There is also a special Python extension called Numerical Python (NumPy) which allows it to become a tool for higher math functions such as linear algebra and matrices. But, enough of numbers for now.</p>
<p><a name="Strings"></a></p>
<h3>Strings</h3>
<p>We already saw an example of a string in the “Hello World” example. “A string is an ordered collection of characters, used to store and represent text-based information”. Python strings can be enclosed in double, single, or even triple (for multi-line strings) quotes. For example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; &#39;Hello World&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;Hello World&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; &#34;Hello &#39;Hello World&#39;&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">&#34;Hello &#39;Hello World&#39;&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; &#39;&#39;&#39;This is a longer string that spans</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230; several lines, doesn&#8217;t say anything</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230; meaningful, and ends here&#39;&#39;&#39;<br />
</span><br />
The fact that strings are “ordered” means that we can access their components by position. Each character in a string has an index position before it. For example the string “Python” has the following indexes:</p>
<div id="attachment_197" class="wp-caption alignnone" style="width: 559px"><img class="size-full wp-image-197" title="Ordered Strings" src="http://www.themaemo.com/wp-content/uploads/2009/12/Ordered-Strings.png" alt="String Indexes" width="549" height="98" />
<p class="wp-caption-text">String Indexes</p>
</div>
<p>Notice that the first character “P” is located at index 0, as in programming languages like C, not 1 as in languages like Basic. However, unlike C, with Python you can also use negative indexes, which start counting from the end of the string.</p>
<p>To get the characters located at certain indexes we can use their index number inside brackets. Let&#8217;s look at some examples.</p>
<p>To get the character located at the beginning of the string we can use:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; &#34;Python&#34;[0]</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;P&#39;<br />
</span><br />
Or if we are using a variable:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; hw = &#34;Hello World&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; hw[0]</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;H&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; hw[1]</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;e&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; hw[-1]</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;d&#39;<br />
</span><br />
We can also get “slices” of a string by specifying the first and last index of the slice separated by a colon.<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; hw[1:7]</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;ello W&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; hw[4:-2]</span></p>
<p><span style="font-family: Courier New,monospace;">&#8216;o Wor&#8217;<br />
</span><br />
The left or right bounds of the string can be specified in a slice by leaving the index blank. For example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; hw[:5]</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;Hello&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; hw[6:]</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;World&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; hw[:]</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;Hello World&#39;<br />
</span><br />
Strings are very smart “objects”. They know how to behave correctly in many situations such as:</p>
<p>When adding a string to a string<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; hw + &#34; Today&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;Hello World Today&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; hw[:5] + &#34; You&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;Hello You&#39;<br />
</span><br />
Multiplication<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; hw[:6] * 5</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;Hello Hello Hello Hello Hello &#39;<br />
</span><br />
Notice that in all the operations we have done so far with strings, the operation returns a result but the original string remains unchanged. That is how strings work. A string is what is called an “immutable sequence”. What this means is that to change a string we actually have to create a new string. For example:</p>
<p>Trying to change the value of a string character in place<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; hw[0] = &#34;M&#34;<br />
</span><br />
Raises an error!</p>
<p>However, since variables are so flexible we can simply assign a new value to it:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; hw= &#34;M&#34; + hw[1:]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; hw</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;Mello World&#39;<br />
</span><br />
This works because the hw[1:] expression is calculated first, then “M” is added to it, and finally the result is assigned as the value of the variable hw.</p>
<p>Another way to join strings with variables is by means of string formatting. For example, what if you had a program that greets the user by name according to the time of day. For example, it could say something like: “Hi Joe. How are you this morning?”. In this case the program could gather the name of the user somehow and place it in a variable called “name”. It could also fetch the time of the day by looking at the computers clock, determine if it is morning, afternoon, or evening, and place the result in a variable called “time_of_day”. You could then have the following line in your program to merge them into a string:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; Greeting = &#34;Hi %s. How are you this %s&#34; % (name, time_of_day)<br />
</span><br />
Python replaces the occurrences %s with the respective variable in the tuple after the % character. (A &#8220;tuple&#8221; is a sequence of items inside a parentheses. Tuples are further explained latter in this book.) Here is another example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; dessert = &#34;Pecan Pie&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; max_per_person = 1</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; answer = &#34;The dessert of the day is %s. You can only take %i per person&#34; % (dessert, max_per_person)</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; answer</span></p>
<p><span style="font-family: Courier New,monospace;">&#34;The dessert of the day is Pecan Pie. You can only take 1 per person&#34;<br />
</span><br />
In this example we added a string variable and an integer variable into the <span style="font-family: Courier New,monospace;">answer</span> string. Strings and integers have different formatting codes (%s, %i). See Python&#8217;s documentation for other formatting codes.</p>
<p>Strings also have built-in methods such as the following:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; &#34;hello&#34;.capitalize()</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;Hello&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; &#34;HELLO&#34;.lower()</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;hello&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; &#39;hello&#39;.upper()</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;HELLO&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; &#39;Hello World&#39;.replace(&#34; &#34;, &#34;-&#34;)     #Replace occurrences of first parameter with second</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;Hello-World&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; &#39; Hello World &#39;.lstrip()     #To strip the whitespace at the left of string</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;Hello World &#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; &#39; Hello World &#39;.rstrip()     #To strip the whitespace at the right of string</span></p>
<p><span style="font-family: Courier New,monospace;">&#39; Hello World&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; &#39; Hello World &#39;.strip()     #To strip the whitespace at the left and right</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;Hello World&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; &#34;This is a longer string&#34;.split()</span></p>
<p><span style="font-family: Courier New,monospace;">[&#39;This&#39;, &#39;is&#39;, &#39;a&#39;, &#39;longer&#39;, &#39;string&#39;]<br />
</span><br />
On the last example we see how the split method returns a list of strings. It separates the original string by some given delimiter. When none is given, the default, whitespace, is used. But, what is a list?</p>
<p><a name="Lists"></a></p>
<h3>Lists</h3>
<p>Like strings, lists are ordered sequences. So, they share many of the same functionality. However, lists are mutable sequences, that is we can change them in place. Also, unlike strings, lists can contain any type of object: numbers, strings, even other lists. Much of the simplicity, power, and popularity of Python comes from this built in object type.</p>
<p>To define a list, we simply have to enclose it in square brackets using commas as item delimiters:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; L = [1, 2,&#39;three&#39;, 4]<br />
</span><br />
The L variable is now a list object. We can use indexes and slices to access its members:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; L[0]</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;1&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L[2:]</span></p>
<p><span style="font-family: Courier New,monospace;">[&#39;three&#39;, 4]<br />
</span><br />
And, as mentioned before, we can change them in place:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; L[2] = 3</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L</span></p>
<p><span style="font-family: Courier New,monospace;">[1, 2, 3, 4]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L[0] = [&#39;one&#39;, &#39;two&#39;, &#39;three&#39;]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L</span></p>
<p><span style="font-family: Courier New,monospace;">[[&#39;one&#39;, &#39;two&#39;, &#39;three&#39;], 2, 3, 4]<br />
</span><br />
We can also concatenate lists (add them together):<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; L + [8, 9]</span></p>
<p><span style="font-family: Courier New,monospace;">[[&#39;one&#39;, &#39;two&#39;, &#39;three&#39;], 2, 3, 4, 8, 9]<br />
</span><br />
And repeat or multiply:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; [1, 2, 3] * 3</span></p>
<p><span style="font-family: Courier New,monospace;">[1, 2, 3, 1, 2, 3, 1, 2, 3]<br />
</span><br />
We can delete items from lists using the del function:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; L = [[&#39;one&#39;, &#39;two&#39;, &#39;three&#39;], 2, 3, 4]</span></p>
<p><span style="font-family: Courier New,monospace;">del L[1]</span></p>
<p><span style="font-family: Courier New,monospace;">L</span></p>
<p><span style="font-family: Courier New,monospace;">[[&#39;one&#39;, &#39;two&#39;, &#39;three&#39;], 3, 4]<br />
</span><br />
Or even:<br />
<span style="font-family: Courier New,monospace;"><br />
del L[0][1]     #Deletes item at index 1 of L[0]</span></p>
<p><span style="font-family: Courier New,monospace;">L</span></p>
<p><span style="font-family: Courier New,monospace;">[[&#39;one&#39;, &#39;three&#39;], 3, 4]<br />
</span><br />
Lists can be sorted and reverse sorted:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; L = [&#39;c&#39;, &#39;b&#39;, &#39;a&#39;, 1]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L.sort()</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L</span></p>
<p><span style="font-family: Courier New,monospace;">[1, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L.reverse()</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L</span></p>
<p><span style="font-family: Courier New,monospace;">[&#39;c&#39;, &#39;b&#39;, &#39;a&#39;, 1]<br />
</span><br />
Notice that the sort and reverse methods do not return a new sorted list as a result. They simply change the list in place and return a special value called None. To see the results of the sort we have to print the list.</p>
<p>Another little peculiarity to watch out for when using lists is the following:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; L1 = [1, 2, 3]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L2 = L1</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L2</span></p>
<p><span style="font-family: Courier New,monospace;">[1, 2, 3]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L2[0] = 8</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L2</span></p>
<p><span style="font-family: Courier New,monospace;">[8, 2, 3]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; L1</span></p>
<p><span style="font-family: Courier New,monospace;">[8, 2, 3]<br />
</span><br />
Notice that changing an item in L2 also changed it in L1. The reason for this is that when we assign L1 as the value of L2 what Python actually does is assign a pointer to L1 as the value of L2. In other words, they are linked. If this is not the desired result, you can avoid it by doing the following:<br />
<span style="font-family: Courier New,monospace;"><br />
L2 = L1[:]<br />
</span><br />
Because slices return new lists, assigning a slice of all the items of L1 as the value of L2 results in L2 being a new, independent list, a clone of L1.</p>
<p>So as you can see lists are very easy and friendly to use. In Python lists have a very close relative. Let&#8217;s take a look at it now.</p>
<p><a name="Tuples"></a></p>
<h3>Tuples</h3>
<p>Tuples are very similar to lists. The difference (and the reason for their existence) is that tuples are immutable. In other words, they can not be changed in place. Other than that everything we saw about lists also applies to tuples.</p>
<p>Tuples are defined by parentheses:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; t = (1, 2, 3)</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; t[1]</span></p>
<p><span style="font-family: Courier New,monospace;">2</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; t[1:]</span></p>
<p><span style="font-family: Courier New,monospace;">(2, 3)<br />
</span><br />
Earlier we saw that strings know how to convert themselves to lists using their &#8217;split&#8217; method. We can also convert a string into a tuple with a function called <span style="font-family: Courier New,monospace;">tuple</span>. It works like this:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; tuple(&#39;some string&#39;)</span></p>
<p><span style="font-family: Courier New,monospace;">(&#39;s&#39;, &#39;o&#39;, &#39;m&#39;, &#39;e&#39;, &#39; &#39;, &#39;s&#39;, &#39;t&#39;, &#39;r&#39;, &#39;i&#39;, &#39;n&#39;, &#39;g&#39;)<br />
</span><br />
You may wonder when will you ever use tuples, since lists do everything tuples can do and more. Well, among other reasons, since tuples are immutable, they are handy whenever you want to make sure that they do not accidentally get changed by some call. Some built in operations require tuples. And last but not least, tuples make for excellent dictionary keys.</p>
<p><a name="Dictionaries"></a></p>
<h3>Dictionaries</h3>
<p>Dictionaries allow us to collect all kinds of objects just as lists and tuples do. However, rather than fetching them back through the use of index or offset location, dictionaries allow us to fetch their items by keys. So, for each item in a dictionary, there is an accompanying key. Here is a dictionary example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; d = {&#39;key1&#39;: &#39;dog&#39;, 2: &#39;cat&#39;, (&#39;key&#39;, &#39;number&#39;, &#39;three&#39;): &#39;mouse&#39;}<br />
</span><br />
As you can see, dictionaries are enclosed in curly brackets &#8216;{}&#8217;, and each key-value pair is separated by a colon &#8216;:&#8217;. Notice that we can use strings (<span style="font-family: Courier New,monospace;">&#39;key1&#39;</span>), numbers (<span style="font-family: Courier New,monospace;">2</span>), and tuples (<span style="font-family: Courier New,monospace;">(&#39;key&#39;, &#39;number&#39;, &#39;three&#39;)</span>) as dictionary keys. All of these are immutable type objects. Values can be anything we want: strings, lists, &#8230;, and even other dictionaries, which contain other dictionaries, that contain other&#8230;. Like lists, dictionaries can be modified in place. However, they are not ordered sequences so, as we said before, we can not use indexes or slices to retrieve their items. It is important to remember that the order in which dictionary items appear is not to be relied upon. Nevertheless, Python offers very simple ways to work with dictionaries. Let&#8217;s look at some examples.</p>
<p>Retrieving a value:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; d[&#39;key1&#39;]</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;dog&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; d[2]</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;cat&#39;<br />
</span><br />
Changing the value of a key:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; d[&#39;key1&#39;] = [&#39;milk&#39;, &#39;cake&#39;]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; d</span></p>
<p><span style="font-family: Courier New,monospace;">{(&#39;key&#39;, &#39;number&#39;, &#39;three&#39;): &#39;mouse&#39;, 2: &#39;cat&#39;, &#39;key1&#39;: [&#39;milk&#39;, &#39;cake&#39;]}<br />
</span><br />
Notice that on the last example the order of the dictionary items is printed differently than how we entered it. Now let&#8217;s add a new entry:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; d[&#39;new&#39;] = 1.5</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; d</span></p>
<p><span style="font-family: Courier New,monospace;">{(&#39;key&#39;, &#39;number&#39;, &#39;three&#39;): &#39;mouse&#39;, 2: &#39;cat&#39;, &#39;key1&#39;: [&#39;milk&#39;, &#39;cake&#39;], &#39;new&#39;: 1.5}<br />
</span><br />
As with lists, to delete an item we can use the del function:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; del(d[(&#39;key&#39;, &#39;number&#39;, &#39;three&#39;)])</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; d</span></p>
<p><span style="font-family: Courier New,monospace;">{2: &#39;cat&#39;, &#39;key1&#39;: [&#39;milk&#39;, &#39;cake&#39;], &#39;new&#39;: 1.5}<br />
</span><br />
Two very important operations that are frequently needed is the ability to retrieve all the keys or all the values contained in a dictionary. Python does this easily:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; d.keys()</span></p>
<p><span style="font-family: Courier New,monospace;">[2, &#39;key1&#39;, &#39;new&#39;]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; d.values()</span></p>
<p><span style="font-family: Courier New,monospace;">[&#39;cat&#39;, [&#39;milk&#39;, &#39;cake&#39;], 1.5]<br />
</span><br />
Notice that in both instances Python returns a list of the items we requested. This is handy because we can then use any of the functions and methods associated with lists, such as sort and reverse, to arrange them in the order that we want. We could then iterate through each item in the right order, in spite of the fact that dictionaries are unordered collections. You may now wonder, what is to iterate? This brings us to our next major subject.</p>
<p><a name="How to program in Python"></a></p>
<h2>How to program in Python</h2>
<p>In this section we are going to see how we can use the different data and object types that we have already covered, as well as a few others, to put together full scale programs.</p>
<p>Up to now all we have done is review some of the main object types available in Python using quick little one line expressions that do a single operation. However, the real power of computer programming is its ability to have the computer do multiple things for us with one simple command on our part. The concept of iteration is a good example of this.</p>
<p>Iteration is “the process of repeating a set of instructions a specified number of times or until a specific result is achieved. For example if we had a list with five items and we wanted to print each one of them, we could manually type five lines of code that would print each item. But what if we did not know how many items the list contained at any particular time, or what if the list contained hundreds of items? Well, actually the solution is very simple. This is how we would do it in Python:</p>
<p><a name="for Loops"></a></p>
<h3>&#8220;for&#8221; Loops</h3>
<p>As we have seen already, lists can be of any length we desire. Lets see what we can do with a little list of 5 items.<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; L = [1, 2, 3, 4, 5]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; for i in L:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     print i</span></p>
<p><span style="font-family: Courier New,monospace;">1</span></p>
<p><span style="font-family: Courier New,monospace;">2</span></p>
<p><span style="font-family: Courier New,monospace;">3</span></p>
<p><span style="font-family: Courier New,monospace;">4</span></p>
<p><span style="font-family: Courier New,monospace;">5<br />
</span><br />
What we are doing is iterating through the list L, assigning a temporary variable named i (can be whatever you want) for each item in L, and printing it. Here is another example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; students = [&#39;John&#39;, &#39;Peter&#39;, &#39;Jeremy&#39;, &#39;Carly&#39;]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; for student in students:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     print &#34;You are a very good student, %s. &#34; %(student)</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;</span></p>
<p><span style="font-family: Courier New,monospace;">You are a very good student, John.</span></p>
<p><span style="font-family: Courier New,monospace;">You are a very good student, Peter.</span></p>
<p><span style="font-family: Courier New,monospace;">You are a very good student, Jeremy.</span></p>
<p><span style="font-family: Courier New,monospace;">You are a very good student, Carly.<br />
</span><br />
This construct is called a for loop. It is the first compound statement that we look at. Notice that the first line, the header, of the loop terminates in a colon. Notice also that the second line of each of the two examples above, are indented. This is very important, as we will see later. The body of a for loop doesn’t have to be limited to one line. It can contain as many lines of commands as desired. It will execute all the commands of the body for each iteration, unless otherwise told in the code.</p>
<p>It is important to understand that the for loop is a sequence iterator. It can iterate through ordered sequence items, such as strings, lists, tuples, etc. This will carry you a long way in your programming needs. However, there is another way to iterate or repeat a series of commands. But before we see it, you need to understand another important concept.</p>
<p><a name="truth Tests"></a></p>
<h3>&#8220;truth&#8221; Tests</h3>
<p>In computer programming, certain expressions evaluate, to either truth or false. For example, we know that it is true that 5 is greater than 4. Python knows that too, and this is how Python expresses that:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; 5 &gt; 4</span></p>
<p><span style="font-family: Courier New,monospace;">1<br />
</span><br />
As you can see, the expression 5 &gt; 4 returned a value of 1. In programming, when evaluating the truthfulness of an expression, 1 is the common value for true and 0 is the common value for false. For example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; 4 == 5</span></p>
<p><span style="font-family: Courier New,monospace;">0<br />
</span><br />
On this example you can see that to evaluate the equality of two values we used two equal signs rather than one. This is because, as we saw earlier, one equal sign is the assignment operator. So remember to use two equal signs when evaluating equality. </p>
<p>Besides math operations, Python recognizes other expressions as either true or false. For example, empty data types, like an empty string, list, etc, evaluate to false. There are also many operators that can be used inside a truth test expression. See the Python documentation for these if you are interested.</p>
<p>You will understand truth tests better by seeing them in action. So, let&#8217;s dive into our second method of iteration.</p>
<p><a name="while Loops"></a></p>
<h3>&#8220;while&#8221; Loops</h3>
<p>A while loop repeats a set of commands until some terminating condition is met. They may never execute the body of the loop if the terminating condition is false to start with. Here is a basic example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; x = 1</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; while x &lt; 10:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     print x,</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     x = x + 1</span></p>
<p><span style="font-family: Courier New,monospace;">1 2 3 4 5 6 7 8 9<br />
</span><br />
Here you can see that the condition that we test for is whether x is less than 10. This little loop will continue repeating itself until that condition is false. You may also notice that by adding a comma at the end of a print statement it does not print a new line character after each print. So, all the numbers are printed in the same line.</p>
<p>One important lesson to remember with while loops is that, unless you want the loop to run forever, you should be sure that the condition will eventually be false. Let&#8217;s just look at another simple example to solidify your understanding:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; s = &#39;Something&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; while s:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     print s,</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     s = s[1:]     # Removes the first character of the string</span></p>
<p><span style="font-family: Courier New,monospace;">&#39;Something&#39; &#39;omething&#39; &#39;mething&#39; &#39;ething&#39; &#39;thing&#39; &#39;hing&#39;, &#39;ing&#39;, &#39;ng&#39;, &#39;g&#39;<br />
</span><br />
In this case, once the string is empty, the while loop stops because <span style="font-family: Courier New,monospace;">s</span> evaluates to false.</p>
<p>So, as we can see, without truth tests while loops would not be possible. However, there is an even more basic programming feature where truth tests play a key role. With it we will be able to start adding real intelligence to our programs.</p>
<p><a name="if Statements"></a></p>
<h3>&#8220;if&#8221; Statements</h3>
<p>The if statement allows our programs to select a course of action depending on whether or not a certain condition exists. The if statement is the third multi-line or compound statement that we look at. Let&#8217;s look at an example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; if 5 &gt; 4:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     print &#34;Life is good&#34;<br />
</span><br />
As in the previous compound statements, if statements have a header. The header begins with the if word itself followed by the expression that is to be evaluated, and ends with a colon. Again the body of the statement is indented.</p>
<p>Let&#8217;s look at a more realistic example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; first_name = &#39;Bob&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; last_name = &#39;&#39;</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; Greeting = &#39;Hello %s %s&#39; % (first_name, last_name)</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; if first_name and last_name:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     print Greeting<br />
</span><br />
In this example we can see that the Greeting would only be printed if both <span style="font-family: Courier New,monospace;">first_name</span> and <span style="font-family: Courier New,monospace;">last_name</span> evaluate to true. Since, in this case, last_name is an empty string it will evaluate to false, and the Greeting will not be printed. Python will simply continue to its next command outside of the body of the if statement. Let&#8217;s look at another example:<br />
<span style="font-family: Courier New,monospace;"><br />
age = int(raw_input(&#34;How old are you? &#34;))     #The int function returns its input as integer.</span></p>
<p><span style="font-family: Courier New,monospace;">gender = raw_input(&#34;Are you male or female? &#34;)</span></p>
<p><span style="font-family: Courier New,monospace;">if age &gt;= 13 and age &lt;= 19:</span></p>
<p><span style="font-family: Courier New,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;if gender.capitalize() == &#34;Male&#34;:</span></p>
<p><span style="font-family: Courier New,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &#34;You are a teenage boy.&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;else:</span></p>
<p><span style="font-family: Courier New,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &#34;You are a teenage girl.&#34;<br />
</span><br />
The raw_input function allows us to get input from the user. If we type those commands in a file named test.py and ran it as a script from the Command Prompt (e.g.: ~\somedirectory&gt;python test.py) we would get the following output:<br />
<span style="font-family: Courier New,monospace;"><br />
How old are you? 15</span></p>
<p><span style="font-family: Courier New,monospace;">Are you male or female? Male</span></p>
<p><span style="font-family: Courier New,monospace;">You are a teenage boy.<br />
</span><br />
Here we can see that if statements can be nested inside other if statements. The same is true for any other compound statement. You can nest compound statements as deep as you like. In this example we also see that an else statement can be used in conjunction with an if statement. An if/else combination allows the program to decide between two courses of action. Only one of the two will be executed. A similar construct is the elif statement (which stands for “else if”) which allows the program to choose from any number of ways to proceed depending on which condition is found to match first. For example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; if day == &#34;Saturday&#34;:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     print &#34;Happy day&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230; elif day == &#34;Sunday&#34;:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     print &#34;Weekend is almost over.&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230; elif month != &#34;August&#34;:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     print &#34;Today is not an August weekend&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230; elif hour &gt; 22:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     print &#34;Time to go to bed&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230; else:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     print &#34;Today is not a weekend, it is August and I still have time to do something&#34;<br />
</span><br />
With if statements we can give our programs many options to select from. It will check each one in order and select the first one that returns a true evaluation. Notice that the “not equal” operator (!=) returns a true evaluation if the Month variable is anything other than “August”. Something to watch out for in this kind of construct is that the “Time to go to bed” string will not get printed even if it is past 10:00 p.m., unless all the previous options were false. And finally, notice that we ended the sequence of statements with an else block. This one will get executed for sure once all the previous options proved false.</p>
<p>So, as you can see, truth tests in conjunction with if/elif/else statements can be very powerful in helping you express the logic of your program. The if statement is very often found inside while and for loops to change the flow of operation of the loop or to exit the loop if a certain condition is found. Let&#8217;s look at an example:<br />
<span style="font-family: Courier New,monospace;"><br />
&gt;&gt;&gt; L = [&#34;mouse&#34;,&#34;&#34;, &#34;cat&#34;, &#34;dog&#34;, &#34;parrot&#34;]</span></p>
<p><span style="font-family: Courier New,monospace;">&gt;&gt;&gt; for animal in L:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     if animal == &#34;dog&#34;:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;         print &#34;Dogs are not allowed&#34;</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;         break</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     elif animal == &#34;&#34;:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;         pass</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;     else:</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;         print &#34;What a nice %s you have&#34; %(animal)</span></p>
<p><span style="font-family: Courier New,monospace;">&#8230;</span></p>
<p><span style="font-family: Courier New,monospace;">What a nice mouse you have</span></p>
<p><span style="font-family: Courier New,monospace;">What a nice cat you have</span></p>
<p><span style="font-family: Courier New,monospace;">Dogs are not allowed<br />
</span><br />
The break statement is used to exit the current loop. If the loop of this example had been inside another loop, the break statement would have simply continued to the next iteration of the outer loop. The pass keyword simply tells Python to do nothing. Again, notice the all important use of indentation to correctly identify the end of nested statements.</p>
<p>What we have covered so far is enough for you to be able to write very rich and powerful programs. However, we have just touched the most basic elements of computer programming. There are a few more concepts to learn in order to take full advantage of what can be done in Python.  We will look at them in <a href="http://www.themaemo.com/python-for-newbies-part-two/">Python for Newbies &#8211; Part 2</a>.<img src="http://www.themaemo.com/?ak_action=api_record_view&#038;id=183&#038;type=feed" alt="" />
<p style="text-align: center;"><a href="http://www.directphoneshop.co.uk/coming-soon-mobile-phones.asp">Latest Mobile Phones 2010</a></p>
<a href='http://www.themaemo.com/python-for-newbies/' class='retweet vert' >Python for Newbies &#8211; Tutorial</a>]]></content:encoded>
			<wfw:commentRss>http://www.themaemo.com/python-for-newbies/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>The N900 and its Killer Feature</title>
		<link>http://www.themaemo.com/and-now-for-something-completely-different-the-n900-and-its-killer-feature/</link>
		<comments>http://www.themaemo.com/and-now-for-something-completely-different-the-n900-and-its-killer-feature/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 04:26:50 +0000</pubDate>
		<dc:creator>rm42</dc:creator>
				<category><![CDATA[Devices]]></category>
		<category><![CDATA[Editorial]]></category>
		<category><![CDATA[Nokia N900]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[maemo review]]></category>
		<category><![CDATA[maemo5]]></category>
		<category><![CDATA[n900]]></category>
		<category><![CDATA[nokia n900]]></category>

		<guid isPermaLink="false">http://www.themaemo.com/?p=142</guid>
		<description><![CDATA[I won't go into every detail and feature because those have been covered ad nauseum already, including the fact that it has by far the best web browser(s) and the best multi-tasking experience of any comparable device out there right now.  I will only focus on some of the features that are unique to the N900 and make it a real standout, and which, if any, could be considered the N900's “ killer feature”.]]></description>
			<content:encoded><![CDATA[<a href='http://www.themaemo.com/and-now-for-something-completely-different-the-n900-and-its-killer-feature/' class='retweet vert' >The N900 and its Killer Feature</a><div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.themaemo.com%2Fand-now-for-something-completely-different-the-n900-and-its-killer-feature%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.themaemo.com%2Fand-now-for-something-completely-different-the-n900-and-its-killer-feature%2F" height="61" width="51" /></a></div>
<p><img class="alignnone size-full wp-image-145" title="Killer_Bunny" src="http://www.themaemo.com/wp-content/uploads/2009/11/Killer_Bunny.jpg" alt="Killer_Bunny" width="333" height="326" /></p>
<p>There are a lot of great smart phones out in the market right now.  They all have some strong points that distinguish them, even if it is just that they happen to run on the network that some users want (or have) to be on.  In this climate it is very hard to come out with a feature that can truly distinguish a device as a standout.  In spite of that, Nokia has created a huge amount of anticipation with the <a href="http://www.themaemo.com/tag/n900/" target="_blank">N900</a>, although they refuse to call it a “Smart Phone”, preferring instead the term “mobile computer”.  <span id="more-142"></span>It is important for prospective buyers to understand why.  Otherwise, some users may purchase it expecting it to be something it is not.  And the opposite is true as well.  Some users may fail to appreciate what it is that the N900 offers that other devices don&#8217;t.  Lets take a look at what the N900 is and is not.</p>
<h3>The N900 does not have the most feature complete phone experience in the world.</h3>
<p>Advanced phone users may be disappointed to find out that there are currently several features missing from the N900 phone application.  For example, you cannot set a different ring tone for different contacts, you cannot group or categorize your contacts, there is no voice dial, etc.  Now, some of these features may appear in the near future as a firmware upgrade, but for the moment they are missing.  This, I believe is why Nokia doesn&#8217;t want to call the N900 a smart phone.  The phone application is just another application on the device, not necessarily the most important.</p>
<h3>The N900 is not the best music player in the world.</h3>
<p>From several reports by early owners of the phone, the sound quality of the N900, both through the <a href="http://www.themaemo.com/tag/n900-speakers/" target="_blank">external built in speakers</a> and through the headphones, is better than average.  However, the built in music player has some features that some users may sorely miss.  For example, there appears to be no built in equalizer, and there is no way to control music play from the provided headset, one would have to get a bluetooth headset for this functionality.  Also, playlists are not as robust as in other players and have a bit of a learning curve since they work a bit differently.  Nevertheless, it is good to note that there are other media players available for the N900 that do offer some of these features and that may be a better fit for some users.</p>
<h3>The N900 is not the best GPS device in the world.</h3>
<p>The N900 comes with a Nokia product called Ovi Maps 1.0.  The 1.0 version number apparently refers to it being the first Maemo release of Ovi Maps.  From many user reports, it is disappointing and much less robust than the Ovi Maps 3.0 available on some Symbian devices.  For example, it does not provide turn by turn voice directions, only visual.  The one advantage it probably has over something like Google&#8217;s Navigator is that that it can be used without a data connection, which is a good thing when there is no 3G signal.  However, there seems to be a third party alternative called <a href="http://www.crowdedbrain.co.uk/mobile-pda/sygic-mobile-maps-for-the-new-nokia-n900.html">Sygic</a> which is apparently gearing up to release a more feature complete GPS navigation package for Maemo.  Also, there are several efforts to leverage Google&#8217;s GPS functionality on the N900.  For example, <a href="http://tomch.com/maemaps.html">Maemaps</a>.</p>
<h3>The N900&#8217;s email functionality is not the best in the world.</h3>
<p>There are apparently several problems when using the built in Mail application, Modest, with an IMAP mail server connection.  If you use Gmail, just use the browser interface.  Also, the Mail for Exchange functionality currently only works with MS Exchange server 2007, not 2003, or Google&#8217;s version of Active Sync.  There are sure to be some improvements in these areas, but if this is important for you right now, beware.</p>
<h3>The N900&#8217;s OS, Maemo 5, is not the most mature and bug free OS in the world.</h3>
<p>If you have seen some of the N900 videos floating around on the Internet you probably know already that the N900 has a very beautiful interface, can multi-task better than any other comparable device, and  has many other unique and desirable features.  Maemo 5 is a considerable upgrade from previous Maemo versions.  However, many of its features have not yet been fully developed and much less stabilized and matured.  That should happen through the life span of the device by means of firmware upgrades.  This is not uncommon in the electronics market where time to market is so important that many times manufacturers feel pressured to release a product before it is completely finished.  Manufacturers have to weigh this option carefully because if the missing or incomplete features result in lots of returns, profits will suffer.  From some early reports, the N900 initial release may be suffering from some quality control problems.  So take that into consideration.  If you happen to receive a defective device you may have to spend a few days without it while you wait for a replacement.</p>
<h3>The N900 does not currently have a huge number of applications.</h3>
<p>I don&#8217;t think this is going to be a big surprise since the phone has only been out for a few days, but I figure I might as well include it.  See below for how this is going to quickly be remedied.</p>
<p>Are you still here? I won&#8217;t feel hurt if at this point you decide that the N900 is not for you.  In fact, I believe a lot of people who are currently buying this phone would probably be better off with a different device.  Just yesterday I recommended an Android phone to a friend of mine.  However, it may be good for you to hang around a bit and let me show you what are the N900&#8217;s strong points.  I won&#8217;t go into every detail and feature because those have been covered ad nauseum already, including the fact that it has by far the best web <a href="http://www.youtube.com/watch?v=JUCc2C4JPJE&amp;hl=en&amp;fs=1&amp;">browser</a>(<a href="http://www.youtube.com/watch?v=hIc-IdUoWpU">s</a>) and <a href="http://www.themaemo.com/nokia-n900-maemo-beast-dragged-to-its-limit/">the best multi-tasking experience</a> of any comparable device out there right now.  I will only focus on some of the features that are unique to the N900 and make it a real standout, and which, if any, could be considered the N900&#8217;s “ <a href="http://www.themaemo.com/and-now-for-something-completely-different-the-n900-and-its-killer-feature/" target="_blank">killer feature</a>”.</p>
<h3>The N900 is the most compact and coolest POSIX-compliant computer in the market.</h3>
<p>Huh, you say?  POSIX (&#8220;Portable Operating System Interface [for Unix]&#8220;) is a geek term used to define certain standard requirements on an operating system, which include “the application programming interface (API), along with shell and utilities interface”<sup>[1]</sup>.  What this means is that the N900 can be used as a normal UNIX computer and run normal UNIX programs.   This is something fascinating for UNIX and Linux computer professionals and aficionados, because it opens a lot of possibilities for them, especially in development.</p>
<h3>The N900 leverages Linux desktop application development platforms.</h3>
<p>Instead of the restrictive limits imposed on developers wanting to program for the iPhone or <a href="http://www.themaemo.com/tag/android/" target="_blank">Android</a>, you can use a number of different languages, and more are likely to become available.  Also, the GUI aspect of development is done using the standard Linux GUI toolkits, GTK and Qt.  What this means is that the hordes of Linux desktop developers are going to find this device very appealing, if not irresistible.</p>
<h3>The N900&#8217;s Killer feature!</h3>
<p>And now, what in my opinion is the most important feature offered by the N900, the one most likely to have the power to lift the N900 above its competition in the long run is: <em>Python!</em> What in the world is Python, you ask?  Well, my little non-geek reader, <a href="http://www.python.org/">Python</a> is a programming language.  So, what makes Python special?  Well, I don&#8217;t have the time to explain it and give it its fair due.  If you are interested, a really good explanation was given by Eric Raymond in his “<a href="http://www.linuxjournal.com/article/3882">Why Python?</a>” essay.   Let me just list some of the reasons I think Python is  the most important feature of the N900.</p>
<ul>
<li>Python lowers the barrier of entry for N900 developers.<br />
This is because Python is very easy to learn.  My first programming language was Basic, followed by Visual Basic.  I assure you, Python is easier to learn and makes a lot more sense.  This is especially important when looking at code produced by somebody else.</li>
<li>Python makes hard problems easier to solve.<br />
This allows developers to attempt to solve more complex problems than with other more complicated languages.  This will likely result in richer programs available on the N900 than on other similar devices.</li>
<li>Python is great for application development on the go.<br />
You can open up an editor, hack away at your code, and execute the code immediately, without having to wait for a compiler to churn through  your code first.</li>
<li>The N900&#8217;s implementation of Python is not dumbed down.<br />
I realize that there is an effort in Android to make Python and other scripting languages available to some degree, but from what I have seen they are relegated to macro like functionality.  In other words, you wont be able to create full blown stand alone Python applications in Android.  The N900, on the other hand, will allow this.  Even the GUI side of the applications can be created with well known Python toolkits like PyGTK and PyQt.  This is very important because there are tons of developers that already know how to write N900 applications, even if they don&#8217;t realize it yet.</li>
</ul>
<h3>Conclusion</h3>
<p>As you may have noticed, what I think makes the N900 a real standout are features that are important for developers!  And developers are exactly the kind of user the N900 is mostly trying to reach.  Nokia has been on record stating that the N900 is not what they consider a consumer ready device.  They consider it the last step before the true consumer ready Maemo device arrives.  That is why it is so important for them to appeal to developers.  The N900 will provide the fertile ground where developers will be able to create the applications that will in the future make Maemo the best option for the regular user.</p>
<p>Does this mean that if you are not a developer you should not buy the N900?  Not necessarily.  As long as you understand the current limitations of the device, like the ones I mentioned above, are savvy enough to deal with some of the growing pains that are likely to be experienced as the OS and its applications mature and stabilize, and as long as you understand and value the advantages that the N900 offers, then you should be really happy with the N900.  If not, well then enjoy using a different device for a while and come back in a little while, when Maemo 5 is a bit more mature, or when its successor, Maemo 6 arrives.  Very likely you are going to want to be back soon, sooner than you might think.</p>
<p><strong><a href="http://www.themaemo.com/python-for-newbies/">Python for Newbies – Tutorial</a></strong></p>
<p>References:<br />
&#8220;<a href="http://en.wikipedia.org/wiki/POSIX">POSIX</a>&#8221; &#8211; Wikipedia<img src="http://www.themaemo.com/?ak_action=api_record_view&#038;id=142&#038;type=feed" alt="" /></p>
<a href='http://www.themaemo.com/and-now-for-something-completely-different-the-n900-and-its-killer-feature/' class='retweet vert' >The N900 and its Killer Feature</a>]]></content:encoded>
			<wfw:commentRss>http://www.themaemo.com/and-now-for-something-completely-different-the-n900-and-its-killer-feature/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Nokia&#8217;s N900 Brings Forum Servers to their Knees</title>
		<link>http://www.themaemo.com/nokias-n900-brings-forum-servers-to-their-knees/</link>
		<comments>http://www.themaemo.com/nokias-n900-brings-forum-servers-to-their-knees/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 17:26:11 +0000</pubDate>
		<dc:creator>rm42</dc:creator>
				<category><![CDATA[Devices]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Nokia N900]]></category>

		<guid isPermaLink="false">http://www.themaemo.com/?p=121</guid>
		<description><![CDATA[The anticipation for the Nokia N900 has been nothing short of phenomenal.  Today, as reports of the first users receiving their devices start to trickle in, the number of forum users started to swell over at talk.maemo.org.  The site began exhibiting slowness.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.themaemo.com%2Fnokias-n900-brings-forum-servers-to-their-knees%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.themaemo.com%2Fnokias-n900-brings-forum-servers-to-their-knees%2F" height="61" width="51" /></a></div>
<p>The anticipation for the Nokia N900 has been nothing short of phenomenal.  Today, as reports of the first users receiving their devices started to trickle in, the number of forum users started to swell over at talk.maemo.org.  The site began exhibiting slowness.  I checked the number of users online sometime around 10:30 a.m. and saw well over 1000 users logged in.  Then, a little after 12:00 p.m. (Eastern Time), just when the east coast users get to their lunch breaks, the site couldn&#8217;t cope any more and became completely unresponsive.  I guess the folks over at maemo.org now need to beef up their website for the increase of traffic this little jewel is generating.  Oh, the joys and perils of success!<img src="http://www.themaemo.com/?ak_action=api_record_view&#038;id=121&#038;type=feed" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.themaemo.com/nokias-n900-brings-forum-servers-to-their-knees/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nokia&#8217;s Maemo OS &#8211; The next big wave</title>
		<link>http://www.themaemo.com/nokias-maemo-os-the-next-big-wave/</link>
		<comments>http://www.themaemo.com/nokias-maemo-os-the-next-big-wave/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 21:57:38 +0000</pubDate>
		<dc:creator>rm42</dc:creator>
				<category><![CDATA[Editorial]]></category>
		<category><![CDATA[Nokia N900]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[maemo]]></category>
		<category><![CDATA[maemo5]]></category>
		<category><![CDATA[n900 maemo]]></category>
		<category><![CDATA[nokia]]></category>
		<category><![CDATA[nokia n900]]></category>

		<guid isPermaLink="false">http://www.themaemo.com/?p=58</guid>
		<description><![CDATA[Trends in computers come in waves.  Developers are like surfers on the shore spying out the horizon looking out for the next big one.  Today I write about a wave that is just now starting to take shape, looks almost innocuous, but that has the potential to be a great ride as well - Nokia's Maemo operating system.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.themaemo.com%2Fnokias-maemo-os-the-next-big-wave%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.themaemo.com%2Fnokias-maemo-os-the-next-big-wave%2F" height="61" width="51" /></a></div>
<div class="wp-caption alignnone" style="width: 433px"><img title="The next big wave" src="http://msnbcmedia1.msn.com/j/msnbc/Components/Photos/041216/041216_hawaii_hmed_4a.h2.jpg" alt="The next big wave" width="423" height="254" />
<p class="wp-caption-text">The next big wave</p>
</div>
<p>Trends in computers come in waves.  Developers are like surfers on the shore spying out the horizon looking out for the next big one.  Today I write about a wave that is just now starting to take shape, looks almost innocuous, but that has the potential to be a great ride as well &#8211; Nokia&#8217;s Maemo operating system.</p>
<p>There have been several Maemo based devices released already, but the latest one, <a href="http://temporaryland.wordpress.com/2009/10/09/nokian900-not-just-an-itoy/">the N900</a> seems to be really capturing the imagination of lots and lots of users and, in particular, developers.  And that is very significant, even crucial for a platform, as Steve Ballmer has been known to admit.  But aren&#8217;t all mobile device developers already coding for Apple?  Well, they might have been, and a great many of them still are, but that wave seems to be heading in to the <a href="http://www.computerworld.com/s/article/9141222/iPhone_owners_demand_to_see_Apple_source_code">rocks</a> and <a href="http://www.paulgraham.com/apple.html">many of its riders are looking to get off</a>.  OK, you may say, but aren&#8217;t developers more likely to get on the Google Android wave instead?  Well, some have already done just that, but I have reason to believe that the Maemo wave is going to be a lot more fun to ride.  Lets see why.<span id="more-58"></span></p>
<p>In case you didn&#8217;t follow the links above, Paul Graham wrote an excellent essay entitled “<a href="http://www.paulgraham.com/apple.html">Apple&#8217;s Mistake</a>”.  I do encourage you to read it.  On that essay Mr. Graham provides a very insightful appraisal of the current situation with lots of iPhone developers.  To sum it up, they are frustrated with Apple&#8217;s heavy handed treatment and are looking for an alternative.  He considers Android as lacking what it takes to really grab a hold of the mass of developers fancy.  Sure, some Apple developers have jumped to it, but he thinks many are going to just hold tight and let that wave go by hoping for a better one.</p>
<p>So, what does Mr. Graham envision as a worthy wave to entice developers into riding it (or writing for it, if you prefer)?  Well, lets take a look:</p>
<ol>
<li> The device has to be desirable as a personal device for the programmer himself.<br />
I think that this is where Android has failed so far.  Just about all of the Android devices that have been released so far have been lacking the awesomeness needed to entice developers away from their beloved iPhones.  They either felt cheap in comparison with the excellent build quality of the iPhone, were lacking in storage space, were lacking in processing speed, were lacking in application memory, etc., or all of the above.</li>
<li>It would have something very appealing specifically to developers.<br />
As he says: “If you could think of an application programmers had to have, but that would be impossible in the circumscribed world of the iPhone, you could presumably get them to switch.”  And what may such an application be?  Thankfully, he is kind enough to tell us. “Could anyone make a device that you&#8217;d carry around in your pocket like a phone, and yet would also work as a development machine? It&#8217;s hard to imagine what it would look like.”</li>
</ol>
<p>Well, I would love to see what Mr. Graham&#8217;s reaction is going to be when he realizes that such a device already exists and is starting to reach the masses of impatient customers right now.  Not only is the N900 very appealing hardware and software wise, but it also happens to have that ability that, so far, has only been a twinkle in his eye – the ability of using your phone as a developer device.  I will write more about this on a separate post.</p>
<p>I believe that even Nokia has been taken by surprise by the interest this device has generated.  Hundreds of people have been glued to their computers following with obsessive interest the Maemo forums looking for clues as to when they are finally going to be having the device on their hands.  I know that there have been people known to camp out overnight outside of Apple&#8217;s stores on the eve of a new iPhone launch.  But, what has been happening surrounding the launch of the N900 is truly an event worthy of study by sociologist.  Take a look if you want at some of the “monster threads” that have been brewing over at the Maemo forums.</p>
<p><a href="http://talk.maemo.org/showthread.php?t=33153">Did your N900 ship today continued</a> (838 replies at time of writing)</p>
<p><a href="http://talk.maemo.org/showthread.php?t=33313">N900 shipping delayed</a> (3,930 replies at time of writing)</p>
<p><a href="http://talk.maemo.org/showthread.php?t=34183">OPK: N900 deliveries have now started!!</a> (2,513 replies at time of writing)</p>
<div id="attachment_59" class="wp-caption alignnone" style="width: 410px"><img class="size-full wp-image-59" title="Can you say mass obsession?" src="http://www.themaemo.com/wp-content/uploads/2009/11/crowd.jpg" alt="Can you say mass obsession?" width="400" height="350" />
<p class="wp-caption-text">Can you say mass obsession?</p>
</div>
<p>In my view, the Nokia N900 is shaping up to be the beginning of a very interesting ride for many mobile developers, and for many developers that were previously not interested in mobile computing.  And for the regular users?  Well, they are in for a treat with all the apps that are going to be developed for this device.  And I am not talking of simple unit converters, shopping list programs, car fuel consumption trackers, etc.  I believe that the level of sophistication and power of the applications that are going to become available for the N900 will be rivaled by no other phone type device in existence.  Don&#8217;t believe me?  Well, feel free to skip this wave.  But as for me, got to go, surf is up.<img src="http://www.themaemo.com/?ak_action=api_record_view&#038;id=58&#038;type=feed" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.themaemo.com/nokias-maemo-os-the-next-big-wave/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
