<?xml version="1.0"?>
<!DOCTYPE content [ <!ENTITY nbsp " "> ]>
<rdf:RDF xml:base="http://snarfed.org/rdf"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:dc="http://purl.org/dc/elements/1.1/">

<rdf:Description rdf:about="http://snarfed.org">
  <dc:title> snarfed.org  </dc:title>
  <dc:description> draw group stream of consciousness </dc:description>
  <dc:creator> Ryan Barrett &lt;snarfed at ryanb dot org&gt; </dc:creator>
  <dc:language> en </dc:language>
  <dc:format> text/html </dc:format>
  <dc:rights> Copyright 2002-2007 Ryan Barrett </dc:rights>
</rdf:Description>

<rdf:Description rdf:about="http://snarfed.org/space/teaching%20beginning%20programming%20with%20Python">
  <dc:title> teaching beginning programming with Python </dc:title>
  <dc:creator> Ryan Barrett &lt;snarfed at ryanb dot org&gt; </dc:creator>
  <dc:date> 2003-01-01T05:00:00Z </dc:date>
  <dc:language> en </dc:language>
  <dc:format> text/html </dc:format>
  <dc:rights> Copyright 2002-2007 Ryan Barrett </dc:rights>

  <content>
    <p><a href="http://python.org/"><img src="/space/python-new.png" alt="/space/python-new.png" title="" /></a><a href="http://python.org/"><img src="/space/python-old.png" alt="/space/python-old.png" title="" /></a></p>

<p>I'm
a software engineer. I've been programming for a long time - in
<a href="http://www.stanford.edu/">school</a>, at <a href="http://google.com/">work</a>, and for
<a href="/space/software">fun</a>. While I was in school, I also taught computer science as
a TA for three years.</p>

<p>I TAed a <a href="http://cs107.stanford.edu/">mid-level class</a>, though, so by the time I
met the students, they didn't just know how to program, they'd also mastered
more in-depth concepts like pointers, recursion, computer architecture, and
algorithms.</p>

<p>Last Sunday, for the first time, I had the chance to teach someone to program
who'd never written a line of code before. It was very, very different from
TAing. It was also a <em>lot</em> of fun.</p>

<p>I was inspired by <a href="http://davidbau.com/">David Bau</a>'s <a href="http://davidbau.com/archives/2005/07/29/haaarg_world.html">Haaarg,
world!</a>, so I decided
to use <a href="http://python.org/">Python</a> because of its simple syntax, plain-english
keywords, and interactive shell. We started in the shell with a few <strong>print</strong>
statements.</p>

<div class='p-shadow'><pre><code>&gt;&gt;&gt; print 'hi'
hi
&gt;&gt;&gt; print 3
3
&gt;&gt;&gt; print 2 + 2
4
&gt;&gt;&gt; print 'hi', 2
hi 2
</code></pre></div>

<p><br class='clearing' />We quickly covered strings, integers, and basic arithmetic. We opened a .py file
and wrote a Python script, ran it, saw the output, and cheered. Hello, world!</p>

<div class='p-shadow'><pre><code>print 'Hello, world!'
</code></pre></div>

<p><br class='clearing' />We moved on to variables, which we thought about as little boxes in memory, with
names, that could hold things. We used <strong>input()</strong> to ask the user's name,
stored it in a variable, and said hi. We did more arithmetic, this time with
variables instead of integer constants.</p>

<p>Variables were a little tricky, but not too bad. Even better, they were the
worst of it. String operations, expressions, conditionals, loops, and other
control flow came easily. Even libraries and import statements didn't cause much
trouble.</p>

<p>Other than variables, the only speed bump was = for assignment vs. == for
equality testing. It's tough for first-timers to understand why they need to use
different operators. Guido actually mentioned this himself in one of his article
about teaching with Python. I tried to find a link, but no such luck.</p>

<p>In the end, we had a working copy of that old standard, the number guessing
game. Check it out!</p>

<div class='p-shadow'><pre><code>import random
play_again='Y'

while play_again=='Y':
    print "pick a number"
    answer=random.randint(0,100)
    guess=input()
    num_guesses=1
    while guess!=answer:
        num_guesses=num_guesses+1
        if guess&lt;=answer:
            print "higher than ", guess
        if guess&gt;=answer:
            print "lower than ", guess
        if guess&gt;=answer-2 and guess&lt;=answer+2:
            print "almost"
        guess=input()

    print "great! The correct number was ", answer
    print "your number of guesses was ", num_guesses
    print "play again? 'Y'/'N'"
    play_again=input()
</code></pre></div>

<p><br class='clearing' /></p>

  </content>

  <rdf:Seq>

<rdf:li>
<rdf:Description rdf:about="#1154026541.0">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> teaching beginning programming with Python-1 </dc:title>
  <dc:creator> Guest </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    
Nice work! Teaching a newbie to program can be a lot of fun. I know I have enjoyed it a couple of times. And using python just makes the whole thing easier. I remember learning to program in Fortran and well &#8230; it was painful at times. Do I regret it? No, but I think it says volumes that most of the good programmers I know chose easy languages like Python, Ruby and *shudder* Perl for hacking stuff together on their own time.<p class="paragraph"/>Next exercise- generate the Fibinocci sequence and maybe something fun like list comprehensions.<p class="paragraph"/>This kinda makes me think though &#8230; algorithms are fun, but kinda pointless to most people. Most people care more about data and doing fun stuff with data. By most people, I mean me. So are there any good free data sets out there for people to play with under the pretense of learning to program?<p class="paragraph"/> <i class="italic">- <span class="nobr"><a href="http://kodiak.brainymonkey.com">Dan</a></span></i>

  </content>
</rdf:Description>
</rdf:li>

  </rdf:Seq>
</rdf:Description>
</rdf:RDF>
