Slides 2
Slides 2
Section 1
Chapter 2
1
Agenda
• Quiz 2 (on Week 1 concepts) (access code: ) 10 mins
• Arithmetic operations
• Object types
• Variables
• Error types
• Lab 2
2
Numbers
• Numbers are referred to as numeric literals
3
Arithmetic Operators
• Addition
• Subtraction
• Multiplication
• Division
• Exponentiation (𝑎! )
4
Order of Precedence
• Order of precedence
1. Terms inside parentheses
(inner to outer)
2. Exponentiation
3. Multiplication, division,
modulus
4. Addition and subtraction
5
Two Other Integer Operators
• Integer division operator
• Written //
• Modulus operator
• Written %
6
Example on // and %
• Convert 41 inches to feet & inches.
- Rule of thumb: 1 foot = 12 inches
7
The abs Function
8
The round Function
9
The math module
• The math module is a standard module in Python.
• access to math functions and numbers
• Modules are simply files with the “. py” extension containing
Python code that can be imported inside another Python
Program. In simple terms, we can consider a module to be the
same as a code library or a file that contains a set of functions
that you want to include in your application
10
ceil and floor
11
The random module
• The random module can help generate random numbers.
12
Object Types
13
Object types in Python
• Integer, float
• String
• List, tuple
•…
14
Object types after arithmetic operations
• Result of a division is always a float.
15
The int Function
• When the parameter is a number
16
The int Function
• When the parameter is not a number
17
The float Function
18
The str Function
The str function can convert other objects into strings.
19
The input Function revisited
The input function will always convert user inputs into strings.
20
The print Function revisited
• The print function displays items on the screen.
21
Variables
22
Variables
• In mathematics problems, quantities are referred to by
names
• Names given to the values are called variables
23
Variables
speed= int(input(“speed = “))
• Assignment statements
time=int(input(“time = “))
distance = speed * time
print(distance)
24
Variables
• Variable names in Python
• Begin with letter or underscore _
• Can only consist of letters, numbers, underscores
• Convention will be
• Begin with lowercase
• Use cap for additional “word” in the name
• Example: rateOfChange
25
Variables
• Variable names in Python are case-sensitive
26
Augmented Assignments
• Remember: expression on the right side of assignment
statement evaluated before assignment is made
var = var + 1
27
Augmented Assignments
28
Error Types
29
Three types of errors
• Syntax errors
• Runtime errors
• Logic errors
30
Syntax Errors
• Grammatical and punctuation errors are called syntax
errors.
31
Runtime Errors
• Errors discovered while
program is running
called runtime errors or
exceptions
• when invalid data are
input or when a file
cannot be accessed
32
Logic Error
• Occurs when a program does not perform the way it was intended
• Example
33
Numeric Objects in Memory
• The variable n is said to reference (or point to) the number 5 in the
memory location.
• When the second line of code is executed, Python sets aside a new
memory location to hold the number 7 and redirects the variable n to
point to the new memory location.
• The number 5 in memory is said to be orphaned or abandoned. Python
will eventually remove the orphaned number from memory with a
process called garbage collection.
34
Lab 2
35
Example A: Days → Weeks & Days
• Let the user enter the number of days.
• Convert the input to # of weeks and # of days.
36
Example A: Days → Weeks & Days
37
Example B: Future value
Write a program to calculate the future value of a savings account in
5 years. The account currently has $1000. Let the user set the annual
interest rate. Assume the interest is compounded annually.
38
Example B: Future value
Let PV be the initial amount, 𝑟 be the annual rate. The future value after 𝑛 years is
FV = PV 1 + 𝑟 !
39
Example C: Present value
• Your employer will award you a lump sum loyalty bonus of $100,000 at the end of
your fifth year’s employment. Write a program to calculate the present value of
the bonus. Let the user set the annual interest rate. Assume the interest is
compounded annually.
• Economic meaning: in order to provide the 100k bonus in 5 years, your employer
can set aside $86260.88 today in a bank account that earns a 3% annual interest
(assuming the rate is fixed).
40
Example C: Present value
FV
PV = &
1+𝑟
41
Classwork 2: Future value of
annuity(deadline: 11:59pm Sep 17)
Assume you and your employer each contribute HKD 500 to your Mandatory Provident Fund (MPF)
at the end of every month (HKD 1,000 in total). Suppose the beginning account balance is zero, and
the return is compounded monthly. Write a Python program to calculate the future value of the
MPF.
1) Let the user set a target monthly rate of return and the number of months.
2) Round the answer to the nearest dollar.
42
Classwork 2: Ordinary annuity
An ordinary annuity is a series of future cash flows paid out evenly at the end of each period
(e.g., monthly, annually).
43
Classwork 2: Future value of annuity
44
Classwork 2: Future value of annuity
• Assume the month-end contribution amount is 𝑐. The monthly rate is
𝑟, then the total value in 𝑛 months is
" !#$
𝟏+𝒓 𝒏−𝟏
FV = 𝑐 + 𝑐 1 + 𝑟 + 𝑐 1 + 𝑟 + ⋯+ 𝑐 1 + 𝑟 =𝒄⋅
𝒓
45
Classwork 2: Hint
#Step 1: Get user inputs.
#Note the object type associated with the input function.
46
Kahoot
• Go to https://kahoot.it/
47