Novice Programming with PartB’s Michael Gallo

by / January 3, 2014 Technology No Comments

Post By RelatedRelated Post

By Michael Gallo

It’s learn to program week in the Technology Section. For all of you stuck on a beach with nothing but a laptop and a New Year’s Resolution, this guide is for you.

As you’ll see, it takes about a minute to write, compile and run simple a computer program (especially if you have a Mac). Once you’ve done that, the world is your oyster.

First, some background. How do computers work?

Computers are electronic circuits. They combine resistors, transistors, capacitors and other basic electronic components in clever ways. The circuit takes an input of a sequence of electric voltages, manipulates them, and outputs the result of those manipulations as another voltage.

What is programming?

Programming is the control of that circuit. Using a pre-agreed upon vocabulary, programming is how you select the input voltages and tell the computer how to manipulate them once they’re there. In programming’s early days, this meant physically moving switches up and down. That was tedious. Now it usually involves typing phrases into a text editor.

How do I start?

Get a text editor (a simple one like Notepad on Windows or TextEdit on Mac does the trick — both come preinstalled). The instructions (the “code”) get written there. The magic happens in the next step: a separate program converts that text into something that the electronic circuits can understand. Very, very clever people make these converter programs. That program may or may not come preinstalled. Either way, they’re almost always free downloads.

What language should I choose?

Let’s start with Python. It’s a slightly newer language, but it offers a lot of features that makes writing programs more intuitive. Things kind of seem to work. Also, the Python environment comes preinstalled on Macs. Without downloading anything, a Mac can execute code written in the Python language.

Python emphasises the use of succinct phrase. A more classical language choice, such as C, requires the programmer to be more explicit and verbose in describing what they want the computer to do. C also allows the programmer to work more closely to the actual voltages moving through the computer. A language like Java, another very popular choice, lies somewhere in between.

What does a Python program look like?

The classic first program to write in any language is Hello, World!. When run, the program spits out the words “Hello, World!” in some way. It’s designed to be simple with a obvious output so that the programmer can ensure their computer is set up correctly to program in the language.

It looks like this in Python:

print “Hello, World”

That’s it. That is a complete Python program. Write that in a text file and save it as

Great, but how do I make it spit out “Hello, World”? And where does it spit it out?

The answer to those questions might seem like the most obscure bit of process so far, and in some sense it is. But after you do it a few times you’ll think nothing more of it than when you save a Word document.

Mac users, open a Terminal window. (Windows users, run a google search for “Python on Windows” and follow installation instructions. Sorry.) Terminal is the command line program that comes with Macs – it’s what Hollywood likes showing “hackers” using in movies to make what they’re doing look magical. It resides in your Applications folder at the bottom under Utilities.

The open Terminal window presents a text prompt. Type “python[space]” and then drag the file you just saved from your Desktop into the Terminal window. This is where you’re actually using the Python stuff installed on your computer. Until now, you were just typing text in a text editor.

Hit Enter.

Does “Hello, World” print out? Nice, that’s the output from your computer program. Anytime you want to run your program, type into Terminal “python[space]” and then drag the in.

That’s a program? But I didn’t double click anything to open it up, and I didn’t see any graphics on the screen. What gives?

Yes, that is a computer program. Every other program is created by adding complexity to the basic structure you just created. Users of your program, for example, can begin feeding it information — the same line where “Hello, World” printed out can also read text supplied by the user into the program. That’s the basic form of interactivity. You can probably begin to imagine some useful things you can do with that.

As for the double clicking, that’s just a convenient method for running a program that the people who make Macs and PCs divined. When just a second ago you hit Enter, that was in lieu of double clicking an icon. If you really like double clicking things, you can actually type in a few more commands in Terminal, and it will create an icon to double click.

Okay, can you show me a slightly more complicated program?

Sure, let’s reverse all the letters in a sentence.

Open a fresh window in your text editor and begin typing:

sentence = “I need a winter break”

(This line assigns the text to a variable called sentence. Now instead of referring to the text, we can just type “sentence”. It’s just like in algebra when an equation says x = 5. Any time after that, seeing an ‘x’ is the same as seeing a 5.)

reversed_sentence = []

(This creates a new empty ‘list’ – a list is one type of data structure Python provides – in which to store our reversed characters. Lists work a lot like vectors. We’ll fill it in in the “loop” that follows.)

for letter in sentence:

reversed_sentence.insert(0, letter)


(The ‘insert’ command inserts the current letter in the loop in the 0th position of the list reversed_sentence. Finally we convert our list back into a string to print it out. If we didn’t convert, there would be quote marks between each character. That’s just how Python prints lists.)

print ‘‘.join(reversed_sentence)


Now save the file as With a Terminal window open type “python[space]” and then drag the icon into the window. Hit Enter.

kaerb retniw a deen I

What was going on there?

That program demonstrated a loop, specifically a for-loop. Computers are great at performing lots of similar computations very quickly, and loops are typically how those rapid computations are written out in code. That for-loop above looped through each character in the sentence.

Loops and conditional statements (if this is true, then do this) are where programming gets dynamic and useful. We’ll get to both in future issues.