Chapter 1.
Introduction
2025-2026 COMP1117B Computer Programming
Dr. Hengshuang Zhao (hszhao@[Link]) & Dr. H.F. Ting (hfting@[Link])
School of Computing and Data Science
The University of Hong Kong
Computer programming
Computer Programming
is NOT just about writing codes…
problem solving
It is about
– the study of using computer technologies
to solve real-life problems.
2
Computer applications
Application software, also known as application or app, is computer
software which is designed to help the user to perform specific tasks.
Online booking applications for the cinema
One stop booking across multiple cinemas
Smartcard payment application Electronic wallet 3
Computer applications
Google map applications + GPS Google map floor plan (indoor
data (e.g., route planning) positioning system, visual
positioning service)
Google street view
Next dimension of Google Earth Google visual positioning system
[Link] combined with AR technology (2018)
[Link]
Google’s Trekker Project 4
We are looking for…
Innovation, creativity, vision
Acquire fundamental Computer
Programming skill and
implement your ideas!
IDEA Skill set
5
Technology is always evolving
QR-Code enabled virtual stores.
Physical stores for online service. Posters of
products with QR code in hallway, subway/
metro station.
If the poster knows about me and
displays my favorite toys to my
mum and dad, it will be great :P
Tesco (South Korea, subway/ metro) 6
Our future?
Check out the following technologies that
will certainly change our future world.
Artificial learning / machine learning technology
IoT, mobile and wearable devices
Self driving car / driverless car
Cloud technologies
3D printing technologies
7
The world of Computer Science
Applications everywhere
Internet and WWW applications
Electronic commerce applications
Wireless and mobile applications
Database applications
8
The world of Computer Science
Applications everywhere
Computer game applications
Multimedia applications
Computer Vision, Computer Graphics
Artificial intelligence applications
many more … [Link]
Innovative AI Projects
9
Section 1.1
Computer?
Programming?
Understanding how computers work.
10
Basic concepts
Note that “computer” is not only referring to
your personal computer (PC), it can be…
The computer inside your mobile phone.
The computer inside your digital camera.
The computer in your video game console.
Question: What are the common
components of these “computers”?
11
Hardware
The actual physical parts that make
up a computer.
1. Central Processing Unit (CPU)
The “brain” of the computer.
Follows instructions in a program.
Performs simple tasks like
Question: Do you know how is the CPU speed measured?
Addition, subtraction,
multiplication and division. Answer : Clock-rate is the fundamental rate
in cycles per second at which a computer
Move data from one performs its most basic operations such as
memory location to another. adding two numbers.
100MHz = 100 Million cycles per second,
1GHz = 1 Billion cycles per second.
12
Hardware
The actual physical parts that make
up a computer.
2. Main memory
Main Memory
Long list of memory cells. Address 1
Address 2
Address 3
Each memory cell has an address (a number)
…
that gives its position in the memory.
0 0 0 0 0 0 0 1
Each memory cell consists of 8
…
digits (1 byte).
Volatile – information will be lost Information is represented as “0”s
when the computer is switched off. and “1”s in main memory.
13
Hardware
The actual physical parts that make
up a computer.
3. Secondary memory
Non-volatile – information will not be lost
even after the computer is switched off.
For permanent storage of data in units of files.
An internal Hard Disk
E.g., hard disks, thumb drives
Flash hard drive External Hard Disk USB flash drive
14
Hardware
The actual physical parts that make
up a computer.
4. Input devices
Any device that allows a user to communicate
information with the computer.
5. Output devices
Any device that allows a computer to communicate
information to the user.
15
Hardware
What is a computer program?
Processer CPU Input A computer program is a sequence
device(s)
of instructions written to perform a
Fast specified task with a computer.
Output
Main memory device(s)
Slow
Secondary Compute 2+3=?
memory
16
Computer program
When a Program is being executed
2 Processer CPU Input 1 The program is loaded into the main memory.
Compute 2+3=5 device(s) 2 The CPU reads the program instructions and
Fast process the program data.
3 3 The output of the program can be written to the
3
Output main memory, secondary memory, or displayed
Main memory device(s) through the output device(s).
Compute 2+3=?
Question: A CPU can only process data and codes represented as
“0”s and “1”s, so how can it execute a program? A program is
NOT written using “0”s and “1”s!
3 Slow 1
Answer: We need a translation tool to help to translate
Secondary Compute 2+3=? a program written by you, to the “0”s and “1”s machine
memory language that the CPU understand. 17
Programming languages
Machine language
CPU can only understand machine language (machine code).
Well defined instructions and data executed directly by
computer's central processing unit (CPU).
?
Machine Human
language language
0111000100001111 Write a program that computes
the addition of two integers.
1001110110110001
1110000100111110
19
Programming languages
Assembly language
A low-level programming language.
Defined by the hardware manufacturer, so every kind of
computer has its own unique assembler language.
Need to translate to machine code for CPU to execute. The
translation program is called an assembler.
Machine Assembly Human
language language language
0111000100001111 LOAD A Write a program that computes
the addition of two integers.
1001110110110001 ADD B
1110000100111110 STORE C
20
Programming languages
High-level programming languages
Resemble human languages.
Use more complicated instructions than the CPU can follow.
Need to translate to machine code for CPU to execute.
The translation program is called an interpreter or a compiler.
Machine Assembly Programming Human
language language language language
0111000100001111 LOAD A Write a program that computes
# Python program… the addition of two integers.
1001110110110001 ADD B C = A+B
1110000100111110 STORE C
21
Interpreter v.s. compiler
Interpreter Compiler
Scans the entire program and translates it as a
Translates program one statement at a time.
whole into machine code.
It takes large amount of time to analyze the source
It takes less amount of time to analyze the source
code but the overall execution time is
code but the overall execution time is slower.
comparatively faster.
No intermediate object code is generated, hence Generates intermediate object code which further
are memory efficient. requires linking, hence requires more memory.
Continues translating the program until the first It generates the error message only after scanning
error is met, in which case it stops. the whole program.
Hence debugging is easy. Hence debugging is comparatively hard.
Programming language like Python, Ruby use
Programming language like C, C++ use compilers.
interpreters.
Reference: [Link] 22
Section 1.2
Program
Design
Algorithms vs actual coding
23
Programming strategy
Problem solving phase
Understand the problem specification.
Identify the input and output of the program.
Come up with an algorithm / strategy that solves the problem.
The solution is not be specific to any programming language.
Implementation phase
Choose a programming language (e.g., Python) and implement the
algorithm / strategy.
Execute the program on sample input (identify runtime and
logic error)
24
Example 1
Problem specification
A construction company provides a service to build a staircase with stone rods (See
the figure below) for its customers.
Suppose each stone rod is of 1 feet height, and the cost of a stone rod is $10.
What is the
cost of
constructing a
staircase of
height 1 2 3 height of n
cost $10 $30 $60 feets?
25
Example 1
n = 5
Defining n equals to a value, say 5
n is a variable in Python, which we use it to
represent the height of the staircase.
We will compute the total cost w.r.t. the value
of n initialized here.
My problem solving strategy:
Count the number of rods : it
is 1+2+…+n, and then
multiply the total number of
height 1 2 3 rods by $10.
cost $10 $30 $60
26
Example 1
n = 5
rodprice = 10
Defining rodprice equals 10
We use rodprice as another variable to
remember the price of one unit of rod.
My problem solving strategy:
Count the number of rods : it
is 1+2+…+n, and then
multiply the total number of
height 1 2 3 rods by $10.
cost $10 $30 $60
27
Example 1
n = 5
rodprice = 10
count = 0
for i in range(1,n+1):
Counting total number of rods count = count + i
count is another variable that we will initialize
as 0 at first.
Then we will use a for loop to repeatedly
accumulate 1+2+3+…+n.
After these code, count will equal to 15.
We will learn more about the for loop
syntax in upcoming chapters.
My problem solving strategy:
Count the number of rods : it
is 1+2+…+n, and then
multiply the total number of
height 1 2 3 rods by $10.
cost $10 $30 $60
28
Example 1
n = 5
rodprice = 10
count = 0
for i in range(1,n+1):
Calculate the cost count = count + i
cost = count * rodprice
cost is another variable that stores the result of
count multiplying rodprice, which means the
cost of the staircase.
After these code, cost will equal to 150.
My problem solving strategy:
Count the number of rods : it
is 1+2+…+n, and then
multiply the total number of
height 1 2 3 rods by $10.
cost $10 $30 $60
29
Example 1
n = 5
rodprice = 10
count = 0
for i in range(1,n+1):
Display the result to users count = count + i
cost = count * rodprice
Finally we display the result to users using the print("Total cost is ",cost)
print() function.
"Total cost is " is marked using double quote, so
it is regarded as text to display to users.
cost is not marked using quote, so it is a
variable name, therefore 150 will be display.
My problem solving strategy:
Count the number of rods : it
is 1+2+…+n, and then
multiply the total number of
height 1 2 3 rods by $10.
cost $10 $30 $60
30
Example 1 The total number of rods for
staircase of height of n is:
n*(n+1)
2
n
Is there
another way
n+1 to solve the
same
problem?
n n 31
Example 1 The total number of rods for
staircase of height of n is:
n = 5
rodprice = 10
cost =(n *(n + 1) / 2)*rodprice
n*(n+1)
print("Total cost is ",cost)
2
n
Therefore, there are more
than one ways (program) to
solve the same
n+1 computational problem.
It is important to come up
with a clear problem solving
strategy before we start
implementing the code ☺
n n 32
Example 2
L-shape pieces
Problem specification 24
A 2n * 2n chessboard with any one cell removed.
L-shaped pieces that can fill up 3 cells.
Let’s find a way to cover
the whole chessboard with
23
L-shape pieces, without 24
covering the removed cell.
23
33
Example 2
L-shape pieces
24
Impossible to do it without a
proper problem solving strategy…
Especially when the problem is
bigger…
23 24
23
34
Example 2
Divide Conquer?
35
Example 2
Divide Divide
Divide Divide
36
Example 2
Divide Divide
Divide Divide
37
Example 2
38
Example 2 So simple! So simple!
Great “recursive” So simple!
strategy! So simple!
In this course, we will
learn how to implement
this strategy in a Python
program so these
solution can be
generated automatically!
Success
39
Python
Programming
Python –
A Programming Language
Created by the Dutch programmer Guido
van Rossum and first released in 1991.
You may get Python and related tools via
[Link]
➢ Python has two different major versions Python2 and
Python3. And you should use Python3, e.g., Python 3.7.8
➢ Warning: Python 2 and Python 3 have significant
differences.
41
IDE
An Integrated Development Environment (IDE) is
a software that integrates the steps of
developing a program, e.g., input program, “run”
program and debug program.
When you download Python, you will also get an
IDE called IDLE (Integrated Development and
Learning Environment):
IDLE is an integrated development
environment (IDE) for editing and running
Python programs.
It has a number of features to help you
develop your Python programs including
powerful syntax highlighting.
Icon for the Python program [Link]: What you will see if
you open IDLE without any filename.
42
A Python Program
Below is a simple Python program [Link]
43
A Python Program
Below is a simple Python program [Link]
All Python programs should
have the "extension" py.
44
A Python Program
Below is a simple Python program [Link]
A comment line.
For any line, all characters following the
character # will be ignored by the Python
interpreter.
45
A Python Program
Below is a simple Python program [Link]
Display the string (i.e., the sequence of characters)
between the two quotes on the output window.
46
A Python Program
Below is a simple Python program [Link]
input( ) instructed the computer to read
a string, and we can refer this input string
later using this name.
47
How to run a Python Program?
Two modes: script mode vs. interactive mode
Script mode:
Input the whole Python program using the editor of IDLE, and
then call the ``Python shell’’ to execute the program by clicking
the “Run” button on top of the window.
Interactive mode
Input the Python statements in the Python shell directly (without
using its editor)
After each statement is input, the Python shell will execute the
statement and display its result immediately.
48
Arithmetic Operators
Python supports the following arithmetic operations:
➢addition: +
➢subtraction: -
➢multiplication: *
➢division: /
➢floor division: //
➢modulus: %
➢exponent: **
Under the interactive mode, together with these
arithmetic operators, you have a super-calculator.
49
Arithmetic Operators
Challenge: Find out what "floor division", "modulus",
"exponent" operators mean.
50
More Mathematics
51
Testing &
Debugging
Bug & Debug
A mistake in a program is called a bug, and the process of
eliminating bugs is called debugging
53
3 Kinds of Program Errors
1. Syntax errors
2. Run-time errors
3. Logic errors
54
Syntax Errors
Errors resulting from violation of the syntax (i.e.,
the grammar rules) of the programming language
55
Run-time Errors
Errors occur during the execution of a program
Many run-time errors have to do with numeric
calculations
E.g., division by zero
The computer cannot
compute (12 /0) in run-
time
56
Logic Errors
Errors due to incorrect logic flow
Logic errors are most difficult to locate as they are not reported by the
compiler, but incorrect results may be produced
E.g., using + mistakenly for multiplication
A wrong area is
computed
57
Chapter 1.
END
2025-2026 COMP1117B Computer Programming
Dr. Hengshuang Zhao (hszhao@[Link]) & Dr. H.F. Ting (hfting@[Link])
School of Computing and Data Science
The University of Hong Kong