Introducing Computer Programming to High School Students Using Python

Add up all of the numbers from 1-100. 

There is a story that has been told in many different ways of a young man named Carl Gauss who was assigned, along with all of his fellow students to do the above task. Most of the tellings of this story portray the teacher as drunk, tired, or simply wanting to keep his students busy. Then after just a couple of minutes, Carl astonished everyone by raising his hand and announcing the answer.

The age of information did not take off because we could use computers to make pretty spreadsheets or documents, it was because we could now be superheroes. The ability to calculate at lightning fast speeds changed what humans were able to do forever. We could now go to the moon or perform bank transactions instantaneously. We were on the brink of utopia, there was talk that computers would cut the workweek down to 20 hours a week (why it in fact increased is for another post, another time). 

Computers are fast and if you can harness that power you can control the universe.

Gauss took a couple of minutes to discover that 1+100 = 101, 2+99 = 101, 3+98= 101, and so on creating 50 pairs of 101 and therefore the sum of the numbers from 1-100 was (50 x 101) = 5050. Ironically, a student could Google "Gauss 1-100 sum" then find, read, and understand in the same amount of time it took Gauss to do it from scratch. Isn't technology grand?

We can ask computers to do all kinds of things if we speak their language. In this case we will use the language Python.

If you have not downloaded it yet, here is the link: Python. I would encourage you to download version 2.7 as it is more widely compatible. 

You can open Python through IDLE or the Command Prompt, it will work either way.

Before we get to our Gauss problem, lets initiate ourselves into the world of programming.

Type in the following: print('Hello World') so the command line looks like:

print('Hello World')

When you press enter, you will see the response.

Hello World

Congratulations, you and the computer are now on speaking terms. The print function will output to the screen anything you put inside its parenthesis.

Try this:


print(3+5)

We are going to leap ahead to looping. This is where the computer truly excels, when you can ask the computer to do something over and over and over again with accuracy and speed.

Lets say you wanted the computer to say 'Math is fun' 5 times. 

for i in range(1,6):
...    print('Math is fun')

Make sure you don't forget the colon, and then hit enter. On the next line Python you will see 3 dots. This means that you wish to create a "for loop". Hit the spacebar to indent 4 spaces to indicate what will be looped.

Hit enter twice, once to break out of the loop and another to run the program. Python will run through the loop 5 times (the range 1,6 means 1,2,3,4,5 and stop at 6) printing Math is Fun.

Math is Fun
Math is Fun
Math is Fun
Math is Fun
Math is Fun

What you said was, "for every number in the range of 1-6, output to the screen Math is Fun". 

Finally, regarding our Gauss situation. Lets create a variable, which we know from math stores a number.

answer = 0

Type that and hit enter. Now you have told the computer to set aside some memory with the number zero in a place called 'sum'.

Let's create the loop which will go through the numbers 1-100 and add them to the sum.

for answer in range(1,101):
...       answer = answer + number

Hit enter twice, and you are done! In less than a second, the computer did what we could not have done in minutes or hours. 

Hey wait, I don't see anything. Ahh, while the computer is finished, it has not told you anything, but that was because you never asked.

Type in print(sum) and see what the answer to all of the numbers from 1-100. Did you get 5050? Awesome!

Modify the program to add up all of the numbers from 1-1000 or have it print out the result of 10! (Factorial - the product of the numbers, e.g. 1*2*3*4*5, don't do this for a large number or your computer will run out of memory and crash!)

This is one way of using programming to save you time. Consider all of those bank transactions that were at one time done by hand or librarians who manually would catalog and find books on a particular subject. What about calculating the shortest way around the traffic jam on the freeway. The possibilities are truly limitless.

I will continue to provide some more examples but as you get more and more acquainted with programming languages like Python you can use your own creativity to adapt to your needs and interests as I have. As programming tends to be a teach yourself kind of thing, you will need to find what tutorials work best for you. If you prefer books, Amazon and other book stores have plenty. The number of tutorials online are at least twice as much. Many of my students benefit from YouTube Videos from authors like thenewboston

Programming is an art, not just a science. If you are having problems installing, getting a program to work, hearing more of my recommendations for resources, helping you to create your own programs, or how to implement in the classroom; leave a comment or email me at phil@brokenairplane.com.