hanegreefs.com/Business Information SystemsBIS
Lecture 4 · unit 3 of 16

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

GenerationWhat it isProperties
First: machine languagebinary instructions (ones and zeros) executed directly by the CPUno translator needed; unreadable for people; machine-specific
Second: assemblera short mnemonic per machine instruction (LD for load, ADDI for add integer, ST for store) and names for memory locationsone-to-one with machine instructions, so still machine-specific; translated by an assembler program; the first assembly language dates from 1947
Third: high-level languagesstatements such as if, for, while, x = y + 3, close to human languagemachine 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.

Worked example

From byte to character

Convert the byte 01001101.

1

Positions with a 1 (from the right)

1, 4, 8 and 64
2

Add them

1 + 4 + 8 + 64 = 77
3

Look up 77 in the ASCII table

the capital letter M
Answer

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.

Worked example

From number to byte

Which byte equals 99?

1

Largest position that fits

64, remainder 35
2

Next

32, remainder 3; then 2 and 1
3

Positions with a 1

64 + 32 + 2 + 1
Answer

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

KindExamplesIn Python
Single predefined typesinteger (whole numbers), float or real (with decimals), Boolean (True or False), characterint, float, bool, str (a string can hold more than one character)
Self-built single typesenumerated type or list (one-dimensional, can grow and shrink); interval type such as 20..29 or Monday..Fridaylist, tuple, set
Self-built multiple typesarray: 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 memorydictionary 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.

GroupPython operators
Arithmetic+   -   *   / (division, always a float)   // (floor division)   % (modulo, the remainder)   ** (power)
Comparison==   !=   <   <=   >   >= (the result is a Boolean)
Booleannot, 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.

Worked example

Priority in practice

Evaluate 2 + 3 * 2 ** 2 and not True or 5 > 3 and 2 > 4.

1

Power first

2 ** 2 = 4
2

Then multiply, then add

3 * 4 = 12; 2 + 12 = 14
3

Boolean part

not True = False; 5 > 3 and 2 > 4 = True and False = False; False or False = False
Answer

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 methodWhat is passedEffect of changes inside the function
By valuea copy of the valuethe copy is deleted afterwards; the original is untouched
By referencea reference to the same memory locationchanges 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.

Loading cards
Question
Click, tap or press space to flip
Answer

Multiple choice (8)

Question 1 · Lecture 4

Which byte adds up to 99?

Why: 64 + 32 + 2 + 1 = 99. The others give 83, 75 and 107.
Question 2 · Lecture 4

Which property belongs to assembler?

Why: Assembler keeps one mnemonic per machine instruction, so it stays machine-specific.
Question 3 · Lecture 4

How does Python translate a program in the course's terms?

Why: Python is interpreted.
Question 4 · Lecture 4

What is the value of 2 + 3 * 2 ** 2?

Why: Power first, then multiplication, then addition: 2 + 12.
Question 5 · Lecture 4

Which Python data structure corresponds to the record type?

Why: A record groups named fields of mixed types: a dictionary.
Question 6 · Lecture 4

A function receives a list and appends an element to it. After the call, the caller's list:

Why: A list is shared with the function, like pass by reference.
Question 7 · Lecture 4

Which statement about the file type is correct?

Why: Files are unlimited and kept in external memory.
Question 8 · Lecture 4

In not A or B and C, which operator is evaluated last?

Why: not first, then and, then or.

Exam-style open questions (2)

Open question · Lecture 4

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.
Open question · Lecture 4

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.