I believe that everyone should be able to program a computer! Now I can see you all rolling your eyes but bear with me for a moment.
We live in the Information Age, computers are everywhere and computer literacy is a skill everyone needs. Unfortunately most people stop short, learning to drive a computer but not really learning to use its full potential.
Now don’t get me wrong, I’m not advocating that everyone become professional programmers. We are strange bunch and you really have to love puzzles and think in a particular way to do coding as job, but the number of times I have seen people perform mindless, error prone, tasks on a computer which could be automated by a simple computer program is astounding. For instance re-sizing hundreds of photos or writing 100 personalized letters, one… at… a… time…
Work smarter not harder is the name of the game!
So what is this “code” stuff anyway ? Well, computers are pretty simple devices, powered by electricity everything comes down to whether something is turned on or off. Programming (or coding) is the process of writing out a set of instructions that you want the computer to perform and then en_coding_ them in to a form that the computer can understand.
In early days of computers this meant that a computer program (a group of instructions) would look like this:
10110100 00001001 10111010 00001000 00000001 11001101 00100001 11000011 01001000 01100101 01101100 01101111 00100001 00001101 00100100
Some poor engineer would need to figure out all these 1’s and 0’s (ons and offs) and then enter them into a computer by either toggling switches or feeding it paper tape with holes punched into it. By the way those 1’s and 0’s above display the text Hello! on the screen when execute by a computer!.
Clearly this wasn’t productive at all. So the concept of an “assembler” was developed. This would allow a program to be written in a human friendlier way which, could then be assembled into the 1’s and 0’s that the computer could understand. So now you could write the following to print Hello! on the screen, instead of coding up a bunch of 1’s and 0’s:
start: mov ah,9 mov dx,hello int 21h ret hello db 'Hello!',13,36
This is much better…not! At least it vaguely looks like English but its still not really easy to understand. Luckily 3rd generation “languages” were developed which, meant that programs could be written in English (with a very strict grammar) and then “compiled” into the 1’s and 0’s needed by the computer. Our program to display Hello! on the screen now just becomes:
A crash course in how programs work
A computer program contains 4 different things:
Statements – Instructions to do something. For example the print statement we saw above or
Which adds 1 and 1. These can be combined, so the following prints out 2 on the screen:
Loops – Instructions to do something a number of times. For example to display the numbers 1 to 10 on the screen:
for count in range(1,11): print count
Conditionals – Instructions to do something, if something is true. For example:
if 1+1 == 2: print "The answer is 2!"
Variables – Places to store things (data) needed by the program. For example
answer = 1+1 print answer answer = answer + 40 print answer
- Adds 1 and 1 and stores the result (2) into a variable called answer
- Displays the value of answer on the screen (2)
- Takes the value of answer, adds 40 to it and then stores the result (42) back into the variable called answer
- Finally displays the value of answer again on the screen (42)
Tada! You’re programming
Its remarkably easy and with a little bit of study, you can save yourself hours of time or even write yourself that smartphone app you have also wanted. If you are interested in learning more about programming, the Internet is chock full of resources. I’d suggest you check out the Codecademy or The Khan Academy sites to get started. The examples above were written using a language named Python (named after the Monty Pythons) which is particularly easy to learn, but it is only one of many different computer languages.
Oh yes and those tedious tasks I mentioned at the start of this post, here is how you would solve them in Python:
# Python program that resizes all the .jpg images in a folder named "photos" # load some "modules" that tell the computer how to manipulate files and images import glob from PIL import Image # we want to resize images to 640 by 480 pixels size = (640,480) # grab a list of the .jpg files in the photos folder photos_to_resize = glob.glob('photos/*.jpg') print "Resizing: " + str(photos_to_resize) # resize each photo in the folder and save the resized photo for photo in photos_to_resize: resized_photo = photo[:-4] + "_resized.jpg" # create name for the resized photo im = Image.open(photo) # open the photo im.thumbnail(size, Image.ANTIALIAS) # resize the photo im.save(resized_photo, "JPEG") # save the photo print photo + " resized and saved as " + resized_photo
# Python program that generates a personalised letter for a list of people # it generates a word document with one letter per page # load a "module" to tell the computer how to create word documents from docx import Document # the list of people that we want to create letters for names = [ 'Emma', 'Charlotte', 'Ella', 'Sophie', 'Hannah', 'Emily', 'Jessica', 'Olivia', 'Grace', 'Isabella' ] # create a new word document document = Document() # for each name in the list create a letter for name in names: document.add_paragraph( "Dear %s," % name) # add the salutation # add the rest of the text document.add_paragraph( "Thanks for your interest in the Acme 2001 Widget. We are pleased to announce that these are now available for sale.") document.add_paragraph( "As a valued customer you can purchase one with a 25% discount!") document.add_paragraph( "Yours Sincerely") document.add_paragraph( "Horace P Sweet") # add a page break so we get one letter per page document.add_page_break() # save the word document document.save('Letters.docx')