1. Introduction to Computer Programming

1.1. Computer

1.1.1. What is a computer?

Is computer a calculator that is bigger and more advanced?

Calculator app
A calculator on a computer.

If computer is a calculator, then, is abacus the first computer invented?

Abacus 6
Abacus - an ancient mechanical computing device.

Is your smartphone a computer?
What defines a computer?

  • In addition to performing arithmetic calculations, a computer is designed in such a way that

  • we can write different programs (in a process called programming or software development)

  • for the computer to execute to perform different tasks.

1.1.2. What is the architecture of a computer?

A computer contains three main hardware components:

  • Input device

  • Processing unit

  • Output device

1.1.2.1. Peripherals

Computer peripherals
Computer Peripherals.

Input and output devices connected to a computer are called peripherals.
They allow users to interact with the computer in different ways.

Exercise Some examples of output devices are:

  • Monitor

  • Speaker

Can you give an awesome example in the following box?

  • 3D printer available at CityU

Exercise Some examples of input devices are:

  • Keyboard

  • Mouse

Can you give an awesome example?

  • 3D scanner available at CityU

Exercise Many devices are both input and output device. Can you give at least 3 examples?

  • hard disk

  • CD/DVD Rom (writable)

  • touch screen

1.1.2.2. Central Processing Unit

CPU
An Intel CPU.

The brain of a computer is its processor unit, or the Central Procesisng Unit (CPU).
It is located on the motherboard and connects to different peripherals using different connectors.

Two important components in the CPU are:

  • Arithmetic and Logic Unit (ALU): Performs arithmetics like a calculator (but for binary numbers)

  • Control Unit (CU): Directs the operations of the processor in executing a program.

Let’s run a CPU Simulator below from a GitHub project.

  • Note that all values are zeros in the RAM (Random Acess Memory) initially.

  • Under Settings, click Examples->Add two numbers. Observe that the values in the RAM have changed.

  • Click Run at the bottom right-hand corner.

%%html
<iframe src="https://tools.withcode.uk/cpu" width="800" height="800">
</iframe>

1.2. Programming

1.2.1. What is programming?

Programming is the process of writing programs.
But what is a program?

Exercise You have just seen a program written in machine language. Where is it?

The first six lines of binary sequences in the RAM. The last line Ends the program.

  • The CPU is capable of carrying out

    • a set of instructions such as addition, negation, Copy, etc.

    • some numbers stored in the RAM.

  • Both the instructions and the numbers are represented as binary sequences.

  • E.g., in Intel-based CPU, the command for addition is like 00000011 00000100

1.2.2. Why computer uses binary representation?

%%html
<iframe width="912" height="513" src="https://www.youtube.com/embed/Xpk67YzOn5w" allowfullscreen></iframe>

Exercise The first electronic computer, called Electronic Numerical Integrator and Computer (ENIAC), was programmed using binary circuitries, namely switches that can be either On or Off.

Two women operating ENIAC
Programmers controlling the switches of ENIAC.

However, it did not represent values efficiently in binary. 10 binary digits (bits) was used to represent a decimal number 0 to 9.
Indeed, how many decimals can be represented by 10 bits?

2 ** 10 # because there are that many binary sequences of length 10.
1024

As mentioned in the video, there are International Standards for representing characters:

  • ASCII (American Standard Code for Information Interchange) maps English letters and some other symbols to 8-bits (8 binary digits, also called a byte). E.g., A is 01000001.

  • Unicode can also represent characters in different languages such as Chinese, Japanese…etc.

There are additional standards to represent numbers other than non-negative integers:

Why define different standards?

  • Different standards have different benefits. ASCII requires less storage for a character, but it represents less characters.

  • Although digits are also represented in ASCII, the 2’s complement format is designed for arithmetic operations.

1.3. Different generations of programming languages

Machine language is known as the 1st Generation Programming Language.

Are we going to start with machine language? Start with learning 2’s complement and the binary codes for different instructions?

No. Programmers do not write machine codes directly because it is too hard to think in binary representations.

Instead, programmers write human-readable mnemonics such as ADD, SUB…,
called Assembly language, or the 2nd Generation Programming Language.

Motorola 6800 Assembly Language
A Code written in an assembly language.

