Lecture 5 A
Lecture 5 A
COMP0001
Lecture 5A
Elementary Programming
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
6 Problem to solve
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
6 Problem to solve
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?
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.
Area of a circle
AreaCircle.py
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
6 Problem to solve
Examples
1 Compute are with console input
AreaCircle.py
2 compute average
Average.py
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
6 Problem to solve
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.
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)
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
Assignment Statement
x = 1 # Assign 1 to x
x=x+1
i=j=k=1
Simultaneous Assignment
multiple assignments var1, var2, ..., varn = exp1, exp2, ..., expn
Swap x with y x, y = y, x
AverageMultiAssign.py
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.
Numeric types
1 integer: e.g., 3, 4
2 float: e.g., 3.0, 4.0
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:
DisplayTime.py
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.
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.
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.
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
6 Problem to solve
Arithmetic Expressions
is translated to
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
datatype(value)
Write a program that displays the sales tax with two digits after the
decimal point.
SalesTax.py
CurrentTime.py
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
6 Problem to solve
Requirement Specification
System Analysis
Seeks to analyze the business process in terms of data flow, and to
identify the system’s input and output.
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.
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.
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.
Deployment
Deployment makes the project available for use.
For a Java program, this means installing it on a desktop or on the
Web.
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.
1 Motivation
2 User Input
3 Identifiers
4 Arithmetic Expressions
6 Problem to solve
This program lets the user enter the interest rate, number of years, and
loan amount, and computes monthly payment and total payment.
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
End.