Earliest "computers" were essentially created to solve one particular problem, like Pascal's calculator, which could add and subtract numbers.
Pascal's Calculator (1649)
The earliest electronic computers also were designed to execute one particular algorithm. If you wanted the computer to do something else, you had to physically re-wire it.
A breakthrough came with the idea of programmable computers which could solve lots of different problems, by somehow changing the algorithm that the computer follows.
The first design for a programmable computer was created by Charles Babbage in 1837, which he called the Analytical Engine. Ada Lovelace worked with Babbage by translating algorithms for the Analytical Engine, creating the first computer programs. Unfortunately the Analytical Engine was never completed.
Model of the Analytical Engine (1837)
It would not be for another century until programmable computers were successfully built, there were several in the 1940s, including the ENIAC, built at the University of Pennsylvania in 1945.
The ENIAC Computer (1945)
In a programmable computer, the program is fed into the computer along with the data. The computer then follows the instructions of the program to operate on the data.
Functional view of a computerThe CPU:
Computers execute a program one instruction at a time. Each instruction is very simple and precise, such as the following:
Computers execute these very simple instructions one after another, very quickly. The instructions are actually encoded as binary numbers. The direct instructions are called "machine language". For instance the first instruction above is the following (for ARM machines):
Machine language is the most detailed view of programming, and is tedious and difficult to the point nobody would willingly do it. In order to avoid this, we've created "assembly language" which is easier. The instruction above in assembly language looks like this:
add r3, r1, r2
A program called an "assembler" translates these assembly instructions into the binary machine instructions so the computer can understand them.
Assembly language is easier since now we can read the name of the instruction and more easily see what memory cells are involved. But it is still very tedious for a few reasons:
To make programming easier, we've created "high level languages" which allow us to write programs more naturally. One line of a high level language program could expand into many assembly/machine language lines. They also allow us to name memory locations making it easier to understand.
The code above in Python might look like this:
total = cost + tax
In high-level languages, we can more easily keep track of what is going on in a program. Programming in a high-level language can still be challenging because we must be very precise and explicit, since computers can only follow instructions, they don't know what we meant to do.
High-level language programs must be converted into machine instructions as well. This can be done by a compiler which converts the program to machine code ahead of time, or by an interpreter which converts the program as its being executed.
Python uses an interpreter. When a Python program is executed, it's converted into machine instructions as it's being run.
In our first Python program, we saw the
input("How are you feeling today? ")
When we run this program, it will print the prompt to the screen for us, and then wait for us to type something in. We can only type one line of text. As soon as we hit enter, Python moves on from the input instruction. In this case, there is no next instruction so the program finishes.
In the program above, whatever the user types in can't really affect the program at all. The program is ignoring whatever input the user gives it. In order to do something with input, we must put it into a variable.
Variables in programming are names that we associate with some piece of information. Variables let us refer back to something that was created earlier on in a program. They also let us save whatever the user inputs, so we can keep track of it.
Variables in Python have names that are made of letters, numbers and underscore characters. They cannot begin with a number and cannot have spaces in them. Are the following legal variable names?
Variables also cannot begin with a Python keyword. Keywords are words that have special meaning in Python. We will see keywords as we learn the features of Python. If the name you come up with is colored in IDLE, then it is probably a keyword, and you should think of a new name!
The way that a variable is created in Python is by putting the name on the left hand side, then an equals sign, and finally the thing that you want to save. For instance, if we want to save our user's input in a variable, we could do it like this:
answer = input("How are you feeling today? ")
Now when we run this program, it will ask us the question, and wait for us to enter a response. It will then save whatever we give it into the variable called "answer". We can now change the program so that it prints it back to us:
answer = input("How are you feeling today? ") print("You said") print(answer)
Here is an example run of this program (the bold text is what the user types):
How are you feeling today? Pretty good You said Pretty good
Notice that to print a variable, we can just pass it into the print command. Also, it's really important that there are no quotation marks around answer. What would the program print if we did put quotation marks around answer?
In the program above, we printed two messages on two different lines, which looks weird. We could also have put them in one line, and only did one print instruction. To do that, we can pass the message and the variable to print on one line, separated by a comma. That would look like this:
answer = input("How are you feeling today? ") print("You said", answer)
When we run this program, it will look like this:
How are you feeling today? Pretty good You said Pretty good
There is no limit to how many things we can print like this, we can just keep adding things and putting commas between them. Like if we want to also print "Bye!" so the user knows the program is done, we could add that in:
answer = input("How are you feeling today? ") print("You said", answer, ". Bye!")
Now the program prints this:
How are you feeling today? Pretty good You said Pretty good . Bye!
Notice that Python automatically puts a space between the things that we are
printing. This is often helpful, but in this case makes the output look kind
of weird since there is a space before our period. When we want to avoid this,
we can pass
sep="" to print. This tells it to separate the things
it's printing with nothing at all. Now the program looks like this:
answer = input("How are you feeling today? ") print("You said ", answer, ". Bye!", sep="")
And it will output the following:
You said Pretty good. Bye!
Note that we had to now put a space between "You said" and the variable because now there isn't one put in automatically.
Next we will look at doing things with variables besides just printing them out!
Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.