Dark theme

Key Ideas


Although there is a lot in this part, the key ideas are fairly simple. They are:

  1. A computer program is a set of instructions for the computer telling it what to do. In most high-level languages the instructions are written in something humans can easily (well, reasonably easily!) understand in one or more text files. The file/s are then converted into binary data that a computer can understand and 'run'.
  2. Two types of software are important when running code. A 'compiler' turns one language into another (usually human readable source code into binary); while an 'interpreter' takes some language and both converts it and runs it. We broadly divide languages into 'compiled' languages, where the code is converted into binary specific to a type of computer, which can then be run multiple times without the source code (which may be commercially valuable), and 'interpreted' languages, where the source code is sent to anyone who needs the program, and an interpreter is used each time the code is run to change the source code to a running program. In fact, the distinction is becoming weaker. A lot of languages, including Python, now have a sequence where the source code is compiled into an intermediate language called 'bytecode', and this is then run inside another piece of software called a 'virtual machine', which does the final conversion to a running program. This helps isolate the code from the operating system for security reasons, but also makes the interpretation more efficient (for example, by storing and reusuing highly optimised binary where it can). Nevertheless, we talk of Python as an 'interpreted' language as you usually need the source code each time you run it.
  3. Most computer programs are text files containing human readable text which describes the tasks you want the computer to do. These are listed top to bottom and generally run in that order. Each language has its own syntax; essentially the grammar of the language. The simplest programs are based on:
    1. delimiters: symbols associated with structuring and syntax &ndash for example, line breaks and colons in Python.
    2. keywords: words that mean something specific in the language and can't be used for anything else.
    3. variables: labels attached to values (or, as we'll see, other bits of code) &ndash these allow us to store data in a way we can talk about it (the 'state' of the program).
    4. operators: symbols that operate on values around them &ndash for example, the addition symbol "+".
    These elements (sometimes called the 'atoms' of a program) build up into expressions (combinations that evaluate to a value) and statements (full instructions to the computer). A statement is the smallest runnable component of a program, and in Python is usually a single line in the text file.
  4. It's rare that a simple list of tasks will run from top to bottom of a text file. Usually you want to run some bits of code more than once, or change how the code operates as it interacts with users. Therefore most programs have 'control flow' structures more complicated than just a list of tasks.
    1. Programs may have control flow keywords &ndash like if &ndash that change the flow of the program.
    2. Programs may separate out chunks of code into a separate area called a 'procedure' &ndash like print() &ndash these can be 'called' (run) as many times as needed, and information can be sent to them and recieved back from them. Depending on where they are in the code, and which language you are using, they can be called 'subroutines', 'procedures' or, as in Python, 'functions' or 'methods'. They save you having to type out the same code multiple times, and saves the code being longer and more complicated than it needs to be.
    3. The ultimate version of this is to separate the procedures and variables into separate files that deal with specific issues &ndash like representing a menu on the screen &ndash and building these up into libraries we can call upon. This is the core of Object Oriented Programming, which links together a variety of files to do a job. In Python, some of the proceedures &ndash like print() &ndash are built into the core language files so you can just use them.
  5. You don't need to understand how to use any of these things at the moment, they are just to give you a flavour of the kinds of things we'll be looking at, but here's one version of the code from the lecture, with a brief pull apart:

    value = 2
    answer = 4 + value
    if answer > 0 :
       print (answer)

    In this code, value is a variable label attached to the number 2; so, if we make the expression 4 + value, what we actually get is 4 + 2. The value of this expression when evaluated is attached to the variable label answer. We then have some control flow structuring: answer > 0 is a 'condition'. If the condition is true (which it is), the indented line after the colon happens; if it is false, it doesn't, and the program just ends. The indented line calls the built-in procedure print() and passes in the value represented by the answer label. The most obvious elements of the syntax are that each statement starts on a new line, that a colon suggests some kind of independent block of code that may or may not run is coming, and that that block of code is then indented. In total, then, there's a lot going on in this little piece of code. The only thing that's not in there is some explicit object oriented code (though I can guarentee there's some running in the background of print()).