COMP1117 Computer Programming
COMP1117 Computer Programming
Computer Programming
1
Outcome base learning
Outcome Descriptions
Computational mind Able to identify possible solutions for problems based on
computer programs
Program implementation Able to implement solutions using Python
Program comprehension Able to understand program by others and participate in
larger scale system implementation.
2
Details of the courses
Textbook:
http://greenteapress.com/wp/think-python-2e/
Assessment
4 programming assignments (40%)
A1 (5%), A2 (10%), A3 (10%), A4 (15%)
Tutorial exercises (10%)
Workshop starts from Jan 25, 2022
Final exam (50%)
Instructors: H.F. Ting
TAs: Jolly Cheng, Chun Kiu Lai, Zerong Xie, Ruixing Jia and ~10 STAs
3
The Programming language Python
Created by the Dutch programmer Guido
van Rossum and first released in 1991.
You can use Python directly from your
moodle account.
You may get Python and related tools via
https://www.python.org/downloads/
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.
4
High-level Languages
Common programming languages include …
6
Bridging the gap
Some tool is needed to convert programs written in high-level language into
machine language understood by the computer. There are two methods:
Interpreter Compiler
Translates program one Scans the entire program and translates it as a
statement at a time. whole into machine code.
Python uses interpreters. Programming language like C, C++ use
compilers.
It takes less amount of time to
analyze the source code but It takes larger amount of time to analyze the
source code but the overall execution time is
the overall execution time is comparatively faster.
slower.
Continues translating the
program until the first error is It generates the error message only after
scanning the whole program. Hence debugging
met, in which case it stops.
is comparatively hard.
Hence debugging is easy.
7
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 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.
Eg. Icon for the Python program Q4.py:
8
A Python Program
Below is a simple Python program hello.py
9
A Python Program All Python programs should
have the "extension" py.
10
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.
11
A Python Program
Below is a simple Python program hello.py
12
A Python Program
Below is a simple Python program hello.py
13
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.
14
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.
15
Arithmetic operators
16
More math
17
Testing & Debugging
A mistake in a program is called a bug, and the process of
eliminating bugs is called debugging
18
Three kinds of program errors
1. Syntax errors
2. Run-time errors
3. Logic errors
19
Syntax Errors
Errors resulting from violation of the syntax (i.e., the grammar
rules) of the programming language
20
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
21
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
22