The FizzWife Project: Variables and Functions with a side order of Parameters

February 19, 2011 under The FizzWife Project

  >> [0] Introduction
  >> [1] Hello World
  >> [2] Variables and Functions with a Side Order of Parameters

After completing the “Hello, World!” exercise, Dena was ready to move onto the basics; variables and functions. In writing her “Hello, World!” program, she was introduced to her first function; Python’s print() function. The print() function simply directs supplied input to an output – sys.stdout (aka: the console) in our case.

As she was currently accustomed to directly passing a string literal to print(), I began by describing a few common data types:

  • strings: a series of characters (Ex: “Hello, World!”)
  • integers: whole numbers (Ex: 42)
  • floats: decimal numbers (ex: 3.14)

With her new-found knowledge of a few basic data types under her belt, variables were the next topic on the agenda. I have a habit of using analogies, and my description of variables would not be an exception. To convey the concept, I likened variables to labelled jars, thinking that it may be easier to understand rather than focusing too much on memory addressing. Jars contain a single item and that item can change over time. This led to an improved “Hello, World!” program:

message = "Hello, World!"
print(message)            # displays "Hello, World!"

The realization of the power of variables was unleashed!

message = "Hello, World!"
print(message)            # displays "Hello, World!"
message = "Hi, World!"
print(message)            # displays "Hi, World!"

Strings are fine and dandy, but numbers make the world go ’round. Keeping things simple with whole numbers, I asked her to create a variable to hold her age (actual ages have been changed to protect the innocent):

age = "22"
print(age)             # displays "22"

Hmmm, it seems ok, but is it really? We can find out by pretending it’s her birthday and incrementing her age by one year, as birthdays tend to do.

age = "22" + 1
age = age + 1
print(age)             # TypeError: Can't convert 'int' object to str implicitly

Oh noes! Now the idea of types mentioned way back at the beginning is making sense. A string of characters – two “2” characters beside eachother – can’t be added to the actual number 1 to produce any meaningful output. Would the result be a string; an integer? Answer; it would be nothing because it’s not possible. Dena takes another stab at it:

age = 22
age = age + 1
print(age)             # displays 23

Ah, that’s more like it. Describing data types to non-programmers during their first programming attempts may initially feel like a lost cause. At this point, the only differentiating factor between strings and integers in her mind is either the presense of absence of quotation marks around a value. But Dena’s a quick learner, and she begins to understand what’s going on under the hood. I ask her to display the sentence:

My name is [name] and I am [age] years old.

After rolling up her sleeves, she types out:

name = "Dena"
age = 22
message = "My name is " + name + " and I am " + age + " years old."
print(message)    # TypeError: Can't convert 'int' object to str implicitly

WTMFH?!?! (I’ll leave it to the reader to figure out what that means). There’s that error again. She’s unsure why printing her age, which is an integer, was totally doable before, but now it isn’t when she’s using it to build her sentence (which is a string). As soon as the expletive was out of her mouth, she realised that her problem was attempting to mix data types. What she didn’t know was how to convert variables data types from one type to another. And so, I explained Python’s standard str() function, and that its job is to cast variables from their data type to a string representation. So she put str() to good use:

name = "Dena"
age = 22
message = "My name is " + name + " and I am " + str(age) + " years old."
print(message)    # displays "My name is Dena and I am 22 years old."

Success!

Through the past couple of lessons, Dena’s been using a few of Python’s built-in functions, like print() and str(). She knows what the do, but hasn’t learned what they are yet. I briefly mentioned that functions are just like the functions that she saw in math class in high school; the function takes some variables (parameters) and produces a result. Using str() as an example, I explained the passed in parameter (her age variable) and str() gave her back a result (string representation of her age variable’s value). Her eyes glazed a little, but I assured her that it will all make sense soon, and she’ll even be creating her very own functions. But first, there’s one more data type that she should know about. Perhaps she can make a List of things that she’s learned thus far…

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
comments: 0 »

The FizzWife Project: Hello, World!

January 27, 2011 under The FizzWife Project

Before Dena writes her FizzBuzz implementation, I felt it necessary to explain how a computer actually works and what is involved in the creation of software using a canonical “Hello World” program as an example. I spent the first hour covering the major components within a computer, how they work together and how software makes it all tick. While drawing maniacally on a whiteboard, Dena witnessed my best attempt at describing the hardware’s inner workings of:

  • CPUs and how various architectures differ
  • storage, both primary (ex: RAM) and secondary (ex: hard disks)

After thoroughly confusing and then (hopefully) un-confusing her, the time had come for me to make sense of it all by proceeding to explain how software consists of instructions and how those instructions are executed by the hardware. At some point, I ended up broaching the subject of bits, bytes and binary arithmetic, which eventually led to talk of ASCII character tables.

