by Simon Bluck
Computers and Programming
A digital computer consists of: memory, processor and i/o. The computer manipulates numbers, just numbers.
There are different kinds of memory accessible to the processor. A memory consists of an indexed set of locations. Each location holds numbers. E.g. location 9253 might contain numbers 43, 98 and 99.
The processor chip contains a mass of logic circuits for obeying instructions such as fetch from memory, add, multiply etc. The “engine” of the processor contains logic that repeatedly fetches a number from memory, interprets that number as an instruction, and executes that instruction.
The memory location index to fetch the instruction from is held in a special memory location called an Instruction Pointer (IP). The processor engine automatically increments IP after fetching the instruction. The engine operates repeatedly, so fetches and obeys instructions stored in sequential memory locations. The full set of stored instructions is called a program. The execution depends on the value of the instruction, e.g. 41 might mean store, 43 might cause addition, 55 might be “output”. The memory may contain special locations, with particular hard-wired chip logic e.g. location 0 could hold the “instruction pointer” (with its hard-wired auto-increment after fetch), location 1 could be the “accumulator”, where a value is operated on by the instructions.
Memory both holds the programs to run, and is used to store variable values used by the programs.
Consider the following program stored in memory locations 10 .. 13:
Location | Contents | Meaning |
---|---|---|
0 | 10 | Instruction pointer |
1 | 0 | Accumulator |
… | ||
10 | (41, 1, 98) | Fetch contents of location 98 into location 1 |
11 | (43, 1, 99) | Add contents of location 99 to location 1 |
12 | (55, 1, 50) | Output contents of location 1 to output port 50 |
13 | (40, 0, 11) | Store 11 into location 0 |
… | ||
98 | 123 | |
99 | 2 |
The processor runs the program:
The hard-wired “instruction fetch and execute” on location 0 uses the contents of that location as an index to fetch an instruction from (location 10); then it increments location 0, and executes the fetched instruction.
The processor repeats the instruction fetch and execute on location 0, this time fetching an instruction from location 11 – an addition.
So it fetches the contents of location 11, which is an addition. So after these two instruction fetch and executes, we now have:
Location | Contents | Meaning |
---|---|---|
0 | 12 | Instruction pointer |
1 | 125 | Accumulator |
… | ||
10 | (41, 1, 98) | Fetch contents of location 98 into location 1 |
11 | (43, 1, 99) | Add contents of location 99 to location 1 |
12 | (55, 1, 50) | Output contents of location 1 to output port 50 |
13 | (40, 0, 11) | Store 11 into location 0 |
… | ||
98 | 123 | |
99 | 2 |
The program continues. The instruction at location 13 resets the instruction pointer back to 11, causing the program to loop. So the program loops, outputting 123, 125, 127, …
Larger programs would be incomprehensible with everything just numbers. Wouldn’t it be so much easier if it could look like:
Location | Contents | Meaning |
---|---|---|
IP | progstart | Instruction pointer |
ACC | 0 | Accumulator |
… | ||
progstart | (Fetch, ACC, start value) | Fetch contents of start value into accumulator |
loopstart | (Add, ACC, incrvalue) | Add contents of incrvalue to accumulator |
(Output, ACC, 50) | Output contents of accumulator to output port 50 | |
(Jump, loopstart) | Store loopstart index into IP | |
… | ||
startvalue | 123 | |
incrvalue | 2 |
A programming language allows you to do that, and better. E.g. in Python:
# Print numbers starting at 123, incrementing by 2 each time.
startvalue = 123
incrvalue = 2
accumulator = startvalue
while True:
accumulator += incrvalue
print(accumulator)
# End of program
Or, sensibly limiting this loop to stop before 200, just:
for i in range(123, 200, 2): print(i)
The programming language interpreter (python) takes what you write, analyses it, and arranges that the processor performs the relevant low level instructions to obey the defined logic of your program.
What Is Python?
Python was invented by Guido van Rossum, in the late eighties. The language is under continuous, controlled development with the aim of improving it whilst striving to maintain backwards compatibility. Latest versions as of 11 Jan 2016: Python 3.5.1 and Python 2.7.11.
Python is a programming language that provides useful features to enable us to efficiently write effective programs. For instance, allowing us to write basic mathematical expressions such as a
x = a + b / 43
Like most programming languages, programs are written in plain text.
A program, python
, is provided by the designers of the language to process the program file you have written in the Python language. The python program analyses your program on the basis of the Python language rules, reporting errors where you break those rules. As a language interpreter, it executes your program as it analyses.
Working with Python
It’s difficult to write a program if you don’t know where to start. So here’s some useful information and guidance. Firstly, what issues do you face?
Choosing your language: There are many, many programming languages. Python is easy to use, modern, well supported and has a massive online presence. It is usable for many projects. It is of course well described in Wikipedia.
You can choose to use Python 3, or the older but still very much in favour Python 2. The RaspberryPi organisation prefers Python 3 (here, which is also a great mini-intro to Python), and that is what is assumed for this course. The differences are relatively small. See here for a well-considered discussion on whether to us python2 or 3.
Learning the language: It’s useful to look at short, well-written example programs and try and understand them. You have to write Python to learn it, so find some small manageable projects; or even just try modifying some of the examples.
Using the language: You need to know how to create Python program files and how to run them.
Debugging: Programs tend to have bugs. The Python interpreter will report basic syntax and execution errors, and hopefully the errors are clear enough to enable you to fix those problems. But if your program doesn’t actually do what you want it to do, you’ll need to debug it. How do you do that?
Documenting: If you don’t document your program in some way then you’re likely making a problem for the future, when it needs fixing or you’d like to extend it. Them maybe even you as the writer could be struggling to understand quite what it’s doing from the code. Python has some built-in documentation aids.
Publishing: Hey, you’ve made it. You’d like to make public a program, or library module you’ve written. How?
Online Information
Python has a massive online presence. Here’s a set of useful links to carefully selected information sources.
Generally useful links
- PEP8 Style guide – A lot to take in but well worth skimming periodically as you learn and use Python. There’s even an online code checker here.
-
docs.python.org – Official Python3 documentation. An invaluable learning and reference resource. It’s a lot to take in!
-
news:comp.lang.python (Google groups) – Very active Python news group where you can discuss anything to do with Python.
-
The raspberrypi.org Python Learning Tool – For beginners.
-
The Python Wiki – A broad range of Python resources.
Libraries
-
The Python Standard Library (under doc.python.org).
-
PyPI – the Python Package Index. A large contributed-software repository.
Tutorials
-
Python3 Tutorial – Part of the official documentation. Excellent.
-
TutorialsPoint (Python2) – A useful tutorial and information source despite just being for Python2. The Python Quick Guide is very good.
Cheat sheets
Free to download books
-
Non-Programmer’s Tutorial for Python 3 (Wikibooks).
-
Python Programming (Wikibooks).
-
Invent With Python – has many good books on Python. A number are readable online and some are free to download.
NEXT -> Part Two: Coding Tools