Hello, World!

0
1277

By Jeff Kazmierski, Copy Editor

Have you ever downloaded a really cool app like Angry Birds or Alchemy and wondered how they did it? Do you think Java is something you get at Starbucks? Or maybe you have a friend majoring in Computer Science and watching her work on the latest assignment looks like a bunch of gibberish. Chances are, if you’re a non-programmer, the process of programming probably seems a bit like watching a Penn and Teller show. Two-thirds showmanship, one-sixth snappy patter, some hand-waving, and even when they tell you the secret, it’s still a mystery.

If you’re curious about how it’s done, but don’t have time in your schedule to squeeze in an Intro to Computing course, I’m here to help. Programming is easier than you think, and all the tools are out there for the taking.

This is the first of a series of weekly discussions on the fine and subtle art of computer programming.

We’ll be using Python, a language so deceptively simple that even a 9-year-old child can learn it, yet powerful enough that it’s used by multi-billion dollar organizations like Google, Yahoo and the National Weather Service.

To get started, you’ll first need some software. The latest package, version 3.2.2, can be found at python.org/download. Surf there and find the package for the computer you’re using (Windows or Mac OS X). If you’re running Linux, your OS probably already has Python 2.7.2 installed. You can upgrade to 3.2 if you want, but you don’t have to.

You’ll also need an interpreter. The Windows release of Python includes a program called IDLE, which makes it easy to write and edit code. You can download IDLE for Mac and Linux as well – see the sidebar.

Since most of you are probably Windows users, I’ll focus on that version. Functionally, there’s little difference, though. Once you’ve downloaded and installed Python, find it in your Windows menu under “All Programs/Python 3.2.” There should be a program there called “IDLE (Python GUI).” Click it.

You should see a window labeled “Python Shell” with a bunch of stuff about Python 3.2, a copyright notice, and a “>>>” thingy. This is the Python interpreter, a kind of command-line interface where we will be doing most of our work. Next week when we get into writing programs, I’ll talk about the IDLE editor.

First, in the Shell window, type this:

>>> print(“Hello World!”)

Wasn’t that cool? Now try this:

>>> print(2**8)

Nifty! And finally, this:

>>> lumberjack = “okay”

Hey, wait a minute. What happened? The other two gave immediate feedback on the screen and that one just gave us another command line. What gives?

We just did something called a “variable assignment.” Basically, we told the computer to take a chunk of memory, call it “lumberjack,” and put the value “okay” in that chunk. Don’t worry about the technical details for now, just remember how it’s done.

So if we do this:

>>> lumberjack

The shell returns the value of the variable “lumberjack:”

‘okay’

Cool, huh? Try it this way:

>>> print(“I’m a lumberjack and I’m ” + lumberjack)

I’m a lumberjack and I’m okay

>>>

The interpreter takes the print statement, displays the text you put in it, then reaches out to the chunk of memory occupied by “lumberjack” and prints the value stored there.

By now, Java and C programmers are probably wondering “but what about variable type? Don’t I have to tell Python ‘lumberjack’ is a String?”

Nope. Because Python doesn’t care. In fact, we can do this:

>>> lumberjack = 5

Or even this:

>>> lumberjack = input(“If you’re a lumberjack say ‘okay’ “)

And python will happily put whatever value you enter into the lumberjack variable. All variables in Python are dynamic, meaning they change type depending on what they’re currently storing.

What we’ve covered here isn’t truly programming; we’ll get deeper into that next time. But it is the building blocks for a lot of what we’ll do in the future.

Just for fun, let’s wrap up with a short script:

>>> for x in range(1, 11, 1): print(“spam ” * x )

This is called a loop. We told Python to count up from 1 to 11 (10, actually) using the variable x and print that many “spam ” strings at each count. Wondering why it only printed 10 when we told it to count to 11? Because Python counts up to one less than the highest value. Remember that; it’s important.

That’s all for now; next time we’ll get deeper into writing scripts. Until then, if you’re interested in learning more, there are a lot of good books about Python, some more technical than others. A good place to start is Python Visual Quickstart by Toby Donaldson, which is what I used to teach the language to my kids.

Good luck, and good programming!

Comments

comments