CS1302 Introduction to Computer Programming
Computer¶
What is a computer?¶
A calculator that is bigger and more advanced?

Figure 1:A calculator on a computer.
If so, is abacus the first computer invented?

Figure 2:Abacus - an ancient mechanical computing device.
Is your smartphone a computer?
What defines a computer?
What is the architecture of a computer?¶
Peripherals¶

Figure 3:Computer peripherals.
Input and output devices connected to a computer are called peripherals.
They allow users to interact with the computer in different ways.
Solution to Exercise 1 #
- 3D printer available at CityU
Solution to Exercise 2 #
- 3D scanner available at CityU
Solution to Exercise 3 #
- Hard disk
- CD/DVD Rom (writable)
- Touch screen
Central Processing Unit¶

Figure 4:An Intel CPU
The brain of a computer is its processor or the Central Procesisng Unit (CPU).
It is located on the motherboard and connected 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.
To visualize how CPU works, run the CPU Simulator below.
- Note that all values are zeros in the RAM (Random Acess Memory) initially.
- Under Settings, click
ExamplesAdd two numbers. Observe that the values in the RAM have changed. - Click
Runat the bottom right-hand corner.
Programming¶
What is programming?¶
But what is a program?
Solution to Exercise 4 #
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
Add,Subtract,Store, etc., - for 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 00000011 00000100
Why computer uses binary representation?¶
Binary representation
### BEGIN SOLUTION
2 ** 10 # because there are that many binary sequences of length 10.
### END SOLUTIONAs mentioned in the video, there are International Standards for representing characters:
There are additional standards to represent numbers other than non-negative integers:
- 2's complement format for negative integers (e.g. -123)
- IEEE floating point format for floating point numbers such as .
Different generations of programming languages¶
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, ...
This is called the assembly language:

Figure 6:A Code written in an assembly language.
Are you going to learn an assembly language?
Both machine language and assembly language are low-level languages which are
- difficult to write for complicated tasks (requiring many lines of code), and
- platform-specific:
- the sets of instructions and their binary codes can be different for different types of CPUs, and
- different operating systems use different assembly languages/styles.
Anyone wants 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 DEMOLED. - Click
RUN SCRIPTand observes the LED of the board. - Run the demos
ASSEMBLYandMATHrespectively and compare their capabilities.
High-level Language¶
Programming
Programmer nowadays write in human-readable languages such as
- C/C++
- Java
- JavaScript/Typescript
- Pascal
- Basic
- HTML
- PHP
- Python
- ...
called high-level languages.
What is a high-level language?¶
- A program written in a high-level language gets converted automatically to a low-level machine code for the desired platform.
- This abstracts away low-level details that can be handled by the computer automatically.
For instance, a programmer needs not care about 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.
What programming language will you learn?¶
You will learn to program in python, covering
- basic topics including values, variables, conditional, iterations, functions, composite data types,
- advanced topics that touch on functional and object-oriented programming, and
- engineering topics such as numerical methods, optimizations, and machine learning.
Why Python?
Why python?
How does a Python program look like?
import datetime # library to obtain current year
cohort = input("In which year did you join CityU? [e.g., 2020]")
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? ")which obtains some information from user input.
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 the following objects:
- Functions such as
input,now, andint, 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 integer1. They are values you type out literally. - Variables such as
cohortandyear, which are meaningful names to values.
To help others understand the code, there are also comments that start with #.
These are meant to explain the code to human but not to be executed by the computer.
Solution to Exercise 6
Programming using natural languages. A step towards this direction: ChatGPT for coding
Write programs that people enjoy reading, like literate programming. A step towards this direction: nbdev.
Literate programming