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."
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…