Are you going to learn an assembly language?

Both machine language and assembly language are low-level language which

  • are difficult to write for complicated tasks (requiring many lines of code), and

  • are platform specific:

Anyone want to learn assembly languages, and write a program in many versions to support different platforms?

Probably for programmers who need to write fast or energy-efficient code such as

  • a driver that controls a 3D graphics card, and

  • a program that control a microprocessor with limited power supply.

But even in the above cases, there are often better alternatives. Play with the following microprocessor simulator:

  • Click CHOOSE A DEMO->LED.

  • Click RUN SCRIPT and observes the LED of the board.

  • Run the demos ASSEMBLY and MATH respectively and compare their capabilities.

%%html
<iframe width="900", height="1000" src="https://micropython.org/unicorn/"></iframe>

1.4. High-level Language

%%html
<iframe width="912" height="513" src="https://www.youtube.com/embed/QdVFvsCWXrA" allowfullscreen></iframe>

Programmer nowadays use human-readable language known as the 3rd generation language (3GL) or high-level language.

  • Examples includes: C, C++, Java, JavaScript, Basic, Python, PHP, …

1.4.1. What is a high-level language?

  • A code written in high-level language gets converted automatically to a low-level machine code for the desired platform.

  • Hence, it abstracts away details that can be handled by the computer (low-level code) itself.

For instance, a programmer needs not care where a value should be physically stored if the computer can find a free location automatically to store the value.

Different high-level languages can have different implementations of the conversion processes:

  • Compilation means converting a program well before executing of the program. E.g., C++ and Java programs are compiled.

  • Interpretation means converting a program on-the-fly during the execution of a program. E.g., JavaScript and Python programs are often interpreted.

Roughly speaking, compiled programs run faster but interpreted programs are more flexible and can be modified at run time.
(The truth is indeed more complicated than required for this course.)

1.4.2. What programming language will you learn?

You will learn to program using Python. The course covers:

  • Basic topics including values, variables, conditional, iterations, functions, composite data types.

  • Advanced topics that touch on functional and object-oriented programming.

  • Engineering topics such as numerical methods, optimizations, and machine learning.

See the course homepage for details.

Why Python?

%%html
<iframe width="912" height="513" src="https://www.youtube.com/embed/Y8Tko2YC5hA?end=200" allowfullscreen></iframe>

In summary:

  • Python is expressive and can get things done with fewer lines of code as compared to other languages.

  • Python is one of the most commonly used languages. It has an extensive set of libraries for Mathematics, graphics, AI, Machine Learning, etc.

  • Python is Free and Open Source, so you get to see everything and use it without restrictions.

  • Python is portable. The same code runs in different platforms without modifications.

How does a Python program look like?

# for step-by-step execution using mytutor
%reload_ext mytutor
%%mytutor -h 400
# The program here reads the cohort and reports which year the user is in
# Assumption: Input is integer no greater than 2020
import datetime  # load a library to tell the current year
cohort = input("In which year did you join CityU? ")
year = datetime.datetime.now().year - int(cohort) + 1
print("So you are a year", year, "student.")

A Python program contains statements just like sentences in natural languages.
E.g., cohort = input("In which year did you join CityU? ") is a statement that gives some value a name called cohort.

For the purpose of computations, a statement often contains expressions that evaluate to certain values.
E.g., input("In which year did you join CityU? ") is an expression with the value equal to what you input to the prompt.
That value is then given the name cohort.

Expressions can be composed of:

  • Functions such as input, now, and int, etc., which are like math functions the return some values based on its arguments, if any.

  • Literals such as the string "In which year did you join CityU? " and the integer 1. They are values you type out literally.

  • Variables such as cohort and year, which are meaningful names to values.

To help others understand the code, there are also comments that start with #.
These are descriptions meant for human to read but not to be executed by the computer.

Exercise What do you think the next generation programmimng should be?

Perhaps programming using natural languages. Write programs that people enjoy reading, like literate programming.
Indeed, Jupyter notebook is arguably a step towards this direction. See nbdev.

%%html
<iframe width="912" height="513" src="https://www.youtube.com/embed/bTkXg2LZIMQ" allowfullscreen></iframe>