<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-2908934836353501557</atom:id><lastBuildDate>Fri, 15 Nov 2024 11:58:48 +0000</lastBuildDate><category>python</category><category>lisp</category><category>ascii</category><category>caesar&#39;s shift</category><category>math</category><category>sicp</category><category>string formatting</category><category>App Academy</category><category>Reading list</category><category>chr</category><category>data structures</category><category>decimals</category><category>dictionary</category><category>exception handling</category><category>github</category><category>history</category><category>interpreter</category><category>iterative</category><category>maketrans</category><category>map</category><category>modular</category><category>mutable types</category><category>numbers</category><category>ord</category><category>other resources</category><category>recursive</category><category>style</category><category>thepythonchallenge.com</category><title>def housewife_hacker(Jessie):</title><description>Mom and wife learning computer programming</description><link>http://housewifehacker.blogspot.com/</link><managingEditor>noreply@blogger.com (Gypsy Chemist)</managingEditor><generator>Blogger</generator><openSearch:totalResults>26</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-8536267790171158763</guid><pubDate>Sun, 05 Oct 2014 18:17:00 +0000</pubDate><atom:updated>2014-10-05T12:10:13.117-07:00</atom:updated><title>Sorting Backbone subviews in Rails</title><description>&lt;span style=&quot;font-family: inherit;&quot;&gt;I recently completed my solo project at App Academy, which can be seen&amp;nbsp;&lt;a href=&quot;http://polltheworld.herokuapp.com/&quot; target=&quot;_blank&quot;&gt;here on Heroku&lt;/a&gt;. My polling application has two different index views. One index view is if the current user has not answered the poll, sorted with the most recent at the top. The other index is for the polls they have answered, sorted by their most recently answered polls on top. Each index is a Backbone composite view, consisting of subviews. Every line on the table of polls is a subview, so sorting them turned out to be different than what I first tried. I also had infinite scroll, which probably should have given me a clue on the correct way to solve the problem.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;The first place I looked was the Backbone documentation.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
If you define a comparator, it will be used to maintain the collection in sorted order. This means that as models are added, they are inserted at the correct index in collection.models&lt;/blockquote&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;That seemed to be what I wanted, so I defined a comparator. I wanted the most recent at the top, which could have been done with created_at, but more simply could be done by descending id. Comparator does accept a function with a negative value. Unfortunately, I could not get the order to change in my view. The collection was sorted correctly in my browser console, but my JSON API was not in the correct order. So then I realized my API controller was to blame.&lt;/span&gt;&lt;br /&gt;