At this point, she assured me that – after some additional explanation – she was indeed following along. It would seem to me that people who don’t often have to think in terms of systems might have trouble grasping this deluge of techno mumbo-jumbo within the span of an hour, so I feared she was merely humouring me. But she insisted she understood, so I’ll chalk it up more to her being a quick learner and less of me being a decent teacher.

With her newly-acquired knowledge of what those “ones and zeroes” actually mean, I then went on to the basics of software and how “in the old days”, programmers created software by feeding computers punch cards which represented the binary data and instructions that it could understand. This brought us to the first example program; “Hello World” writing in machine code:

01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100 00100001 00100000

Dena either had a look of terror, disgust or some combination thereof when she saw this. I assured her that software development had come a long way since this sort of programming was the norm, and this would be the only time she’d see something like it.

Next up was the language of the CPUs; Assembly. Harking back to the previous 60 minutes, Dena was made aware that the processors of the various architectures each had their own “readable” language that programmers could use to write software. And so “Hello World” written in x86 Assembly was ready for viewing:

       .MODEL Small
       .STACK 100h
       .DATA
   msg db 'Hello, world!$'
     .CODE
   start:
     mov ah, 09h
     lea dx, msg
     int 21h
     mov ax,4C00h
     int 21h
   end start

While “English” was apparent in this code, I sensed that she was thinking that she bit off more than she could chew by (willfully, I must add) signing up for this experiment. Fears were cast aside when I mentioned that technology evolves quickly and high-level programming languages from COBOL and Fortran, all the way to C/C++, Java, C#, Haskell and [drumroll] Python would make this all worth while. I explained how modern compilers and linkers are special software that take readable code and turn it into machine code that computers can understand. Often, I’d turn to examples that she could relate to in ways such as:

You know at work when you launch Microsoft Word? Well, Microsoft developers wrote a bunch of code (probably in C++) to enable you the user to do all sorts of things like letting you change fonts and their sizes, set margins, print, etc. That code was compiled by a C++ compiler that they use to become that winword.exe file on your computer so that you can simply click on Word’s shortcut and magic happens.

Relating it all in that manner appeared to cement the understanding. Without delving into functions just yet, Dena wrote her first program in a high level programming language. In a style that’s clearly all her own, she wrote a “Hello World” program in Python:

print("Hello, Bitches!")

Some experimentation followed, where she attempted to add some indentation. Her incorrect stabs at it looked like:

        print("Hello, Bitches!")

and:

print        ("Hello, Bitches!")

and:

print(        "Hello, Bitches!")

Eventually, she realized what works:

print("        Hello, Bitches!")

Slightly unsure of herself and why only the last attempt at indentation worked, I commended her first programming victory and that her experimentation conveniently leads us to the next lesson: Variables and Functions with a side order of Parameters.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
comments: 0 »

The FizzWife Project

January 17, 2011 under The FizzWife Project

Welcome to The FizzWife Project!

The goal of the experiment is to teach my wife Dena, a law clerk with little technical/scientific background, enough about the bare essentials of computer programming so that she is able to successfully implement FizzBuzz on her own.

FizzBuzz, of course, is a problem that Jeff Atwood (Internet) famously summarized as a test of programming ability (and not programming proficiency). FizzBuzz boils down to the following problem statement:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

The problem is trivial for experienced programmers, but may present a challenge to non-programmers such as Dena.

A note to all coders out there: please don’t post your own solutions to FizzBuzz in the comments; I will delete them.

The impetus for this experiment actually had little influence from yours truly. In August, Dena and I visited her brother in Kotzebue, Alaska. For the long trip, I packed some books (GASP! real dead-tree versions) to pass the time, and one of those books was Coders at Work. At various points during the trip, Dena – completely of her own volition – picked up and read some of Coders at Work; its conversational nature (and lack of any actual code) proved interesting to her. So much so, that she approached me with interest in learning something relating to what the interviewees in Coders at Work spoke about. I didn’t think she was serious, but she continued to broach the subject even after we returned.

My dilemma was to determine a project or a set of projects that would help her gain understanding of the bare essentials, while at the same time keeping her interest high. The project had to avoid dependencies on computer science-y things like data structures and algorithms, engineering topics like methodologies/paradigms/architectures, and any sort of APIs. Robert “Uncle Bob” Martin recently highlighted that programming, regardless of the language and environment boils down to three things; sequence, selection and iteration. And I think FizzBuzz, while being simple, is an excellent example of these three principles. I also settled on Python as the language she’ll use, to avoid strongly typed languages for the time being and the benefit of using IDLE for REPL purposes. Immediate feedback is a good thing! Also, I feel Python’s syntax is easy for a beginner to understand.

Join Dena and I as she’s introduced to the essence of programming, with the goal of her creating her own FizzBuzz implementation. I’ll post updates on her progress and code snippets, so stay tuned!

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
comments: 0 »