1
Preliminary Computer science
COMP0001
School of Science, Computing and Artificial Intelligence
Lecturer: Mr Ilenius Ildephonce
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 1 / 43
2
Lecture 5A
Elementary Programming
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 2 / 43
Contents
3
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
5 Software Development Process
6 Problem to solve
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 3 / 43
Content
4
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
5 Software Development Process
6 Problem to solve
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 4 / 43
Motivation
5
Motivation
Suppose, for example, that you need to take out a student loan. Given
the loan amount, loan term, and annual interest rate, can you write a
program to compute the monthly payment and total payment?
This lecture shows you how to write programs like this.
Along the way, you learn the basic steps that go into analyzing a
problem, designing a solution, and implementing the solution by
creating a program.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 5 / 43
Example: Area of a circle
6
Area of a circle
AreaCircle.py
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 6 / 43
Content
7
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
5 Software Development Process
6 Problem to solve
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 7 / 43
User Input
8
Reading input from the console
1 Use the input function
variable = input("Enter a string: ")
2 Use the float and int function to convert a string to a float or int.
var = float(stringVariable)
var = int(stringVariable)
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 8 / 43
Reading input from console example
9
Examples
1 Compute are with console input
AreaCircle.py
2 compute average
Average.py
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 9 / 43
Content
10
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
5 Software Development Process
6 Problem to solve
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 10 / 43
Identifiers
11
Identifiers
An identifier is a sequence of characters that consists of letters,
digits, underscores (_), and asterisk (*).
An identifier must start with a letter or an underscore. It cannot
start with a digit.
An identifier cannot be a reserved word.
(import keyword
s = keyword.kwlist)
Reserved words have special meanings in Python, which we will
later discuss.
An identifier can be of any length.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 11 / 43
Variables
12
Value assignment
# Compute the first area
radius = 1.0
area = radius * radius * 3.14159
print("The area is ", area, " for radius ", radius)
# Compute the second area
radius = 2.0
area = radius * radius * 3.14159
print("The area is ", area, " for radius ", radius)
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 12 / 43
Expression
13
Expression
x = 1 # Assign 1 to variable x
radius = 1.0 # Assign 1.0 to variable radius
# Assign the value of the expression to x
x = 5 * (3 / 2) + 3 * 2
x = y + 1 # Assign the addition of y and 1 to x
area = radius * radius * 3.14159 # Compute area
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 13 / 43
Assignment Statement
14
Assignment Statement
x = 1 # Assign 1 to x
x=x+1
i=j=k=1
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 14 / 43
Simultaneous Assignment
15
Simultaneous Assignment
multiple assignments var1, var2, ..., varn = exp1, exp2, ..., expn
Swap x with y x, y = y, x
AverageMultiAssign.py
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 15 / 43
Named Constants
16
Named Constants
The value of a variable may change during the execution of a program,
but a named constant or simply constant represents permanent data
that never changes. Python does not have a special syntax for naming
constants. You can simply create a variable to denote a constant. To
distinguish a constant from a variable, use all uppercase letters to
name a constant.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 16 / 43
Numerical Data types
17
Numeric types
1 integer: e.g., 3, 4
2 float: e.g., 3.0, 4.0
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 17 / 43
Numeric Operators
18
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 18 / 43
Reminder operator
19
Reminder operator
Remainder is very useful in programming. For example, an even
number % 2 is always 0 and an odd number % 2 is always 1. So you
can use this property to determine whether a number is even or odd.
Suppose today is Saturday and you and your friends are going to meet
in 10 days. What day is in 10 days? You can find that day is Tuesday
using the following expression:
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 19 / 43
Example
20
Write a program that obtains minutes and remaining seconds from
seconds.
DisplayTime.py
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 20 / 43
Overflow
21
Overflow
When a variable is assigned a value that is too large (in size) to be
stored, it causes overflow. For example, executing the following
statement causes overflow.
45.0 ** 1000 OverflowError: ’Result too large’
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 21 / 43
Underflow
22
Underflow
When a floating-point number is too small (i.e., too close to zero) to be
stored, it causes underflow. Python approximates it to zero. So
normally you should not be concerned with underflow.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 22 / 43
Scientific notations
23
Scientific notations
Floating-point literals can also be specified in scientific notation, for
example, 1.23456e+2, same as 1.23456e2, is equivalent to 123.456,
and 1.23456e-2 is equivalent to 0.0123456. E (or e) represents an
exponent and it can be either in lowercase or uppercase.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 23 / 43
Content
24
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
5 Software Development Process
6 Problem to solve
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 24 / 43
Arithmetic Expressions
25
Arithmetic Expressions
is translated to
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 25 / 43
Augmented assignment operators
26
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 26 / 43
Type conversion and rounding
27
datatype(value)
i.e., int(4.5) => 4
float(4) => 4.0
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 27 / 43
Example: Keep 2 Decimal points
28
Write a program that displays the sales tax with two digits after the
decimal point.
SalesTax.py
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 28 / 43
Example: Show current time
29
Write a program that displays current time in GMT in the format
hour:minute:second such as 1:45:19. The time.time() function returns
the current time in seconds with millisecond precision since the
midnight, January 1, 1970 GMT. (1970 was the year when the Unix
operating system was formally introduced.) You can use this function
to obtain the current time, and then compute the current second,
minute, and hour as follows.
CurrentTime.py
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 29 / 43
Content
30
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
5 Software Development Process
6 Problem to solve
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 30 / 43
Software Development Process
31
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 31 / 43
Requirement Specification
32
Requirement Specification
A formal process that seeks to understand the problem and document
in detail what the software system needs to do. This phase involves
close interaction between users and designers.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 32 / 43
System Analysis
33
System Analysis
Seeks to analyze the business process in terms of data flow, and to
identify the system’s input and output.
entails modeling the system’s
behavior. The model is intended to capture the essential elements of
the system and to define services to the system.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 33 / 43
System design
34
System design
The process of designing the system’s components.
This phase involves the use of many levels of abstraction to
decompose the problem into manageable components, identify
classes and interfaces, and establish relationships among the classes
and interfaces.
The essence of system analysis and design is input, process, and
output. This is called IPO.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 34 / 43
Implementation
35
Implementation
The process of translating the system design into programs. Separate
programs are written for each component and put to work together.
This phase requires the use of a programming language like Java. The
implementation involves coding, testing, and debugging.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 35 / 43
Testing
36
Testing
Ensures that the code meets the requirements specification and
weeds out bugs.
An independent team of software engineers not involved in the design
and implementation of the project usually conducts such testing.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 36 / 43
Deployment
37
Deployment
Deployment makes the project available for use.
For a Java program, this means installing it on a desktop or on the
Web.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 37 / 43
Maintenance
38
Maintenance
Maintenance is concerned with changing and improving the product.
A software product must continue to perform and improve in a
changing environment. This requires periodic upgrades of the product
to fix newly discovered bugs and incorporate changes.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 38 / 43
Content
39
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
5 Software Development Process
6 Problem to solve
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 39 / 43
Problem: Computing Loan Payments
40
This program lets the user enter the interest rate, number of years, and
loan amount, and computes monthly payment and total payment.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 40 / 43
Problem 2
41
Write a program prompts the user to enter two points, computes
their distance, and displays the distance.
Distance.py
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 41 / 43
Problem 3
42
Problem 3
Write a program that prompts the user to enter two points, computes
their distance, and displays the points and their distances in graphics.
DistanceGraphic.py
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 42 / 43
43
End.
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 5A October 8, 2021 43 / 43