&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;def index&lt;/span&gt;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; &amp;nbsp; @polls = Poll.all&lt;/span&gt;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;end&lt;/span&gt;&lt;/blockquote&gt;
The most basic index following RESTful conventions youve probably seen in every Rails tutorial. There is a lot me we could do with this however. Mine ended up as:&lt;br /&gt;&lt;blockquote class=&quot;tr_bq&quot;&gt;
def index&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&amp;nbsp; &amp;nbsp; if params[:answered] == &quot;false&quot;&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @polls = Poll.where.not(id: current_user.answered_polls).order(id: :desc)&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&amp;nbsp; &amp;nbsp; else&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @polls = current_user.answered_polls.order(&quot;responses.id DESC&quot;)&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&amp;nbsp; &amp;nbsp; end&lt;br /&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; @polls = @polls.includes(:responses).page(params[:page])&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&amp;nbsp; &amp;nbsp; @page_number = params[:page]&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&amp;nbsp; &amp;nbsp; @total_pages = @polls.total_pages&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&amp;nbsp; &amp;nbsp; render :index&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
end&lt;/blockquote&gt;
Those last three instance variables were for pagination. I explicitly rendered index here for consistency. All my other actions were rendering JSON, butindex is rendering JSON that I customized in Jbuilder. What is cool here is how both indexes used the association answered_polls differently. In the first view, the id in the resulting table is the poll.id, which would be in the same order that a poll was created in the database. The second example was ordering when the user responded to the poll. I only wanted to see the polls they had answered, but I wanted the id of the responses, not the poll. I could not access responses.id as a symbol, the way I did in the first example. ActiveRecord is pretty good at it&#39;s job, but sometimes you have to give it some SQL in quotes. In order to figure out the filtering and ordering of these two index views, I had to think about how I&#39;d do it in pure SQL. Lucky for me, I love SQL and this solved my sorting problems. &lt;br /&gt;&lt;table class=&quot;highlight tab-size-8 js-file-line-container&quot; style=&quot;background: rgb(255, 255, 255); border-collapse: collapse; border-spacing: 0px; box-sizing: border-box; color: #333333; line-height: 18.200000762939453px; tab-size: 8;&quot;&gt;&lt;tbody style=&quot;box-sizing: border-box;&quot;&gt;
&lt;tr style=&quot;box-sizing: border-box;&quot;&gt;&lt;td class=&quot;blob-code js-file-line&quot; id=&quot;LC24&quot; style=&quot;box-sizing: border-box; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top;&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;/blockquote&gt;
</description><link>http://housewifehacker.blogspot.com/2014/10/sorting-backbone-subviews-in-rails.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-6223202790942853216</guid><pubDate>Sat, 31 May 2014 17:03:00 +0000</pubDate><atom:updated>2014-05-31T10:04:17.899-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">App Academy</category><title>App Academy: W-9D-2 - Being Accepted</title><description>In 9 weeks and 2 days, I will be starting App Academy in San Francisco. For my Pythonistas out there, yes I am aware I may be made fun of for becoming a Rubyist, but I&#39;m confident this is a good decision for me to have career options as a developer for Rails, Backbone, or Django. For those unfamiliar with App Academy, it is a 12 week hacker bootcamp for Ruby, Rails, SQL, JavaScript, and Backbone.js that includes portfolio building, interview preparation and a final project. What is the most different about App Academy is their payment model. Tuition is collected after you get a job, 18% of your yearly earnings payable over 6 months. As someone with two kids too young for public schools, I&#39;m not going to see much of my first 12 paychecks, but it will still be worth the investment.&lt;br /&gt;
&lt;br /&gt;
I learned more about the program through the application and acceptance process. I submitted my application 2.5 weeks ago, never having done any Ruby, but with Python experience. I got an email back the same day for me to do the coding challenge that included reading assignments from Ruby Monk and Chris Pine&#39;s Learn to Program. I spent about 5 days learning Ruby very slowly, because I was visiting family. App Academy does not give a deadline for the coding challenge, so you can take the time you need to prepare. I was prepared to do the coding challenge in Ruby, but the directions said I could do Ruby or Python and it would take about 40 minutes. It took me about 20 minutes to do it in Python while my 9 month old was nursing and my almost 3 year old was &quot;going Bananas&quot; with a cartoon on Netflix (Bruno and the Banana Bunch). The next day, I got an email to schedule an interview with a TA. The Skype interview was audio only and consisted of just one programming question. I did make an error with initializing a variable in the wrong level of my nested loops, but solved it in 9 minutes in Python. I made an effort to talk through my logic process while solving the problem. Less than an hour later, I got an email to schedule a final interview with Kush Patel. There was another live coding problem, which took me 12 minutes to solve in Ruby. I talked through some of the logic, but I realized I was humming at one point (something I do when I&#39;m enjoying a problem). Kush did ask some questions to make sure the program could work for me and that I was aware of some of the requirements. I was accepted the next day, which was quite a surprise because it was a Saturday. So from the time I completed the coding challenge, the interview process took me only 6 days.&lt;br /&gt;
&lt;br /&gt;
One of the requirements of the program is the reason I was being silly with my post title. Every weekday, students post a blog entry with the week number and day number. I have 9 weeks and 2 days until my cohort begins, hence negative numbers. Although tuition is collected afterwards, there is a $3,000 deposit. The deposit holds your spot in the program and goes towards that 18% fee (tuition) when you do get a job, plus or minus any money from fees. Fees are used to keep students focused by penalizing tardiness, absences, getting off task with phones or emails, and failing to write daily blog posts. Fines are redistributed among the graduates at the end so that you only lose money if you were worse than your classmates about following the rules. Another rule of the program is that students may be asked to leave the program. Assessments are taken each week to evaluate if someone is falling behind. It is definitely an intense program, but I&#39;m prepared for a challenge. The gamer inside me expected some fireworks or ominous music when I transferred the first $1250 to hold my place in the program yesterday. And so our hero begins her journey... I&#39;ve currently worked through 12 chapters of Chris Pine&#39;s Learn to Program, 2 chapters of Peter Cooper&#39;s Beginning Ruby, and the first half of the Ruby Primer section on Ruby Monk. I am preparing to have a strong foundation for W1D1 !!</description><link>http://housewifehacker.blogspot.com/2014/05/app-academy-w-9d-2-being-accepted.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-3760666037561595130</guid><pubDate>Thu, 13 Feb 2014 22:43:00 +0000</pubDate><atom:updated>2014-02-13T14:43:33.418-08:00</atom:updated><title>Recursive Turtle Tree</title><description>I know the title of this post may sound like random words thrown together, but it&#39;s actually a really cool activity to learn about recursion using the turtle module in Python (Linux users need to install tkinter). I&#39;ve been learning about algorithms and abstract data structures as part of my faking a CS degree personal mission.&amp;nbsp;&lt;a href=&quot;http://interactivepython.org/runestone/static/pythonds/index.html&quot; target=&quot;_blank&quot;&gt;Problem Solving with Algorithms and Data Structures&lt;/a&gt;&amp;nbsp;has been a fantastic resource so far (and free). Although the authors&#39; explanation of recursion is the best I&#39;ve ever seen, I think I could just add a little to the tree drawing example. Here&#39;s the program they provide:&lt;br /&gt;
&lt;br /&gt;
import turtle&lt;br /&gt;
&lt;br /&gt;
def tree(branchLen,t):&lt;br /&gt;
&amp;nbsp; &amp;nbsp; if branchLen &amp;gt; 5:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; t.forward(branchLen)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; t.right(20)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; tree(branchLen-15,t)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; t.left(40)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; tree(branchLen-15,t)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; t.right(20)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; t.backward(branchLen)&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
&amp;nbsp; &amp;nbsp; t = turtle.Turtle()&lt;br /&gt;
&amp;nbsp; &amp;nbsp; myWin = turtle.Screen()&lt;br /&gt;
&amp;nbsp; &amp;nbsp; t.left(90)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; t.up()&lt;br /&gt;
&amp;nbsp; &amp;nbsp; t.backward(100)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; t.down()&lt;br /&gt;
&amp;nbsp; &amp;nbsp; t.color(&quot;green&quot;)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; tree(75,t)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; myWin.exitonclick()&lt;br /&gt;
&lt;br /&gt;
main()&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Now I&#39;m going to focus on the tree function. Notice tree calls itself twice. Now to step through how this function works, I am going to give each line a number and each recursive call a number such that 1.2 is the second time the first recursive call has been made and 2.3 is the third time the second call has been made.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
def tree(branchLen,t):&lt;br /&gt;
0 &amp;nbsp; &amp;nbsp;if branchLen &amp;gt; 5:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp;t.forward(branchLen)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp;t.right(20)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; 3 &amp;nbsp; &amp;nbsp;tree(branchLen-15,t) &amp;nbsp; &amp;nbsp; first recursive call&lt;br /&gt;
&amp;nbsp; &amp;nbsp; 4 &amp;nbsp; &amp;nbsp;t.left(40)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; 5 &amp;nbsp; &amp;nbsp;tree(branchLen-15,t) &amp;nbsp; &amp;nbsp;second recursive call&lt;br /&gt;
&amp;nbsp; &amp;nbsp; 6 &amp;nbsp; &amp;nbsp;t.right(20)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; 7 &amp;nbsp; &amp;nbsp;t.backward(branchLen)&lt;br /&gt;
&lt;br /&gt;
Let&#39;s trace branchLen = 30, following the code and what the turtle does. I&#39;ve color coded each recursive call:&lt;br /&gt;
&lt;br /&gt;
Code Line &amp;nbsp; &amp;nbsp;Action &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Recursive Call&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Passes &amp;gt; 5 test &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; none&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Forward 30 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; none&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Turns right 20 degrees &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; none&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Calls tree(15) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;none&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: orange;&quot;&gt;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Passes &amp;gt; 5 test &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1.1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: orange;&quot;&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Forward 15 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1.1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: orange;&quot;&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Turns right 20 degrees &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1.1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: orange;&quot;&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Calls tree(0) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1.1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Fails &amp;gt; 5 test &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1.2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: orange;&quot;&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Turns Left 40 degrees &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1.1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: orange;&quot;&gt;5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Calls tree(0) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1.1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: purple;&quot;&gt;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Fails &amp;gt; 5 test &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2.1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: orange;&quot;&gt;6 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Turns right 20 degrees &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1.1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: orange;&quot;&gt;7 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Backward 15 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1.1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Turns Left 40 degrees &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; none&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Calls tree(15) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; none&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Passes &amp;gt; 5 test &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2.2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Forward 15 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2.2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Turns right 20 degrees &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2.2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Calls tree(0) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2.2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #8e7cc3;&quot;&gt;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Fails &amp;gt; 5 test &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1.3&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Turns Left 40 degrees &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2.2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Calls tree(0) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2.2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #38761d;&quot;&gt;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Fails &amp;gt; 5 test &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2.3&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;6 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Turns right 20 degrees &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2.2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;7 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Backward 15 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2.2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;6 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Turns right 20 degrees &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;none&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;7 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Backward 30 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; none&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
If you choose a color such as red or orange, you can see how the function is executed in its entirety, but with interruptions. The colors that appear nested, such as orange inside the red, appear higher in the stack frame. They are completed first even though they started second, but the program continues to the red after the orange is completed. The next value of branchLen to create more recursive calls is 45. You could also try tracing tree(45), using tree(30) as a shortcut to emphasize the benefit of recursion.&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/div&gt;
</description><link>http://housewifehacker.blogspot.com/2014/02/recursive-turtle-tree.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-8208493536252649844</guid><pubDate>Thu, 13 Feb 2014 21:14:00 +0000</pubDate><atom:updated>2014-02-13T13:14:34.778-08:00</atom:updated><title>Coursera -- Introduction to Interactive Programming in Python</title><description>A couple months ago, I took a free course on Coursera.com offered by Rice University and taught by Joe Warren, Scott Rixner, John Greiner, and Stephen Wong. Coursera offers the course a couple times a year, with 2014 offerings including March 24th and October 7th. The course is now part of the Fundamentals of Computing specialization and you earn a certificate after completion with the ability to earn additional &quot;distinction&quot; for scoring an A in the course (I did).&lt;br /&gt;
&lt;br /&gt;
I recommend the course for anyone between &quot;Isn&#39;t Python a snake?&quot; to &quot;Why should I use classes?&quot; Students learn Python by developing simple games in CodeSkulptor, which includes a simple GUI like PyGame without using a terminal or setting up a coding environment. Homework includes quizzes over video material, building games in Python, and evaluating peers&#39; code. I enjoyed the peer reviews because you can recognize common mistakes and see how other people solved the same problem you did. The game development approach finally helped me understand the point of object oriented programming. I went from fear of using classes, to empowered by building my own classes in the last three weeks of the course.&lt;br /&gt;
&lt;br /&gt;
I am looking forward to the next course in the Fundamentals of Computing specialization, Principles of Computing. It is exciting to have an option for continuing to learn after a course. Us self-taught learners often waste time trying to decide what is the most appropriate and useful for us to learn next. If you can tolerate some corny Big Bang references and want to take your Python development to the level of using and understanding classes, you can enroll for free on Coursera.com</description><link>http://housewifehacker.blogspot.com/2014/02/coursera-introduction-to-interactive.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-8804963366209780922</guid><pubDate>Tue, 12 Jun 2012 16:34:00 +0000</pubDate><atom:updated>2012-06-12T09:34:05.438-07:00</atom:updated><title>Moving :)</title><description>I have been busy with reStructuredText lately because I&#39;m moving to &lt;a href=&quot;http://housewifehacker.com/&quot;&gt;housewifehacker.com&lt;/a&gt;. I&#39;ve also been busy with reading&amp;nbsp;entrepreneur&amp;nbsp;blogs and books because our conference management system has been accepted to &lt;a href=&quot;http://startupchile.org/&quot; target=&quot;_blank&quot;&gt;StartUp Chile&lt;/a&gt; to&amp;nbsp;receive&amp;nbsp;$40,000 in equity free funding. July is going to be busy with hacking on different technologies as well as getting our work visas to live in Chile. My new blog will have categories for Code and StartUp, maybe Personal/Travel too, so you can subscribe to which categories you are interested in. I dont have feed burner or RSS set up on the new domain yet, but you can check out my most recent code tutorial on &lt;a href=&quot;http://housewifehacker.com/&quot; target=&quot;_blank&quot;&gt;there&lt;/a&gt; now. It&#39;s a guess the number script using the new preferred string formatting for python. It also&amp;nbsp;addresses&amp;nbsp;some exception handling, the process of developing a script, and a teensy bit about global variables and scope. Please follow me on&amp;nbsp;&lt;a href=&quot;http://twitter.com/housewifehacker&quot; target=&quot;_blank&quot;&gt;twitter&lt;/a&gt; @housewifehacker</description><link>http://housewifehacker.blogspot.com/2012/06/moving.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-6907619242242068517</guid><pubDate>Tue, 15 May 2012 20:50:00 +0000</pubDate><atom:updated>2012-05-15T13:56:26.405-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">data structures</category><category domain="http://www.blogger.com/atom/ns#">dictionary</category><category domain="http://www.blogger.com/atom/ns#">mutable types</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Simple Dictionary Practice: Is Anagram?</title><description>I do not use dictionaries very often. Friday, I was without internet all day, so I took the opportunity to play with dir() and help() to discover some dictionary properties. My short-lived obsession with Draw Something on the iPhone has gotten me interested in anagrams (kicked the habit by reading programming books). I believe using dictionaries is the fastest and most accurate way to determine if two words are anagrams of each other.&lt;br /&gt;
&lt;br /&gt;
A dictionary is an &lt;i&gt;unordered&lt;/i&gt;&amp;nbsp;set of key: value pairs. Keys must be an immutable type. Values can be anything. Review on mutable versus immutable&lt;a href=&quot;http://housewifehacker.blogspot.com/2012/05/mutable-versus-immutable-objects.html&quot; target=&quot;_blank&quot;&gt; here&lt;/a&gt;. Being unordered causes some interesting properties for working with dictionaries, different from any other python data structure. Instead of being indexed by numbers, dictionaries are indexed by keys. Because they are indexed by keys, each key is unique within it&#39;s dictionary. If two dictionaries with the same keys are added to each other, the values of the same data type combine. This is convenient for our anagram activity. But first, some dictionary review.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict = {} &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# creates an empty dictionary&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type( sample_dict )&lt;br /&gt;
&amp;lt;type &#39;dict&#39;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict[&#39;Name&#39;] = &#39;Jessie&#39; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# creating a key:value pair&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict[&#39;Age&#39;] = 23 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# another key:value pair&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict&lt;br /&gt;
{&#39;Age&#39;: 23, &#39;Name&#39;: &#39;Jessie&#39;}&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict2 = {&#39;Name&#39;: &#39;Jessie&#39;, &#39;Age&#39;: 23} &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# another way to create dict&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type (sample_dict2)&lt;br /&gt;
&amp;lt;type &#39;dict&#39;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict2&lt;br /&gt;
{&#39;Age&#39;: 23, &#39;Name&#39;: &#39;Jessie&#39;}&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict + sample_dict2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# cannot add dictionaries, only values&lt;br /&gt;
Traceback (most recent call last):&lt;br /&gt;
&amp;nbsp; File &quot;&amp;lt;console&amp;gt;&quot;, line 1, in &amp;lt;module&amp;gt;&lt;br /&gt;
TypeError: unsupported operand type(s) for +: &#39;dict&#39; and &#39;dict&#39;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict[&#39;Age&#39;] + sample_dict2[&#39;Age&#39;] &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# adds values&lt;br /&gt;
46&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict.keys()&lt;br /&gt;
[&#39;Age&#39;, &#39;Name&#39;]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict.values()&lt;br /&gt;
[23, &#39;Jessie&#39;]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type( sample_dict.values() ) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# keys and values are returned as lists&lt;br /&gt;
&amp;lt;type &#39;list&#39;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict.get(&#39;Age&#39;) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# gets the value at a specific key&lt;br /&gt;
23&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type( sample_dict.get(&#39;Age&#39;)) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; # value maintains data type in dictionary&lt;br /&gt;
&amp;lt;type &#39;int&#39;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict.has_key(&#39;Age&#39;) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# D.has_key() returns boolean&lt;br /&gt;
True&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict3 = {&#39;Children&#39;: &#39;Graham&#39;}&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict3.update(sample_dict) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# update keys and values&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict&lt;br /&gt;
{&#39;Age&#39;: 23, &#39;Name&#39;: &#39;Jessie&#39;}&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sample_dict3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# Children field is added as a key:value pair&lt;br /&gt;
{&#39;Age&#39;: 23, &#39;Children&#39;: &#39;Graham&#39;, &#39;Name&#39;: &#39;Jessie&#39;}&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; {&#39;Age&#39;: 23, &#39;Name&#39;: &#39;Jessie&#39;} == {&#39;Name&#39;: &#39;Jessie&#39;, &#39;Age&#39;: 23} &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;# different order is equal&lt;br /&gt;
True&lt;/blockquote&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
How do we know if two words are anagrams? Consider the anagrams odor and door. We could say that they are reshuffled strings. Each word uses the same letters, but in a different order: 2 o&#39;s, 1 r, and 1 d. My simple program creates empty dictionaries for the two words being compared, stores the letters as keys, and adds to the value for each&amp;nbsp;occurrence of the same letter, then checks that the dictionaries are equivalent.&amp;nbsp;I have not included exception handling and I made the design decision to count white space as part of the anagram such that &#39;abc def&#39; is not an anagram of &#39;fdeabc,&#39; but is an anagram of &#39;abc fed.&#39;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
def get_dict(word, count):&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;for i in word.lower():&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if count.has_key(i):&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;count[i] += 1&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;count[i] = 1&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;return count&lt;br /&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&amp;nbsp;def main():&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;word1 = raw_input(&quot;What is the first word? \n&quot;)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;word2 = raw_input(&quot;What is the second? \n&quot;)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;count1 = {}&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;count2 = {}&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;count1 = get_dict(word1, count1) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;count2 = get_dict(word2, count2)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;if count1 == count2:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print(&quot;Yes, those are anagrams!\n&quot;)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print(&quot;No, you&#39;ve failed \n&quot;)&lt;br /&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&amp;nbsp;if __name__ == &quot;__main__&quot;:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;main()&lt;/blockquote&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://housewifehacker.blogspot.com/2012/05/simple-dictionary-practice-is-anagram.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-6700737333957845589</guid><pubDate>Fri, 11 May 2012 14:11:00 +0000</pubDate><atom:updated>2012-05-11T07:11:47.322-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">other resources</category><title>Book Review: The Practice of Programming</title><description>&lt;a href=&quot;http://www.amazon.com/gp/product/020161586X/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&amp;tag=gypsyc-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=020161586X&quot; target=&quot;_blank&quot;&gt;Affiliate Link&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
The Practice of Programming, written by Brian W. Kernighan and Rob Pike, was originally published in 1999. Although most programming books more than a couple years old are obsolete with out of date technology, The Practice of Programming is a pragmatic guide to become a better programmer regardless of your chosen language, framework, or supporting technologies. To quote the epilogue, &quot;The world of computing changes all the time... but there are some constants, some points of stability, where lessons and insight from the past can help with the future.&quot; I highly recommend this book to any programmer who wants to establish good habits for writing understandable and consistent code. I also recommend this for programmers who want to or need to be able to work on projects with other developers. Although the exercises and examples were written in languages other than python, I was able to learn a lot. I reflected a lot on my own code, figured out new questions to ask about python, and am more appreciative  that python does not suffer from some of the common pitfalls of other languages.&lt;br /&gt;
</description><link>http://housewifehacker.blogspot.com/2012/05/book-review-practice-of-programming.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-2710634762463227529</guid><pubDate>Mon, 07 May 2012 18:50:00 +0000</pubDate><atom:updated>2012-05-07T11:50:59.447-07:00</atom:updated><title>Mutable versus Immutable Objects</title><description>Strings, lists, tuples, integers, float, dictionaries, and sets are all types of python objects with different properties and uses. When using an object in a program or in your terminal, your session assigns an Id to access the computer&#39;s memory. The Id will be unique each session and on each computer, so the actual number returned by the id() function is irrelevant. What is relevant is when that number changes. If the id changes as the value changes, the computer had to assign a new id to the immutable object. If the id stays the same, then it is a mutable object, because the value associated with the id can be changed without changing the id. Integers are immutable:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; x = 5&lt;br /&gt;
&lt;div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; y = x &amp;nbsp; &amp;nbsp;# direct the reference of y to be the reference of x&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; id(x) == id(y) &amp;nbsp; &amp;nbsp;# x and y point to same reference&lt;/div&gt;
&lt;div&gt;
True&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; x = 4 &amp;nbsp; &amp;nbsp;# change the value of x&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; id(x) == id(y) &amp;nbsp; &amp;nbsp;# the id of x changed when we changed the value&lt;/div&gt;
&lt;div&gt;
False&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; x == y &amp;nbsp; &amp;nbsp;# the value of y did not change with x&lt;/div&gt;
&lt;div&gt;
False&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
I&#39;m the type of learner that skims through vocabulary lessons to get to the action, but understanding this next part will save you some headaches when trying to manipulate mutable objects. Look what happens when I try to do the same thing I just did to the integers, but now to a list:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; x = [5]&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; y = x &amp;nbsp; &amp;nbsp;# direct the reference of y to be the reference of x&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; id(x) == id(y) &amp;nbsp; &amp;nbsp;# x and y point to the same reference&lt;/div&gt;
&lt;div&gt;
True&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; x.append(4) &amp;nbsp; &amp;nbsp;#change x from [5] to [5, 4]&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; id(x) == id(y) &amp;nbsp; &amp;nbsp;# x and y still point to the same reference&lt;/div&gt;
&lt;div&gt;
True&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; x == y &amp;nbsp; &amp;nbsp;# y changed with x&lt;/div&gt;
&lt;div&gt;
True&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; y&lt;/div&gt;
&lt;div&gt;
[5, 4]&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; z = [5, 5] &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; id(z) &amp;nbsp; &amp;nbsp;# will be a different number for everyone&lt;/div&gt;
&lt;div&gt;
3075316972L&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; z.append(3) &amp;nbsp; &amp;nbsp;#z is now [5, 5, 3]&lt;/div&gt;
&lt;div&gt;
&amp;gt;&amp;gt;&amp;gt; id(z) &amp;nbsp; &amp;nbsp;# id is constant, the list is mutable&lt;/div&gt;
&lt;div&gt;
3075316972L&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
I stumbled upon this while using random.shuffle on a list, while wishing to keep a copy of the list in it&#39;s original form. As you can see by assigning x equal to y, the lists changed together. That was an ineffective way to make a copy because all I did was assign the same Id two different names. Try determining if the other object types are mutable or immutable. I don&#39;t want to spoil the fun for you.</description><link>http://housewifehacker.blogspot.com/2012/05/mutable-versus-immutable-objects.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-2586396760223413271</guid><pubDate>Sun, 22 Apr 2012 21:57:00 +0000</pubDate><atom:updated>2012-04-23T06:29:02.629-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">python</category><category domain="http://www.blogger.com/atom/ns#">style</category><title>Python Style Guide</title><description>Did you realize Python has a style guide to clarify consistent and readable code? It covers topics such as white space of indentations in-line, capitalization of variable, function, and class names, and formatting of comments. Read &lt;a href=&quot;http://www.python.org/dev/peps/pep-0008&quot; target=&quot;_blank&quot;&gt;Pep8&lt;/a&gt; &lt;br /&gt;
&lt;br /&gt;
Now I look at my code examples and see a whole lot of ugly. Some good news is that some of the proper styles can be integrated into your vim configuration. A tab on your keyboard can type four spaces. A red line can be drawn vertically to show when a line exceeds the recommended 79 characters. The other good news is that you can run the pep8 script to find style errors. I am going to make an effort to follow the style guide, as well as improve my names. By using &quot;is&quot; to preface boolean tests, verbs such as &quot;get&quot; prefacing function names, and complete words or widely accepted abbreviations (such as str for string), my code can be more readable. I am also going to make my names something that I can sound out. As my code becomes more complicated, variable class and method names are easier to remember if you can pronounce them out loud.&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
To use pep8, you simply install it using pip or easy install, run it against your python file, then read the corrections pep8 expects. Example corrections may be that two empty lines were expected instead of one, max line length was surpassed, or extra spacing was found. The script prints the lines that each instance is found on, so you can easily make changes if you want to comply with the preferred style. Documentation can be found &lt;a href=&quot;http://pypi.python.org/pypi/pep8&quot;target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
I am reaching a stage in my learning where I am reading more code than I am writing. Ambiguous names, inconsistent capitalization, incorrect spelling, and weird in-line spacing makes reading code more difficult. I did have the mindset that if Python didn&#39;t care, why should I? Well, now I do care. Just like when learning a spoken language, reading is an important part of learning how to communicate with the language properly.</description><link>http://housewifehacker.blogspot.com/2012/04/python-style-guide.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-4759693812879538493</guid><pubDate>Tue, 17 Apr 2012 22:14:00 +0000</pubDate><atom:updated>2012-04-17T15:14:00.716-07:00</atom:updated><title>Just a reflection</title><description>We have moved to Chile. Yes, the country. I really like it ere, except that my husband&#39;s laptop warranty does not apply here. So because he spilt Zuko on his computer, he is using mine and I cannot code Python on an iPhone. I have had some recent experiences that I thought I&#39;d share. First, I want to get better with Linux. If you do not know Linux, you are not a nerd and you are powerless to improve your operating system and most of your coding environment. A friend&#39;s business uses Linux machines as kiosks, so I was setting up all the applications they use on their newest system. The newest version of Ubuntu is insanely beginner friendly. Instead of just giving an error sound when you&#39;ve messed up, it attempts to correct your mistake or explain why you couldn&#39;t do what you were trying to do. Of course Linux is free and so are the programs for it. Just about anything you would buy for your Windows or iOS computer has a free open source version that runs on Linux. In Linux, you can customize everything to suit your needs and style. I can have a window manager that allows me to never use my touchpad/mouse. With a custom operating system and vim configuration, coding could be more productive.&lt;br /&gt;
&lt;br /&gt;
Another reflection is the cool use of Python for games. Blender is a gaming engine with a GUI, simple enough for artistic people to draw and program 3D games. Blender can also be used for video animations. I like Khan Academy, but think that their videos resemble a lecture. After five minutes, I want to fall asleep or check Facebook. I want to try to make instructional videos in Blender to teach math and physics. Blender has a built in physics engine, so I do not think creating physics animations would be difficult. Check out Pissed Off Penguins on YouTube to see Blender being used to make an Angry Birds similar game. The other cool gaming technology in Python is Pygame. I was not previously interested in Pygame. Tetris, Woohoo. But a friend&#39;s application of Pygame in his kiosks is so intriguing to me. I now want to learn other applications of PyGame. &lt;br /&gt;
&lt;br /&gt;
I learned what I could from SICP, but it wasn&#39;t applicable to my interests. I like math, physics, and chemistry, but I&#39;m not interested in using Scheme to prove all of Newton&#39;s theorems. The first chapter was worth reading, but that&#39;s all. So for now I am learning Spanish, playing with my dSLR, and wishing I could build something with Python. Web apps are intimidating, but I&#39;m pretty sure I can do Blender and PyGame. Hopefully I&#39;ll get my computer back in the next three weeks, but probably not :(</description><link>http://housewifehacker.blogspot.com/2012/04/just-reflection.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-8872646213208503801</guid><pubDate>Fri, 16 Mar 2012 00:58:00 +0000</pubDate><atom:updated>2012-03-15T17:58:58.449-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">history</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>ABC and The Birth of Python</title><description>As I have mentioned in a previous post, I am very interested in the how and why of programming, which includes the history of different languages. One of the posters at PyCon 2012 held in Santa Clara over the past week was about the programming language ABC. There is a Youtube video that is unfortunately edited poorly and has the microphones off during the most important part of the interview &lt;a href=&quot;https://www.youtube.com/watch?v=u2-MbpX-bxQ&quot;&gt;here&lt;/a&gt;. &lt;br /&gt;
&lt;br /&gt;
Guido van Rossum is often called &quot;The Father of Python&quot; because of his critical involvement in the development of the language. He wanted programming to be more intuitive, easier to learn, written in true English, and available to everyone. However, he did not start Python until ABC lost its funding. He also did not come up with all the basic principles on his own. Although he did work on the ABC project, he was not one of the original creators, nor is he attributed a large portion of the credit. Leo Geurts, Lambert Meertens, and Steven Pemberton are acknowledged as the leads on the ABC Project sponsored by CWI, Netherlands. &lt;br /&gt;
&lt;br /&gt;
Like Python, ABC uses indentions (white space) to organize loops. ABC is also much easier to read than the programming languages of its time, BASIC, Pascal, and AWK. Tuples, lists, and strings were important in ABC, as they now are in Python. Tuples and indentations were actually utilized by SETL before they were used by ABC. &lt;br /&gt;
&lt;br /&gt;
By looking at examples of&amp;nbsp;&lt;a href=&quot;http://www.setl-lang.org/wiki/index.php/Main_Page&quot;&gt;SETL&lt;/a&gt; developed in the late 1960&#39;s, then examples of &lt;a href=&quot;http://homepages.cwi.nl/~steven/abc/&quot;&gt;ABC&lt;/a&gt; from the 1980&#39;s, and finally modern Python, the progression of the structure and readability is apparent. Python is a beautiful language because of its intuitive design.</description><link>http://housewifehacker.blogspot.com/2012/03/abc-and-birth-of-python.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-2133141226507186953</guid><pubDate>Wed, 14 Mar 2012 20:51:00 +0000</pubDate><atom:updated>2012-03-14T13:51:54.549-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">exception handling</category><category domain="http://www.blogger.com/atom/ns#">iterative</category><category domain="http://www.blogger.com/atom/ns#">lisp</category><category domain="http://www.blogger.com/atom/ns#">modular</category><category domain="http://www.blogger.com/atom/ns#">numbers</category><category domain="http://www.blogger.com/atom/ns#">python</category><category domain="http://www.blogger.com/atom/ns#">recursive</category><category domain="http://www.blogger.com/atom/ns#">sicp</category><title>SICP Square Root Procedure</title><description>I haven&#39;t progressed much further in SICP. There have been so many interruptions. We traveled to Santa Clara for Pycon, where my husband, sontek, lead a tutorial on gevent, socketio, and realtime web (all things currently above my comprehension). Our son met Steve Holden, the chair of the Python Foundation, and many other developers known in the world of Python. I was hanging out in the lobby of the conference center one day so that I could see some of my husband despite the time involvement of the conference. Oddly enough, my SICP book got just as much attention as having a baby in a stroller and being female. I was more than the elephant in the room. I was a pink elephant wearing roller skates. &lt;br /&gt;
&lt;br /&gt;
Continuing with SICP, I want to discuss the square root procedure example. It is excellent at demonstrating a lot of basic programming principles. Of course, Python has excellent &lt;a href=&quot;http://housewifehacker.blogspot.com/2010/10/math-with-python.html&quot;&gt;math tools&lt;/a&gt; that make defining a procedure for square root unneccesary, but let&#39;s just do it for fun. First, we need to understand &lt;a href=&quot;http://http//en.m.wikipedia.org/wiki/Newton&#39;s_method&quot;&gt;Newton&#39;s Method&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Let&#39;s pretend we did not know the square root of 9. We GUESS the answer is 2. But 2 squared is 4. So how do we get closer to the answer? We take the average of our GUESS and X, which is 9, divided by our most recent guess. The average would be 2.75 ( (9/2+2)/2 ). Now 2.75 becomes our new GUESS. We repeat, until guess squared is equal to 9, our x. So what does our code need to do?&lt;br /&gt;
&lt;br /&gt;
make a guess -&amp;gt; square guess to check if good enough -&amp;gt; take the average of guess and x divided by guess if not good enough -&amp;gt; use computed average to improve guess -&amp;gt; square guess to check if good enough -&amp;gt; take the average...&lt;br /&gt;
An iterative procedure (repeating) until when?&lt;br /&gt;
&lt;br /&gt;
Some numbers may take a very long time to solve exactly, or the square root may be a decimal with over 10 numbers beyond the decimal, so we also need to allow for some error. When we check if the square of the guess is equal to our x value, we can allow a difference less than .001, between the squared guess and the value of x, ending the iterations. We also need to define what our original guess will be. SICP uses 1.0, so let&#39;s try that. In both lisp and python, a number with a decimal allows floating numbers to be computed, while an integer without a decimal does not. Some of our definitions are modular, or reusable, so I&#39;m going to give square, average, and abs their own definitions. I am ignoring the built-in absolute value, just so we can do more coding. I&#39;m calling the absolute value definition abs2. I solve this problem in a different order than SICP, so I&#39;m going to show my logic, in Python.&lt;br /&gt;
&lt;br /&gt;
First, I wrote my code in Vim, so I could fix typos easily and experiment more quickly. In my terminal, I created sqrtsicp.py:&lt;br /&gt;
&lt;br /&gt;
vim sqrtsicp.py&lt;br /&gt;
&lt;br /&gt;
Our modular definitions mentioned previously are easy. Let&#39;s get them out of the way.&lt;br /&gt;
&lt;blockquote&gt;def square(x):&lt;br /&gt;
&amp;nbsp; &amp;nbsp; return x*x&lt;br /&gt;
&lt;br /&gt;
#naming absolute as abs2 to avoid built-in &quot;abs&quot;&lt;br /&gt;
def abs2(x):&lt;br /&gt;
&amp;nbsp; &amp;nbsp; if x&amp;lt;0:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return -x         &lt;br /&gt;
&amp;nbsp; &amp;nbsp;else:                 &lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return x  &lt;br /&gt;
&lt;br /&gt;
def average(x,y):&lt;br /&gt;
&amp;nbsp; &amp;nbsp;return (x+y)/2&lt;/blockquote&gt;&lt;br /&gt;
Now for the hard part. We&#39;ll skip the first guess right now. The iterative part involves checking if our guess is good enough, then improving our guess if it is not good enough (good enough is false), only to try to determine if our improved guess is good enough.&lt;br /&gt;
&lt;br /&gt;
def sqrt_iter(guess):&lt;br /&gt;
&amp;nbsp; &amp;nbsp;if good_enough(guess):&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return guess&lt;br /&gt;
&amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return sqrt_iter(improve(guess))&lt;br /&gt;
&lt;br /&gt;
Next we define how we determine if good_enough is true and how to improve our guess. Both of these need to know the value of x to compute.&lt;br /&gt;
&lt;br /&gt;
def good_enough(guess,x):&lt;br /&gt;
&amp;nbsp; &amp;nbsp;return abs2(square(guess)-x)&amp;lt;.001&lt;br /&gt;
&lt;br /&gt;
def improve(guess,x):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;return average(guess,x/guess)&lt;br /&gt;
&lt;br /&gt;
Now we have two problems. One, the x is being used for good_enough() and improve() without receiving the value of x from sqrt_iter(). Second, we need a starting guess. This is the point where I put good_enough(), improve() and sqrt_iter() all together and give it an initial guess. Our variable x is a constant and can be passed in from the overall procedure sqrt().&lt;br /&gt;
&lt;blockquote&gt;def sqrt(x):&lt;br /&gt;
&amp;nbsp; &amp;nbsp; def good_enough(guess):&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return abs2(square(guess)-x)&amp;lt;.001&lt;br /&gt;
&amp;nbsp; &amp;nbsp; def improve(guess):&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return average(guess,x/guess) &amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; def sqrt_iter(guess): &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if good_enough(guess): &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return guess&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return sqrt_iter(improve(guess))&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;return sqrt_iter(1.0)&lt;/blockquote&gt;&lt;br /&gt;
It&#39;s good structuring to have all your definitions before your actions. The final line is the return function that causes the dominos to fall. sqrt_iter() calls itself on the new guess if our latest guess was not good enough. &quot;Calling itself&quot; is known as recursion. Now just make it a program. Within my file, I define the main procedure (not sure of the programmers vernacular)where my program is going to ask what to take the square root of. I want the input to be any positive number, including decimals. I want to use exception handling to ask for a floating number if the user inputs a string. I&#39;m also going to define the decimal places for the output of the square root procedure to 3 digits past the decimal. If a negative number is entered, I ask for a positive number and attempt the program again. If a string is entered, I say &quot;Oops, that&#39;s not a number&quot; before calling the program again.&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;def main():&lt;br /&gt;
&amp;nbsp; &amp;nbsp; try:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; x=float(raw_input(&quot;What number do you want to find the square root of? &quot;))&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; answer = sqrt(x)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print(&quot;The answer is \n%.3f&quot;) %answer&lt;br /&gt;
&amp;nbsp; &amp;nbsp; except:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print(&quot;Oops, that is not a number&quot;)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; main()&lt;br /&gt;
&lt;br /&gt;
if __name__==&quot;__main__&quot;:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; main()&lt;/blockquote&gt;&lt;br /&gt;
Those three pieces combined are a completed program. In my terminal, I can run it by typing:&lt;br /&gt;
python sqrtsicp.py &lt;br /&gt;
&lt;br /&gt;
Playing with it, I attempt to find the square root of -9, 874, .04, and L. All my results are what I expect. This may be a good candidate for selenium testing or a unit test in more complicated programs. For another example of exception handling, view my &lt;a href=&quot;http://housewifehacker.blogspot.com/search/label/caesar%27s%20shift&quot;&gt;Caesar&#39;s Shift&lt;/a&gt;. For more information of float from wikipedia, read &lt;a href=&quot;http://en.wikipedia.org/wiki/Floating_point&quot;&gt;this article&lt;/a&gt;. You can also refresh your math tools knowledge, including absolute value &lt;a href=&quot;http://housewifehacker.blogspot.com/2010/10/math-with-python.html&quot;&gt;here&lt;/a&gt;.</description><link>http://housewifehacker.blogspot.com/2012/03/sicp-square-root-procedure.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-2306466853549005099</guid><pubDate>Fri, 09 Mar 2012 16:35:00 +0000</pubDate><atom:updated>2012-03-09T08:35:33.676-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">interpreter</category><category domain="http://www.blogger.com/atom/ns#">lisp</category><category domain="http://www.blogger.com/atom/ns#">sicp</category><title>Applicative Order Versus Normal Order Interpreter</title><description>How would you solve for c in the following problem? Ignore computer programming languages for just a minute.&lt;br /&gt;
&lt;blockquote&gt;b=(a+2)*(a+1) c=a*b a=3&lt;/blockquote&gt;Choice 1:&lt;br /&gt;
&lt;blockquote&gt;c=(3)*(3+2)*(3+1)&lt;br /&gt;
c=3*5*4&lt;br /&gt;
c=60&lt;/blockquote&gt;Choice 2:&lt;br /&gt;
&lt;blockquote&gt;c= (3)*((3+2)*(3+1))&lt;br /&gt;
c=(3)*(5*4)&lt;br /&gt;
c=(3)*(20)&lt;br /&gt;
c=60&lt;/blockquote&gt;&lt;br /&gt;
They look very similar and yield the same result, however they act differently. In choice 1, the numbers are substituted and evaluated at the end. In Choice 2, the intermediate step (solving for b) is completed before evaluating for c. Choice 2 is applicative order, or strict evaluation. Choice 1 is normal order, also known as lazy evaluation because it only calls what is needed. The Lisp interpreter acts like Choice 2.&lt;br /&gt;
&lt;br /&gt;
Knowing that lisp is applicative-order, we can now theorize the results of the somewhat famous Ben Bitdiddle Test, exercise 1.5 in SICP.I attempted to predict the outcome yesterday when I did not have access to my computer. I&#39;m still bitter about it. First is the example written in lisp. Second is the test translated into Python. Do you think lisp and python act the same? What will they do when tested?&lt;br /&gt;
&lt;blockquote&gt;(define (p) (p))&lt;br /&gt;
(define (test x y)&lt;br /&gt;
(if (= x 0)&lt;br /&gt;
0&lt;br /&gt;
y))&lt;br /&gt;
&lt;br /&gt;
(test 0 (p))&lt;/blockquote&gt;Or in Python&lt;br /&gt;
&lt;blockquote&gt;def p():&lt;br /&gt;
return p()&lt;br /&gt;
&lt;br /&gt;
def test(x, y):&lt;br /&gt;
return 0 if x==0 else y &lt;br /&gt;
&lt;br /&gt;
test(0, p())&lt;/blockquote&gt;&lt;br /&gt;
I spent about 45 minutes irritated, thinking there was no way lisp would return either 0 or y, because the p would be looping forever, I was able to get on a computer. I WAS RIGHT! Applicative order interpreters cannot evaluate the test, because they are too concerned with the definition of p. &quot;Lazy&quot; or normal order interpreters only apply the needed information, so they are able to return 0. According to this test, Python is also a strict interpreter, using applicative order like lisp. I expect the differences between normal order and applicative order will be relevant again in further reading of SICP.</description><link>http://housewifehacker.blogspot.com/2012/03/applicative-order-versus-normal-order.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-2544999415843346984</guid><pubDate>Sat, 03 Mar 2012 22:39:00 +0000</pubDate><atom:updated>2012-03-03T14:39:54.102-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">lisp</category><category domain="http://www.blogger.com/atom/ns#">Reading list</category><title>Getting Back Into It</title><description>It has been awhile since I have posted anything for two reasons. First, I got stuck. Python came easy for me, but programming requires much more than knowing one language. For that reason, I have decided to resume my learning with more elementary programming concepts. Some people learn by doing, but I learn by dissecting. I need to know how and why things work the way they do. Which programming languages came first? How does a program evaluate a complex math problem with variables? What are the strengths and weaknesses of each of the languages? What are interpreters and how do they work? How does code become a website? My second reason for my absence is that I had a baby. Now 8 months old, he is a little less demanding of my attention and energy than he was before he was capable of sitting up on his own. &lt;br /&gt;
&lt;br /&gt;
I am currently reading &lt;a href=&quot;http://www.amazon.com/gp/product/0070004846/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&amp;tag=gypsyc-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0070004846&quot;&gt;Structure and Interpretation of Computer Programs&lt;/a&gt;. SICP and &lt;a href=&quot;http://www.amazon.com/gp/product/0262560992/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&amp;tag=gypsyc-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0262560992&quot;&gt;Little Schemer&lt;/a&gt; are used to teach generalized computer programming principles, utilizing the languages of LISP and Scheme for examples. Both books were created at MIT for computer programmers. Lisp is the second oldest programming language still in use. Reddit.com was written in Lisp before it was converted to Python. Unlike how Python is organized by white space, Lisp uses parenthesis. A ridiculous amount of parenthesis. I have not yet attempted to write code in Lisp, but I am anticipating customizing a VIM configuration for using only with Lisp will be beneficial. I am enjoying the conciseness and organization of Lisp, compared to Python. Below is an example of the same mathematical expression in the two languages.&lt;br /&gt;
 &lt;br /&gt;
Python&lt;blockquote&gt;3 * a + 7 + b&lt;/blockquote&gt;Lisp&lt;blockquote&gt;(+ (* 3 a) 7 b)&lt;/blockquote&gt;&lt;br /&gt;
I&#39;ll be discussing any &quot;Eureka&quot; revelations here on the blog, though it may take me a couple weeks to get through SICP.</description><link>http://housewifehacker.blogspot.com/2012/03/getting-back-into-it.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-6732138787130818958</guid><pubDate>Sun, 05 Dec 2010 04:39:00 +0000</pubDate><atom:updated>2010-12-04T21:03:22.968-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">github</category><title>Now on Github</title><description>HousewifeHacker is now on &lt;a href=&quot;https://github.com/HousewifeHacker&quot;&gt;Github&lt;/a&gt;. To avoid further formatting problems with pasting my code on the blog, I will be using github to embed my code. For those who are unfamiliar with Github, the site allows programmers to follow open source contributions from different authors and contribute to full projects (Repositories) or small code snippets (Gists) by creating your own code or making changes to other user&#39;s work (forking). Users can determine whether their repositories and gists are public or private. The objective of Github is to serve as a social coding resource as well as a revision control. Because I want to be able to contribute to open source projects as I become more knowledgeable of Python, Github will become an increasingly valuable resource.</description><link>http://housewifehacker.blogspot.com/2010/12/now-on-github.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-4383980251470024340</guid><pubDate>Fri, 03 Dec 2010 02:51:00 +0000</pubDate><atom:updated>2010-12-04T21:05:00.709-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">ascii</category><category domain="http://www.blogger.com/atom/ns#">caesar&#39;s shift</category><category domain="http://www.blogger.com/atom/ns#">maketrans</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>maketrans Python and Improved Caesar&#39;s Shift</title><description>Maketrans is used to translate strings, character for character. The characters can be symbols, letters, or numbers. The following short example is a Caesar&#39;s Shift moving backwards by two letters.&lt;br /&gt;&lt;pre&gt;#first we need to import maketrans&lt;br /&gt;&gt;&gt;&gt; from string import maketrans&lt;br /&gt;#now define the translation&lt;br /&gt;#parameters are strings, not lists&lt;br /&gt;#first parameter defines what will be found in the string&lt;br /&gt;#second parameter defines what each will be changed to&lt;br /&gt;&gt;&gt;&gt; trans = maketrans(&#39;cdefg&#39;,&#39;abcde&#39;)&lt;br /&gt;#define our string and translate&lt;br /&gt;&gt;&gt;&gt; s = &#39;deg&#39;&lt;br /&gt;&gt;&gt;&gt; s.translate(trans)&lt;br /&gt;&#39;bce&#39;&lt;/pre&gt;&lt;br /&gt;Now using &lt;b&gt;maketrans&lt;/b&gt; and a few other suggestions from the comments, I have written a newly improved Caesar&#39;s Shift. Encode and decode use the same code, because a shift of 4 in one direction of a 26 character alphabet is equivalent to a shift of 22 in the other direction. I also used ascii_letters to shift my translation, which is &#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;. The letter w may be encoded to be a A by a shift of 4, but the decoding will return the lowercase w. There was an error in my exception handling, which is now corrected.&lt;br /&gt;&lt;script src=&quot;https://gist.github.com/728793.js&quot;&gt; &lt;/script&gt;</description><link>http://housewifehacker.blogspot.com/2010/12/maketrans-python-and-improved-caesars.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-2275399180443830219</guid><pubDate>Sat, 13 Nov 2010 08:07:00 +0000</pubDate><atom:updated>2010-12-04T21:06:15.872-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">ascii</category><category domain="http://www.blogger.com/atom/ns#">caesar&#39;s shift</category><category domain="http://www.blogger.com/atom/ns#">chr</category><category domain="http://www.blogger.com/atom/ns#">ord</category><category domain="http://www.blogger.com/atom/ns#">python</category><category domain="http://www.blogger.com/atom/ns#">thepythonchallenge.com</category><title>Caesar Cipher in Python Using ASCII</title><description>ASCII is how American computers store numbers, letters, certain commands, and symbols as numbers. A binary byte is eight digits long, consisting of only 1 and 0. When ASCII was developed, there were 2^8, or 256 possible characters for 8-bit (1 byte) personal computers. Unicode is used more commonly and extensively, but ASCII is sufficient or this exercise. Consult an &lt;a href=&quot;http://www.asciitable.com/&quot;&gt;ASCII Table&lt;/a&gt; or play with python&#39;s default functions to determine the encoding of a character such as &#39;a&#39;, &#39;A&#39; or &#39;&amp;&#39;.&lt;br /&gt;&lt;br /&gt;In python, we can find the ascii value using &lt;b&gt;ord&lt;/b&gt; of a string&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; ord(&#39;a&#39;)&lt;br /&gt;97&lt;br /&gt;&gt;&gt;&gt; ord(&#39;A&#39;)&lt;br /&gt;65&lt;br /&gt;&gt;&gt;&gt; ord(&#39;b&#39;)&lt;br /&gt;98&lt;br /&gt;&gt;&gt;&gt; ord(&#39;1&#39;)&lt;br /&gt;49&lt;br /&gt;&gt;&gt;&gt; ord(&#39;2&#39;)&lt;br /&gt;50&lt;br /&gt;&gt;&gt;&gt; 3-0&lt;br /&gt;3&lt;br /&gt;&gt;&gt;&gt; ord(&#39;3&#39;)-ord(&#39;0&#39;)&lt;br /&gt;3&lt;/pre&gt;&lt;br /&gt;Notice how the lower case and upper case are unique. Also, the ASCII numeric value and the character increase by the same amount. The same trend is true for the continuation of the alphabet, if lower case or upper case characteristic is preserved. The string length must be 1 when using ord, because each index has its own ASCII value.For a multiple digit number or a word, you can use a for loop to go through the string.&lt;br /&gt;&lt;br /&gt;The reverse of ord is &lt;b&gt;chr&lt;/b&gt;, for character. &lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; chr(97)&lt;br /&gt;&#39;a&#39;&lt;br /&gt;&gt;&gt;&gt; i=97&lt;br /&gt;&gt;&gt;&gt; while i&lt;104:&lt;br /&gt;...   print chr(i)&lt;br /&gt;...   i+=1&lt;br /&gt;... &lt;br /&gt;a&lt;br /&gt;b&lt;br /&gt;c&lt;br /&gt;d&lt;br /&gt;e&lt;br /&gt;f&lt;br /&gt;g&lt;br /&gt;&gt;&gt;&gt; alpha=[]&lt;br /&gt;&gt;&gt;&gt; i=97&lt;br /&gt;&gt;&gt;&gt; while i&lt;104:&lt;br /&gt;...   alpha.append(chr(i))&lt;br /&gt;...   i+=1&lt;br /&gt;... &lt;br /&gt;&gt;&gt;&gt; print alpha&lt;br /&gt;[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Caesar&#39;s shift was how Caesar communicated with his generals, in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, if an &#39;a&#39; is encoded by a &#39;c&#39; with a shift of 2, then a &#39;b&#39; would be encoded by a &#39;d.&#39; Reversely, an &#39;a&#39; in the encoded text would be a &#39;y&#39; in the plaintext. The following program was inspired by thepythonchallenge.com. My husband and I had a lot of fun testing and debugging by sending each other messages over email. The code only changes letters so that spaces, smilies, brackets, and other punctuations and characters will be preserved. string.maketrans would be very useful and less code for a one time application, but the following code is highly reusable.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#string will later allow us to test for ascii letters&lt;br /&gt;import string&lt;br /&gt;&lt;br /&gt;def encode(input,shift):&lt;br /&gt;  #create an empty string&lt;br /&gt;  foo = &#39;&#39;&lt;br /&gt;  #a for loop to shift the letters by the shift input &lt;br /&gt;  for x in input:&lt;br /&gt;    #changes only ascii letters&lt;br /&gt;    if x in string.ascii_letters:&lt;br /&gt;      #upper case end of alphabet, to loop back to A&lt;br /&gt;      if ord(x) &gt; ord(&quot;Z&quot;) - shift and ord(x) &lt;= ord(&quot;Z&quot;):&lt;br /&gt;        new_ord = ord(x) + shift - 26&lt;br /&gt;      #lower case end of alphabet&lt;br /&gt;      elif ord(x) &gt; ord(&quot;z&quot;) - shift and ord(x) &lt;= ord(&quot;z&quot;):&lt;br /&gt;        new_ord = ord(x) + shift - 26&lt;br /&gt;      else:&lt;br /&gt;        new_ord = ord(x) + shift&lt;br /&gt;    #other characters will remain unchanged&lt;br /&gt;    else:&lt;br /&gt;      new_ord = ord(x)&lt;br /&gt;&lt;br /&gt;    new_chr = chr(new_ord)&lt;br /&gt;    foo += new_chr&lt;br /&gt;&lt;br /&gt;  print &quot;Your encoded message is &quot;, foo&lt;br /&gt;&lt;br /&gt;def decode(input,shift):&lt;br /&gt;  #create an empty string&lt;br /&gt;  foo = &#39;&#39;&lt;br /&gt;  #a for loop to shift the leters by the shift input &lt;br /&gt;  for x in input:&lt;br /&gt;    #changes only ascii letters&lt;br /&gt;    if x in string.ascii_letters:&lt;br /&gt;      #upper case start of alphabet, to loop back to Z&lt;br /&gt;      if ord(x) - shift &lt; ord(&quot;A&quot;)  and ord(x) &gt;= ord(&quot;A&quot;):&lt;br /&gt;        new_ord = ord(x) - shift + 26&lt;br /&gt;      #lower case end of alphabet&lt;br /&gt;      elif ord(x) - shift &lt; ord(&quot;a&quot;) and ord(x) &gt;= ord(&quot;a&quot;):&lt;br /&gt;        new_ord = ord(x) - shift + 26&lt;br /&gt;      else:&lt;br /&gt;        new_ord = ord(x) - shift&lt;br /&gt;    #other characters will remain unchanged&lt;br /&gt;    else:&lt;br /&gt;      new_ord = ord(x)&lt;br /&gt;    new_chr = chr(new_ord)&lt;br /&gt;    foo += new_chr&lt;br /&gt;  print &quot;Your decoded message is &quot;, foo&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;def get_shift():&lt;br /&gt;  try:&lt;br /&gt;    shift = int(raw_input(&quot;How many characters do you want to\&lt;br /&gt; shift?\n&quot;))&lt;br /&gt;  except:&lt;br /&gt;    print &#39;Shift must be a number&#39;&lt;br /&gt;    shift = get_shift()&lt;br /&gt;  if not (shift &gt; 0 and shift &lt;= 25):&lt;br /&gt;    print &#39;Shift must be between 1 and 25&#39;&lt;br /&gt;    shift = get_shift()&lt;br /&gt;  return shift&lt;br /&gt;&lt;br /&gt;def main():&lt;br /&gt;  try:&lt;br /&gt;    action=raw_input(&quot;Welcome to Caeser&#39;s Shifter! Would you \&lt;br /&gt;like to encode or decode a message today?\n&quot;)&lt;br /&gt;  except:&lt;br /&gt;    print &#39;Please type encode or decode&#39;&lt;br /&gt;    main()&lt;br /&gt;  shift = get_shift()&lt;br /&gt;&lt;br /&gt;  print &quot;Ok, let&#39;s %s your message with a shift of %d.&quot; % (action, shift)&lt;br /&gt;  input=raw_input(&quot;What would you like to be translated?\n&quot;)&lt;br /&gt;&lt;br /&gt;  if action==&#39;encode&#39;:&lt;br /&gt;    encode(input,shift)&lt;br /&gt;  elif action==&#39;decode&#39;:&lt;br /&gt;    decode(input,shift)&lt;br /&gt;&lt;br /&gt;if __name__ == &quot;__main__&quot;:&lt;br /&gt;  main()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;There are probably a few areas where redundancy can be reduced, but I am still learning as a developer. This was my first time trying exception handling, which I learned from my husband.</description><link>http://housewifehacker.blogspot.com/2010/10/caesar-cipher-in-python-using-ascii.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>8</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-1769840239067825970</guid><pubDate>Tue, 02 Nov 2010 23:24:00 +0000</pubDate><atom:updated>2010-12-04T21:07:17.370-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">python</category><category domain="http://www.blogger.com/atom/ns#">string formatting</category><title>&#39; \ &#39; Escapes in Python</title><description>I have previously mentioned that &#39;\n&#39; codes for a new line. The backslash is known in Python as an escape. &lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; print &quot;\tThis is a tabbed line&quot;&lt;br /&gt; This is a tabbed line&lt;br /&gt;&gt;&gt;&gt; print &quot;This sentence \nis split on \nthree lines&quot;&lt;br /&gt;This sentence &lt;br /&gt;is split on &lt;br /&gt;three lines&lt;br /&gt;&gt;&gt;&gt; print &quot;Type \\ for a backslash&quot;&lt;br /&gt;Type \ for a backslash&lt;br /&gt;&gt;&gt;&gt; print &quot;Verticle \vTab&quot;&lt;br /&gt;Verticle &lt;br /&gt;         Tab&lt;br /&gt;&gt;&gt;&gt; print &quot;Shopping List: \vPrenatal vitamins \vCocoa Butter \&lt;br /&gt;... \vOrange juice&quot;&lt;br /&gt;Shopping List: &lt;br /&gt;               Prenatal vitamins &lt;br /&gt;                                 Cocoa Butter &lt;br /&gt;                                              Orange juice&lt;br /&gt;&gt;&gt;&gt; print &quot;Shopping List: \n\t*Prenatal vitamins \  &lt;br /&gt;...\n\t*Cocoa Butter \n\t*Orange juice&quot;&lt;br /&gt;Shopping List: &lt;br /&gt; *Prenatal vitamins &lt;br /&gt; *Cocoa Butter &lt;br /&gt; *Orange juice&lt;br /&gt;&gt;&gt;&gt; print &#39;Prevent the single quote 5\&#39;5&quot; from ending the string&#39;&lt;br /&gt;Prevent the single quote 5&#39;5&quot; from ending the string&lt;br /&gt;&gt;&gt;&gt; print &quot;Prevent the double quote 5&#39;5\&quot; from ending the string&quot;&lt;br /&gt;Prevent the double quote 5&#39;5&quot; from ending the string&lt;/pre&gt;&lt;br /&gt;Notice that I also used a backslash in my string so that I could &lt;b&gt;continue&lt;/b&gt; onto the next ine without Python thinking my string was done. There are &lt;a href=&quot;http://docs.python.org/release/2.5.2/ref/strings.html&quot;&gt;other escapes in Python&lt;/a&gt; you can read about. &quot;Carriage return&quot; is a term from type writers to mean that you move to the beginning of a line. Linefeed and formfeed are also typewriter terms. \n and \t will be the most frequently used, though you may also want to be familiar with other escapes for unique problems.</description><link>http://housewifehacker.blogspot.com/2010/11/escapes-in-python.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-6923076168372504472</guid><pubDate>Mon, 25 Oct 2010 20:18:00 +0000</pubDate><atom:updated>2010-10-28T12:47:03.766-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">python</category><title>Guess the Number Game Python</title><description>With inspiration from &lt;a href=&quot;http://inventwithpython.com/chapter4.html&quot;&gt;InventWithPython.com&lt;/a&gt;, I wrote a game to guess a number between 1 and 20 with six attempts. This simple game is good practice for a beginner. My code is written in Python 2.6. The example from the link is written in Python 3 syntax.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;import random&lt;br /&gt;&lt;br /&gt;guessesTaken = 0&lt;br /&gt;&lt;br /&gt;print &#39;Hello! What is your name?&#39;&lt;br /&gt;myName = raw_input() #get user name&lt;br /&gt;&lt;br /&gt;number = random.randint(1,20) #random number between 1 and 20&lt;br /&gt;print &#39;%s I am thinking of a number between 1 and 20.&#39; % myName&lt;br /&gt;&lt;br /&gt;while guessesTaken &lt; 6:&lt;br /&gt;  print &#39;Take a guess.&#39;&lt;br /&gt;  guess = int(raw_input())&lt;br /&gt;  guessesTaken += 1&lt;br /&gt;  if guess &lt; number:&lt;br /&gt;    print &#39;Your guess is too low.&#39;&lt;br /&gt;  elif guess &gt; number:&lt;br /&gt;    print &#39;Your guess is too high.&#39;&lt;br /&gt;  else:&lt;br /&gt;    break #breaks out of loop&lt;br /&gt;&lt;br /&gt;if guess == number:&lt;br /&gt;  if guessesTaken == 1:&lt;br /&gt;    print &#39;Good Job, %s! You guessed my number in 1 guess!&#39;&lt;br /&gt;  else:&lt;br /&gt;    print &#39;Good Job, %s! You guessed my number in %d guesses!&#39; % \&lt;br /&gt;(myName,guessesTaken) #actual format on previous line without &#39;\&#39;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if guess != number:&lt;br /&gt;  print &#39;No. The number I was thinking of was %d&#39; % number&lt;br /&gt;&lt;/pre&gt;</description><link>http://housewifehacker.blogspot.com/2010/10/guess-number-game-python.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-1961273169499507443</guid><pubDate>Mon, 25 Oct 2010 03:50:00 +0000</pubDate><atom:updated>2010-10-24T20:52:38.249-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">python</category><title>Python Debugger Article by Sontek</title><description>My husband wrote a wonderful blog post about debugging Python. Read it &lt;a href=http://sontek.net/debugging-python-with-pdb&gt;here&lt;/a&gt;.</description><link>http://housewifehacker.blogspot.com/2010/10/python-debugger-article-by-sontek.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-1002952097510516981</guid><pubDate>Mon, 25 Oct 2010 01:09:00 +0000</pubDate><atom:updated>2010-12-04T21:08:23.991-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">python</category><category domain="http://www.blogger.com/atom/ns#">string formatting</category><title>String Formatting in Python</title><description>An easy way to combine strings with non strings is to use commas:&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt;&gt;&gt;&gt; print &#39;The value of pi is often rounded to&#39;,3.14&lt;br /&gt;The value of pi is often rounded to 3.14&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;But what if you want to create a website that welcomes guests by their name after he or she logs in? What if you are pulling information from a list or a dictionary? For personalization or multiple use functionality, we will be using string formatting. Below are the different format characters. There are a few others, but they aren&#39;t necessary. &lt;br /&gt;&lt;pre&gt;%s   string&lt;br /&gt;%d   integers or decimals, but returns floor integer&lt;br /&gt;%r   anything that is not a string, converts object to a string&lt;br /&gt;%f   floating point, so we can control precision after decimal&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Here is an example of using each of the formats:&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; name=&#39;Anderson Silva&#39;&lt;br /&gt;&gt;&gt;&gt; weight= 105 #integer&lt;br /&gt;&gt;&gt;&gt; height= 1.92 #decimal, but I only want 1.9 displayed&lt;br /&gt;&gt;&gt;&gt; home= &#39;Brazil&#39; #string, watch what happens with %r &lt;br /&gt;&gt;&gt;&gt; fighter1=&#39;&#39;&#39;%s&lt;br /&gt;... Nationality: %r&lt;br /&gt;... Weight: %d kg&lt;br /&gt;... Height %.1f m&#39;&#39;&#39; % (name,home,weight,height) #in order &lt;br /&gt;&gt;&gt;&gt; print fighter1&lt;br /&gt;Anderson Silva&lt;br /&gt;Nationality: &#39;Brazil&#39; #%r leaves quotes around strings&lt;br /&gt;Weight: 105 kg&lt;br /&gt;Height 1.9 m&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Here is an example converting centimetres to inches&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; def convert(cm):&lt;br /&gt;...  inches=cm*.39370&lt;br /&gt;...  print &#39;There are %.2f inches in %d centimetres&#39; % (inches,cm)&lt;br /&gt;... &lt;br /&gt;&gt;&gt;&gt; convert(10)&lt;br /&gt;There are 3.94 inches in 10 centimetres &lt;br /&gt;&gt;&gt;&gt;#two places after decimal are shown&lt;br /&gt;&gt;&gt;&gt; convert(3.544445)&lt;br /&gt;There are 1.40 inches in 3 centimetres&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;%f has a default to print six numbers after the decimal. By adding &#39;.1&#39;, &#39;.2&#39;, &#39;.3&#39;, etc... between the &#39;%&#39; and &#39;f&#39;, you determine how many numbers are visible after the decimal. You can also add &#39;+&#39; or &#39;-&#39; before the &#39;.x&#39;, such as showing a change &lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; print &quot;Today&#39;s stock price changed %+.2f&quot; % 1.4888&lt;br /&gt;Today&#39;s stock price changed +1.49&lt;br /&gt;&lt;/pre&gt;</description><link>http://housewifehacker.blogspot.com/2010/10/string-formatting-in-python-r.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-3997225758692397704</guid><pubDate>Fri, 22 Oct 2010 16:51:00 +0000</pubDate><atom:updated>2010-12-04T21:09:05.801-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">decimals</category><category domain="http://www.blogger.com/atom/ns#">math</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Decimal Library for Changing Number Precision in Python</title><description>The built in math functions in Python use binary approximations, giving some funky results when dealing with numbers containing decimals:&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; .1+.2&lt;br /&gt;0.30000000000000004&lt;br /&gt;&gt;&gt;&gt; round(100.00/3.000,4)&lt;br /&gt;33.333300000000001&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;One way to appropriately find the sum of decimals is to use strings&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; str(.1+.2)&lt;br /&gt;&#39;0.3&#39;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Also, the default is to round to the nearest whole number when dividing&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; 1/3&lt;br /&gt;0&lt;br /&gt;&gt;&gt;&gt; 100/3&lt;br /&gt;33&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The decimal library is a useful tool for floating point arithmetic. Instead of the command &#39;from decimal import *&#39; that would import everything from decimal, all I need to import is Decimal and getcontext. When importing modules, simplicity is preferred. There are less problems with naming in your code and you can be more aware of the tools at your disposal. I already imported decimal and looked through the directory to determine which modules I wanted. I&#39;m only going to show the precision feature of decimal. You may want to import the entire library if you want to use other functions.&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; from decimal import getcontext&lt;br /&gt;&gt;&gt;&gt; from decimal import Decimal&lt;br /&gt;&gt;&gt;&gt; getcontext()&lt;br /&gt;Context(prec=28, rounding=ROUND_HALF_UP, Emin=-999999999, &lt;br /&gt;Emax=999999999, capitals=1, flags=[Inexact, Rounded], &lt;br /&gt;traps=[DivisionByZero, Overflow, InvalidOperation])&lt;br /&gt;&gt;&gt;&gt; #our precision is also known as significant figures, &lt;br /&gt;applied after arithmetic &lt;br /&gt;... #let&#39;s change our precision&lt;br /&gt;... &lt;br /&gt;&gt;&gt;&gt; getcontext().prec=6&lt;br /&gt;&gt;&gt;&gt; Decimal(&#39;1&#39;)/Decimal(&#39;7&#39;) #can be performed to strings&lt;br /&gt;Decimal(&#39;0.142857&#39;)&lt;br /&gt;&gt;&gt;&gt; Decimal(1)/Decimal(7) #can be performed to integers&lt;br /&gt;Decimal(&#39;0.142857&#39;)&lt;br /&gt;&gt;&gt;&gt; Decimal(10)/Decimal(7)&lt;br /&gt;Decimal(&#39;1.42857&#39;) #notice that 6 is the total number of &lt;br /&gt;figures, not the number after the decimal&lt;br /&gt;&gt;&gt;&gt; Decimal(10)/Decimal(5)&lt;br /&gt;Decimal(&#39;2&#39;) #not &#39;2.00000,&#39; which is considered more &lt;br /&gt;accurate than 2 by the science community&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;As someone with a science background, I found the decimal library&#39;s use of &#39;significant figures&#39; interesting. Decimal can also be used in financial reporting or billing. You can also find maximums and minimums, change rounding properties, and do anything that you can do with the math library. I personally prefer the math library for the algebraic functions performed by decimal, because math&#39;s syntax is simpler. To learn more about decimal, click &lt;a href=&quot;http://docs.python.org/library/decimal.html&quot;&gt; here &lt;/a&gt;.</description><link>http://housewifehacker.blogspot.com/2010/10/decimal-library-for-changing-number.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-6332825096700999387</guid><pubDate>Thu, 21 Oct 2010 02:35:00 +0000</pubDate><atom:updated>2010-12-04T21:09:42.532-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">map</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Using Map Function in Python</title><description>Today I discovered the map function in Python. Map causes some simple for loops to be verbose and unnecessary. Let&#39;s look at how to change a list of integers to a list of strings. First we&#39;ll use a for loop:&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; list=[1,2,3,4,5]&lt;br /&gt;&gt;&gt;&gt; index=0&lt;br /&gt;&gt;&gt;&gt; for x in list:&lt;br /&gt;...   list[index]=str(list[index])&lt;br /&gt;...   index+=1&lt;br /&gt;... &lt;br /&gt;&gt;&gt;&gt; print list&lt;br /&gt;[&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now we&#39;ll use the map function and we&#39;ll define our 1-5 list using the range function&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; list1=range(1,6)&lt;br /&gt;&gt;&gt;&gt; map(str,list1) #performs str function to every index of list1&lt;br /&gt;[&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;] &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You can also make your list within the map function, such as splitting a string into a list. The following example shows how you can define a method using else, elif (else if), and if statements, then run your method to a single string with the map function, resulting in a list with the method performed to each index.&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; def pluralize(word):&lt;br /&gt;...   if word[-1]==&#39;y&#39;:&lt;br /&gt;...     return word[:len(word)-1]+&#39;ies&#39; #replaces the y with ies&lt;br /&gt;...   elif word[-1]==&#39;s&#39;:&lt;br /&gt;...     return word+&#39;es&#39;&lt;br /&gt;...   else:&lt;br /&gt;...     return word+&#39;s&#39;&lt;br /&gt;... &lt;br /&gt;&gt;&gt;&gt; map(pluralize, &quot;The sexy waitress brought me a beer&quot;.split()) &lt;br /&gt;[&#39;Thes&#39;, &#39;sexies&#39;, &#39;waitresses&#39;, &#39;broughts&#39;, &#39;mes&#39;, &#39;as&#39;, &#39;beers&#39;]&lt;br /&gt;&gt;&gt;&gt;#my husband chose the sentence&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I simplified my method to not account for every scenario put into it. Although this specific example does not produce correct English, the map function correctly split the string on the whitespace and carried out the method on each index of the list.</description><link>http://housewifehacker.blogspot.com/2010/10/using-map-function-in-python.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-3890786026848526346</guid><pubDate>Mon, 18 Oct 2010 23:10:00 +0000</pubDate><atom:updated>2010-10-24T17:55:51.617-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">python</category><title>Indexes, Strings and Lists in Python</title><description>A Python string&#39;s properties can be best understood by using sentences and words, although numbers can also be stored as strings. Be careful not to name a string as &#39;str&#39; or &#39;string&#39;, because these are built in functions in Python. Let&#39;s play with some basic functions you can do to strings&lt;br /&gt;&lt;pre&gt;[gypsychemist@inspidell ~]$ python&lt;br /&gt;Python 2.6.4 (r264:75706, Jun  4 2010, 18:20:16) &lt;br /&gt;[GCC 4.4.4 20100503 (Red Hat 4.4.4-2)] on linux2&lt;br /&gt;Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more &lt;br /&gt;information.&lt;br /&gt;&gt;&gt;&gt; sentence1=&#39;This is two strings &#39;&lt;br /&gt;&gt;&gt;&gt; sentence2=&#39;combined.&#39;&lt;br /&gt;&gt;&gt;&gt; sentence1+sentence2&lt;br /&gt;&#39;This is two strings combined.&#39;&lt;br /&gt;&gt;&gt;&gt; s=sentence1+sentence2&lt;br /&gt;&gt;&gt;&gt; print s&lt;br /&gt;This is two strings combined.&lt;br /&gt;&gt;&gt;&gt; r=&#39;&#39;&#39;Three quotes will&lt;br /&gt;... preserve the formatting&lt;br /&gt;... for multiple lines&#39;&#39;&#39;&lt;br /&gt;&gt;&gt;&gt; print r&lt;br /&gt;Three quotes will&lt;br /&gt;preserve the formatting&lt;br /&gt;for multiple lines &lt;br /&gt;&gt;&gt;&gt; s.split()&lt;br /&gt;[&#39;This&#39;, &#39;is&#39;, &#39;two&#39;, &#39;strings&#39;, &#39;combined.&#39;]&lt;br /&gt;&gt;&gt;&gt; r.split(&#39;e&#39;)&lt;br /&gt;[&#39;Thr&#39;, &#39;&#39;, &#39; quot&#39;, &#39;s will\npr&#39;, &#39;s&#39;, &#39;rv&#39;, &#39; th&#39;, &lt;br /&gt;&#39; formatting\nfor multipl&#39;, &#39; lin&#39;, &#39;s&#39;]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The split command divides a string into a list based on the delimiter you define. In the example of splitting the s string, leaving the delimiter blank causes splitting on all white spaces (tab, enter, space). Anything can be the delimiter, such as using &#39;e&#39; in the splitting of r example. &#39;\n&#39; is a new line. &lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; p=&quot;  The extra white space on the ends     &quot;&lt;br /&gt;&gt;&gt;&gt; p.strip()+&#39; is removed by the strip function&#39;&lt;br /&gt;&#39;The extra white space on the ends is removed by the &lt;br /&gt;strip function&#39;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Indexes are useful for both strings and lists. A common graphic to explain indexes is:&lt;br /&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE0FZacFaOjabPXoxPEpuuKLfLcSSes8C7n8uj8hWGv1tUzwYq8ZTjs_RlZJOaTRmwf1PTscpDQdFlPdNaQifN1CBvj3ZlR4R6ppm1K4odNWpgrW_l1vf_HyqtvZfhdHqUf36Y7HLRO5w/s1600/hello.png&quot;&gt;&lt;img style=&quot;display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 200px; height: 112px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE0FZacFaOjabPXoxPEpuuKLfLcSSes8C7n8uj8hWGv1tUzwYq8ZTjs_RlZJOaTRmwf1PTscpDQdFlPdNaQifN1CBvj3ZlR4R6ppm1K4odNWpgrW_l1vf_HyqtvZfhdHqUf36Y7HLRO5w/s200/hello.png&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5529531961245833618&quot; /&gt;&lt;/a&gt;&lt;br /&gt;The letters or numbers in a string begin with the index 0, or you can work from the back with index of -1. When selecting a range of indexes, the last number is excluded. &lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; f=&#39;hello&#39;&lt;br /&gt;&gt;&gt;&gt; f[0:2]&lt;br /&gt;&#39;he&#39;&lt;br /&gt;&gt;&gt;&gt; f[:2] #a starting index of 0 is assumed&lt;br /&gt;&#39;he&#39;&lt;br /&gt;&gt;&gt;&gt; f[2]&lt;br /&gt;&#39;l&#39;&lt;br /&gt;&gt;&gt;&gt; f[-2:] #completing until the end is assumed&lt;br /&gt;&#39;lo&#39;&lt;br /&gt;&gt;&gt;&gt; f[-2:-1]&lt;br /&gt;&#39;l&#39;&lt;br /&gt;&gt;&gt;&gt; f[:2]+f[-2:]&lt;br /&gt;&#39;helo&#39;&lt;br /&gt;&gt;&gt;&gt; list=[0,1,2,3,4,5]&lt;br /&gt;&gt;&gt;&gt; list[1]&lt;br /&gt;1&lt;br /&gt;&gt;&gt;&gt; list[-2:0] #doesn&#39;t work&lt;br /&gt;[]&lt;br /&gt;&gt;&gt;&gt; list[-2:]&lt;br /&gt;[4, 5]&lt;br /&gt;&gt;&gt;&gt; list[:1]&lt;br /&gt;[0]&lt;br /&gt;&gt;&gt;&gt; &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&#39;#&#39; is used in Python to write comments. They are ignored in programs and are very useful to solve problems, either to write out a plan or to type in a literal translation of how you expect your code to perform. If your code does not work correctly, you can go back through your notes to quickly find where your logic or syntax may be incorrect.&lt;br /&gt;&lt;br /&gt;As I mentioned briefly in the beginning, a number can also be a string. If a number is a string, it will be used a returned with quotation marks surrounding it. In Python, single quotes and double quotes act the same; however, the opening and closing ends need to be the same. The example below shows why you may want to change a number to a string:&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; pi=3.14&lt;br /&gt;&gt;&gt;&gt; # cannot add an integer to a string&lt;br /&gt;&gt;&gt;&gt; print &#39;The value of pi is often rounded to &#39; + pi &lt;br /&gt;Traceback (most recent call last):&lt;br /&gt;  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;&lt;br /&gt;TypeError: cannot concatenate &#39;str&#39; and &#39;float&#39; objects&lt;br /&gt;&gt;&gt;&gt; #so we change pi to a string&lt;br /&gt;&gt;&gt;&gt;print &#39;The value of pi is often rounded to &#39; + str(pi) &lt;br /&gt;The value of pi is often rounded to 3.14&lt;br /&gt;&gt;&gt;&gt; #you can include a number after a string by using a comma&lt;br /&gt;&gt;&gt;&gt;print &#39;The value of pi is often rounded to&#39;,3.14&lt;br /&gt;The value of pi is often rounded to 3.14&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;You can change a list of numbers to be a list of strings by using a for loop.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; list=[1,2,3,4,5]&lt;br /&gt;&gt;&gt;&gt; str(list[1]) #returns the list index as a string without &lt;br /&gt;changing the list&lt;br /&gt;&#39;2&#39;&lt;br /&gt;&gt;&gt;&gt; list #list is still composed of integers&lt;br /&gt;[1, 2, 3, 4, 5]&lt;br /&gt;&gt;&gt;&gt; index = 0 #we&#39;re setting a variable to start at 0 because&lt;br /&gt; index starts at 0&lt;br /&gt;&gt;&gt;&gt; for x in list: #begins with index 0&lt;br /&gt;...   list[index] = str(list[index]) #assigns index 0 integer as &lt;br /&gt;a string of the previous list[0]&lt;br /&gt;...   index += 1 #after the previous command is performed on [0],&lt;br /&gt; it will be performed on [1]&lt;br /&gt;...   #for loop continues through indexes 1 to 4 after index 0&lt;br /&gt;&gt;&gt;&gt; list #our list is now changed to be strings&lt;br /&gt;[&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now lets play with some list commands that are nondiscriminate against strings or integers:&lt;br /&gt;&lt;pre&gt;&gt;&gt;&gt; list1=[&#39;8&#39;,&#39;9&#39;,&#39;10&#39;]&lt;br /&gt;&gt;&gt;&gt; list.extend(list1) #add list1 to end of list, list+list1&lt;br /&gt;&gt;&gt;&gt; print list&lt;br /&gt;[&#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;10&#39;]&lt;br /&gt;&gt;&gt;&gt; list.insert(2,&#39;3&#39;) #inserts &#39;3&#39; at index 2&lt;br /&gt;&gt;&gt;&gt; print list&lt;br /&gt;[&#39;3&#39;, &#39;4&#39;, &#39;3&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;10&#39;]&lt;br /&gt;&gt;&gt;&gt; list.insert(2,3) #now we&#39;ll insert an integer&lt;br /&gt;&gt;&gt;&gt; print list #list can have combination of strings &amp; non strings&lt;br /&gt;[&#39;3&#39;, &#39;4&#39;, 3, &#39;3&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;10&#39;]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;For more documentation and to try writing example programs, visit Google&#39;s Python classroom: http://code.google.com/edu/languages/google-python-class/index.html</description><link>http://housewifehacker.blogspot.com/2010/10/indexes-strings-and-lists-in-python.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE0FZacFaOjabPXoxPEpuuKLfLcSSes8C7n8uj8hWGv1tUzwYq8ZTjs_RlZJOaTRmwf1PTscpDQdFlPdNaQifN1CBvj3ZlR4R6ppm1K4odNWpgrW_l1vf_HyqtvZfhdHqUf36Y7HLRO5w/s72-c/hello.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2908934836353501557.post-2799819787914730904</guid><pubDate>Mon, 18 Oct 2010 18:16:00 +0000</pubDate><atom:updated>2010-10-24T17:59:29.867-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">math</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Math with Python</title><description>If this is your first time using Python in Windows, you will have to download it from python.org. Every operating system includes a terminal, which we will be using to practice and test. Google &quot;How to open terminal&quot; and your operating system name to find specific information about opening your terminal. You will also need either VIM, WING, Eclipse, or another editor to save and run your programs, though we do not need to use it for this tutorial. This article will focus on math capabilities within Python, either performing calculations as a common calculator or within logic commands. &lt;b&gt;So open your terminal and play with me&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;I start my code with telling my terminal that I would be working in python. [gypsychemist@inspidell ~]$ is my shell prompt, &quot;python&quot; is what I typed, and the next three lines are showing that python is installed and open for me to use.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;[gypsychemist@inspidell ~]$ python&lt;br /&gt;Python 2.6.4 (r264:75706, Jun  4 2010, 18:20:16) &lt;br /&gt;[GCC 4.4.4 20100503 (Red Hat 4.4.4-2)] on linux2&lt;br /&gt;Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more&lt;br /&gt;information.&lt;br /&gt;&gt;&gt;&gt; 3+2&lt;br /&gt;5&lt;br /&gt;&gt;&gt;&gt; 3*2&lt;br /&gt;6&lt;br /&gt;&gt;&gt;&gt; 6/2&lt;br /&gt;3&lt;br /&gt;&gt;&gt;&gt; 3-2&lt;br /&gt;1&lt;br /&gt;&gt;&gt;&gt; 3*(3-1)&lt;br /&gt;6 &lt;br /&gt;&gt;&gt;&gt; pi=3.14&lt;br /&gt;&gt;&gt;&gt; 3*pi&lt;br /&gt;9.4199999999999999&lt;br /&gt;&gt;&gt;&gt; abs(2-3)&lt;br /&gt;1&lt;br /&gt;&gt;&gt;&gt; pow(3,2)&lt;br /&gt;9&lt;br /&gt;&gt;&gt;&gt; 3**2&lt;br /&gt;9&lt;br /&gt;&gt;&gt;&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Basic math commands are shown above. &#39;+&#39; is for addition, &#39;-&#39; for subtraction. &#39;*&#39; for multiplication, and &#39;/&#39; for division. The basic math rules regarding parenthesis and order of operations are followed correctly. You can also store numbers as words or letters to perform math equations on, such as the example with pi. In Python, &#39;=&#39; is used to assign and does not imply equivalency. The absolute value function and two alternative ways to find a number with an exponent (also known as power) are shown. &lt;br /&gt;&lt;br /&gt;A useful tool in python is the &lt;a href= &quot;http://docs.python.org/library/math.html&quot;&gt; math library&lt;/a&gt;. To see the functions you can use within math, type &#39;import math&#39;&lt;Enter&gt; and &#39;dir(math)&#39; on the next line. You just told the terminal you are opening the library and viewing the directory. The terminal will show you the commands including different trigonometric functions, advanced algebra, and rounding options. To learn more about a command (such as floor rounding), you can type &#39;help(math.floor)&#39;. Press &#39;q&#39; to exit help. &lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; import math&lt;br /&gt;&gt;&gt;&gt; dir(math)&lt;br /&gt;[&#39;__doc__&#39;, &#39;__file__&#39;, &#39;__name__&#39;, &#39;__package__&#39;, &#39;acos&#39;, &#39;acosh&#39;, &lt;br&gt;&#39;asin&#39;, &#39;asinh&#39;, &#39;atan&#39;, &#39;atan2&#39;, &#39;atanh&#39;, &#39;ceil&#39;, &#39;copysign&#39;,&#39;cos&#39;, &lt;br&gt;&#39;cosh&#39;, &#39;degrees&#39;, &#39;e&#39;, &#39;exp&#39;, &#39;fabs&#39;, &#39;factorial&#39;, &#39;floor&#39;, &#39;fmod&#39;, &lt;br&gt;&#39;frexp&#39;, &#39;fsum&#39;, &#39;hypot&#39;, &#39;isinf&#39;, &#39;isnan&#39;, &#39;ldexp&#39;, &#39;log&#39;, &#39;log10&#39;, &lt;br&gt;&#39;log1p&#39;, &#39;modf&#39;, &#39;pi&#39;, &#39;pow&#39;, &#39;radians&#39;, &#39;sin&#39;, &#39;sinh&#39;, &#39;sqrt&#39;, &lt;br&gt;&#39;tan&#39;, &#39;tanh&#39;, &#39;trunc&#39;]&lt;br /&gt;&gt;&gt;&gt; help(math.floor)&lt;br /&gt;&gt;&gt;&gt; math.floor(4.5)&lt;br /&gt;4.0&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You can also test to determine if one number is divisible by another, such as when you want to execute commands on only the even indexes in a list. To determine if 3 is evenly divisible by 2, type &#39;3 %2&#39;. Python returns &#39;1&#39; because there is a remainder of 1 when you divide 3 by 2. If 0 is a result, then numbers are evenly divisible. &lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; 3%2&lt;br /&gt;1&lt;br /&gt;&gt;&gt;&gt; 3%3&lt;br /&gt;0&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If given a list of numbers and you want to perform math functions such as adding the numbers together, you can use a for loop. Follow the below example:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; list=[2,3,6,7,4,6]&lt;br /&gt;&gt;&gt;&gt; sum=0&lt;br /&gt;&gt;&gt;&gt; for x in list:&lt;br /&gt;...   sum+=x&lt;br /&gt;... &lt;br /&gt;&gt;&gt;&gt; sum&lt;br /&gt;28&lt;br /&gt;&gt;&gt;&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In Python, spacing of indentions and capitalization are very important. After any statement ending with &#39;:&#39; , indent by a consistent amount (two spaces, four spaces, tab, your choice).&#39;sum+=x&#39; is the same as &#39;sum=sum+x&#39;. &#39;x&#39; is the value at the index as it goes through the list. So this code just said 0+2+3+6+7+4+6=28. Now that you understand how to do it with a basic for loop, the quicker way is to use the built in sum function. Because I used the variable &#39;sum&#39;, already, I need to first reinstate sum to its built in function.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; from __builtin__ import sum&lt;br /&gt;&gt;&gt;&gt; sum(list)&lt;br /&gt;28&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Using built in functions as variables should be avoided. As you learn more Python, you will learn safe and understandable variables to use in your code. For more information on numbers and math, you can visit http://docs.python.org/library/numeric.html</description><link>http://housewifehacker.blogspot.com/2010/10/math-with-python.html</link><author>noreply@blogger.com (Gypsy Chemist)</author><thr:total>0</thr:total></item></channel></rss>