Programming concepts
Generations of programming languages, bits, bytes and ASCII, compiler versus interpreter, data types, primitives and operator priorities, and functions with parameters passed by value or by reference.
Based on: lecture 4 (programming, Python in a nutshell) · About 55 minutes
Generations of programming languagesImportant
| Generation | What it is | Properties |
|---|---|---|
| First: machine language | binary instructions (ones and zeros) executed directly by the CPU | no translator needed; unreadable for people; machine-specific |
| Second: assembler | a short mnemonic per machine instruction (LD for load, ADDI for add integer, ST for store) and names for memory locations | one-to-one with machine instructions, so still machine-specific; translated by an assembler program; the first assembly language dates from 1947 |
| Third: high-level languages | statements such as if, for, while, x = y + 3, close to human language | machine independent and programmer friendly; each statement becomes many machine instructions; translated by a compiler or interpreter; early examples Fortran (1957), COBOL (1959) and ALGOL (1960); all modern languages, Python included |
The "holy grail" of programming would be to write programs in everyday language. Some modern third-generation languages such as Python, Ruby and Perl already borrow features of fourth-generation tools.
Bits, bytes and ASCIIImportant
A bit is the smallest unit of data: 0 or 1 (off or on). A byte is eight bits and represents a value from 0 to 255. Read a byte from right to left: the positions are worth 1, 2, 4, 8, 16, 32, 64 and 128, and you add the values of the positions that hold a 1.
From byte to character
Convert the byte 01001101.
Positions with a 1 (from the right)
Add them
Look up 77 in the ASCII table
01001101 = 77 = "M". ASCII (American Standard Code for Information Interchange) is the most common table linking numbers to characters: 65 is A, 97 is a, 48 is the digit 0.
From number to byte
Which byte equals 99?
Largest position that fits
Next
Positions with a 1
99 = 01100011. Exam questions give four bytes and ask which one adds up to a number: always add the positions, do not guess from the pattern.
Compiler versus interpreterImportant
Third-generation programs are translated automatically. A compiler translates the whole program into machine code before it runs; it first checks for syntax errors and only translates when there are none (typical for Fortran, Pascal, COBOL). An interpreter translates and executes one instruction at a time (typical for BASIC and Prolog, and also for Python). That is why a Python program can print its first lines and then stop with an error further down.
Data typesImportant
| Kind | Examples | In Python |
|---|---|---|
| Single predefined types | integer (whole numbers), float or real (with decimals), Boolean (True or False), character | int, float, bool, str (a string can hold more than one character) |
| Self-built single types | enumerated type or list (one-dimensional, can grow and shrink); interval type such as 20..29 or Monday..Friday | list, tuple, set |
| Self-built multiple types | array: block of identical elements with fixed dimensions (row, matrix, tensor); record or aggregated type: block of mixed fields; file: unlimited number of records in external memory | dictionary for a record; NumPy arrays; files with open() |
Pseudo code has no type declarations, and neither do untyped languages such as Python: the interpreter works out the type of every variable itself. Languages like Pascal or Fortran require a declaration first.
A file lives in external memory, so the data is kept, and you do not need to fix its size in advance. Records are read one by one into RAM until EOF (end of file); text files are read line by line (EOLN marks the end of a line). All other variables live in internal memory.
Primitives and operatorsImportant
All third-generation languages share the same primitives as pseudo code, with small differences in syntax: assignment (Z = X + Y in Python, Z := X + Y in Pascal), control statements (if, while, repeat, for), comments and functions.
| Group | Python operators |
|---|---|
| Arithmetic | + - * / (division, always a float) // (floor division) % (modulo, the remainder) ** (power) |
| Comparison | == != < <= > >= (the result is a Boolean) |
| Boolean | not, and, or |
| Assignment | = and the short forms += -= *= /= //= %= **= |
Priority, from high to low: brackets, functions, power, * and /, + and -, then the comparison operators; equal priorities are worked out from left to right. Boolean operators have their own order: not, then and, then or.
Priority in practice
Evaluate 2 + 3 * 2 ** 2 and not True or 5 > 3 and 2 > 4.
Power first
Then multiply, then add
Boolean part
The results are 14 and False. You can check this in the predict-the-output lab.
Functions, procedures and parametersImportant
Functions and procedures are modules: mini programs inside the program. Some languages separate them: a function delivers a value, a procedure is a sequence of actions that stands apart from the main program. Python only has functions; a function without return gives None.
Distinguish the definition (def name(par1, par2): with the formal parameters) from the call (name(arg1, arg2) with the actual parameters or arguments).
| Passing method | What is passed | Effect of changes inside the function |
|---|---|---|
| By value | a copy of the value | the copy is deleted afterwards; the original is untouched |
| By reference | a reference to the same memory location | changes are permanent: the caller sees them |
In Python, numbers and strings behave like pass by value (they cannot be changed in place), while a list or dictionary passed to a function can be changed in place, which looks like pass by reference.
The four golden rules of functions: (1) definition and call must use the same name, (2) the number of parameters must match, (3) the types must match, (4) if the function must provide a value, use return.
Key takeaways
- Machine language (binary), assembler (mnemonics, one-to-one), third generation (high level, machine independent).
- A byte has eight bits worth 128, 64, 32, 16, 8, 4, 2, 1; ASCII maps the value to a character.
- A compiler translates everything first; an interpreter (Python) translates and runs line by line.
- Priority: brackets, functions, power, * and /, + and -, comparisons; then not, and, or.
- Pass by value copies, pass by reference shares; use return when a function must deliver a value.
All cards known
Reset the progress or pick another unit to keep practising.
Multiple choice (8)
Which byte adds up to 99?
Which property belongs to assembler?
How does Python translate a program in the course's terms?
What is the value of 2 + 3 * 2 ** 2?
Which Python data structure corresponds to the record type?
A function receives a list and appends an element to it. After the call, the caller's list:
Which statement about the file type is correct?
In not A or B and C, which operator is evaluated last?
Exam-style open questions (2)
Binary and ASCII
Convert 01000010 and 01100001 to decimal and to ASCII characters, and explain the method.
Show a model answer outline
- Read from the right; positions worth 1, 2, 4, ..., 128.
- 01000010 = 64 + 2 = 66 = B.
- 01100001 = 64 + 32 + 1 = 97 = a.
- Capitals start at 65, small letters at 97.
Pass by value or by reference
Explain the difference with a small example of a function that changes its parameter, and say what the caller sees in each case.
Show a model answer outline
- By value: copy, change lost after the call.
- By reference: same location, change visible to the caller.
- Python: a list changed inside a function stays changed; a number reassigned inside does not change the caller.
LabByte and ASCII converter
Choose a mode. Every position is worth twice the one to its right; the verdict tells you which ASCII character the byte stands for.
LabGenerations and translators
A quick sorting game for the theory questions about programming languages.