COMP1117_01 Introduction
COMP1117_01 Introduction
1
Programming in Daily Life
2
Programming in Daily Life
Some MTR lines in Hong
Kong rely on auto-
driving. To avoid
collision or accidents, an
excellent control
program is required.
3
What is Computer Programming?
Teaching the computer to do something for you by providing a set
of instructions.
4
What is an Application?
Application software, also known as application or app, is computer
software which is designed to help the user to perform specific tasks.
8
Programming Empowerment
9
How Computers Work
10
Pre-Class Videos
Covers the very basic concepts on
– binary & data
- circuits & logic
- CPU, memory, input & output
- hardware & software
https://youtube.com/playlist?list=PLzdnOPI1iJNcsRwJhvksEo1tJqjIqWbN-&si=_PBBzfIVS_ox5WoB
11
Basic Concepts
Note that “computer” is not only referring to your personal
computer (PC), it can be…
Computer == Any device
• The computer inside your mobile phone. with computing capabilities
• The computer inside your camera.
• The computer in your video game console.
…
• Volatile – information will be lost when the
computer is switched off. 0 0 0 0 0 0 0 1
……
Information is represented as
“0”s and “1”s in main memory.
14
Hardware
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.
• E.g., hard disks, DVD/CD ROMs, thumb drives
Flash hard drive External Hard Disk DVD/CD ROMs USB flash drive
15
Hardware
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.
16
Computer Program
A computer program is a sequence
Processer CPU Input of instructions written to perform a
device(s) specified task with a computer.
Fast
Output
Main memory device(s)
Slow
Output
Main memory device(s)
Compute 2+3=?
Slow
Slow
Secondary
memory
19
Computer Program
When a Program
is being executed
Processer CPU Input • The program is loaded into the main
device(s)
Compute 2+3=5
memory.
Fast • The CPU reads the program instructions
and process the program data.
Output
Main memory device(s) • The output of the program can be
Compute 2+3=?
written to the main memory, secondary
memory, or displayed through the output
Slow device(s).
Secondary
memory
Input Processing Output
20
Computer Program
A closer look into CPU…
• Control unit (CU) - Extracts instructions from
CU
memory and decodes and executes them.
Fast
Main memory
Compute 2+3=?
Slow
Secondary
memory
21
Computer Program
Registers
A closer look into CPU…
• Control unit (CU) - Extracts instructions from
CU
memory and decodes and executes them.
Fast
• Registers - Store the instructions / data loaded
from main memory for very fast processing.
Main memory
Compute 2+3=?
Slow
Secondary
memory
22
Computer Program
Registers
ALU A closer look into CPU…
• Control unit (CU) - Extracts instructions from
CU
memory and decodes and executes them.
Fast
• Registers - Store the instructions / data loaded
from main memory for very fast processing.
Main memory
• Arithmetic logic unit (ALU) - Performs simple
Compute 2+3=?
arithmetic and logical operations, e.g. AND /OR.
Question: A CPU can only process data and codes represented as “0”s and “1”s, so
Slow how can it execute a program? A program is NOT written using “0”s and “1”s!
Secondary
memory
23
Computer Program
Registers
ALU A closer look into CPU…
• Control unit (CU) - Extracts instructions from
CU
memory and decodes and executes them.
Fast
0011001001001
• Registers - Store the instructions / data loaded
0010101011010 from main memory for very fast processing.
Main memory
• Arithmetic logic unit (ALU) - Performs simple
Compute 2+3=?
arithmetic and logical operations, e.g. AND /OR.
Question: A CPU can only process data and codes represented as “0”s and “1”s, so
Slow how can it execute a program? A program is NOT written using “0”s and “1”s!
?
Machine Human
language language
0111000100001111 Write a program that computes
1001110110110001 the addition of two integers.
1110000100111110
25
Programming Languages
Assembly language
• A low-level programming language.
• Defined by the hardware manufacturer, so every kind of computer has
its own unique assembly language.
• Need to translate to machine code for CPU to execute. The translation
program is called an assembler.
C+ + COBOL
High-level programming languages
• Resemble human languages. Python
• 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.
No intermediate object code is generated, hence it is Generates intermediate object code which further
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++, Java use
interpreters. compilers. 28
Program Design
Algorithms vs. Actual Coding
29
Let’s start with an example
If you run a 10 kilometer race in 42 minutes 42 seconds, what is your
average pace (i.e., time per km in mins & secs)?
Output Input
For this question, the input is a fixed number (42 mins 42 seconds)
If we are going to write a program to display the result on screen, the
output is some output on screen (i.e., standard output, stdout). 30
Solving the Problem
If you run a 10 kilometer race in 42 minutes 42 seconds, what is
your average pace (i.e., time per km in mins & secs)?
Solution 1
1. Convert race time to seconds (42 mins 42 secs --> 42*60 + 42 = 2562 secs)
2. Calculate pace in secs/km (= 2562/10 = 256.2 secs/km)
3. Calculate pace in mins&secs/km (convert 256.2 secs to mins&secs. How?)
4. Display the result on screen
31
Solving the Problem
If you run a 10 kilometer race in 42 minutes 42 seconds, what is
your average pace (i.e., time per km in mins & secs)?
Solution 2
1. Convert race time to minutes (42 mins 42 secs --> 42 + 42/60 = 42.7 mins)
2. Calculate pace in mins/km (= 42.7/10 = 4.27 mins/km)
3. Calculate pace in mins&secs/km (convert 4.27 mins to mins&secs. How?)
4. Display the result on screen
32
Problem Solving Phase
You have just gone through the problem solving phase,
which entails:
1. Understand the problem specification.
2. Identify the input and output of the program.
3. Come up with an algorithm (strategy) that solves the problem.
35
Summary - Programming Strategy
Problem solving phase
1. Understand the problem specification.
2. Identify the input and output of the program.
3. Come up with an algorithm (strategy) that solves the problem.
Implementation phase
1. Choose a programming language (e.g., Python) and implement the algorithm / strategy.
2. Execute the program on sample input
3. Identify runtime and logic error
4. Debug until done
36
Python Programming
37
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
https://www.python.org/downloads/
38
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 Python shell (Interactive mode): this is what
syntax highlighting. you will see if you open IDLE without any
filename.
Icon for the Python program Q4.py:
39
A Python Program
Below is a simple Python program hello.py
40
A Python Program
Below is a simple Python program hello.py
41
A Python Program
Below is a simple Python program hello.py
A comment line.
For any line, all characters following the
character # will be ignored by the Python
interpreter.
42
A Python Program
Below is a simple Python program hello.py
43
A Python Program
Below is a simple Python program hello.py
44
How to run a Python Program?
Two modes: interactive mode vs script mode
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.
Google Colab, Jupyter Notebook, …
Script mode
• Edit the whole Python program (.py) using the editor of IDLE, and then call the
“Python shell” to execute the program by clicking the “Run” button at the top of
the window.
Visual Studio Code, Atom, …
45
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.
46
Arithmetic Operators
48
Testing & Debugging
49
Bug & Debug
A mistake in a program is called a bug, and the process of
eliminating bugs is called debugging.
50
3 Kinds of Program Errors
1. Syntax errors
2. Run-time errors
3. Logic errors
51
Syntax Errors
Errors resulting from violation of the syntax (i.e., the grammar
rules) of the programming language
52
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
53
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
54
Reading Assignment
Think Python, Chapters 1 & 2
55