0% found this document useful (0 votes)
36 views47 pages

Slides 2

Here are the key steps to solve this problem: 1. Get the target monthly interest rate (r) and number of months (n) as inputs from the user. Use input() which returns a string. 2. Calculate the monthly contribution amount (c) which is 1000. 3. Use the formula for future value of an ordinary annuity to calculate the total future value. 4. Round the result and display it to the user. The key differences between this and Example B are: - This involves regular monthly contributions vs a lump sum initial amount - It uses the formula for future value of an annuity vs a single deposit Let me know if any part of the solution

Uploaded by

yeunghc519
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views47 pages

Slides 2

Here are the key steps to solve this problem: 1. Get the target monthly interest rate (r) and number of months (n) as inputs from the user. Use input() which returns a string. 2. Calculate the monthly contribution amount (c) which is 1000. 3. Use the formula for future value of an ordinary annuity to calculate the total future value. 4. Round the result and display it to the user. The key differences between this and Example B are: - This involves regular monthly contributions vs a lump sum initial amount - It uses the formula for future value of an annuity vs a single deposit Let me know if any part of the solution

Uploaded by

yeunghc519
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Numbers

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

• Two Types of numbers


• Number without a decimal point is called an int
• -3, 0, 3
• Number with a decimal point is called a float
• -3.2, 0.00, 3.87
3.0 = float

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

• Hence, 41 inches = 3 feet and 5 inches.

7
The abs Function

8
The round Function

The value of round(n, r) is the number n rounded to r


decimal places. The argument r can be omitted. If
so, n is rounded to a whole number

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.

• The result of any other arithmetic


operation is a float as long as at
least one of the numbers is a float.

15
The int Function
• When the parameter is a number

The int function leaves integers unchanged, and converts floating-point


numbers to integers by discarding their decimal part.

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

A Program that uses speed & time to calculate distance

23
Variables
speed= int(input(“speed = “))

• Assignment statements
time=int(input(“time = “))
distance = speed * time
print(distance)

• Expression on right evaluated


• That value assigned to variable

24
Variables
• Variable names in Python
• Begin with letter or underscore _
• Can only consist of letters, numbers, underscores

• Recommend using descriptive variable names

• 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

• There are thirty-five words, called reserved words (or keywords)


• Have special meanings in Python (e.g. True, False)
• Cannot be used as variable names
• See Appendix B in the textbook
• Or

26
Augmented Assignments
• Remember: expression on the right side of assignment
statement evaluated before assignment is made
var = var + 1

• Python has special operator to accomplish the same thing


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

• average = firstNum + secondNum / 2

• Syntax correct, logic wrong: should be

• average = (firstNum + secondNum) / 2

• Logic errors are the most difficult type of error to locate

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.

The output should resemble the following.

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 + 𝑟 =𝒄⋅
𝒓

• Alternatively, you can use loops (more on that in later weeks).

• Q: What’s the difference between Classwork 2 and Example B?

45
Classwork 2: Hint
#Step 1: Get user inputs.
#Note the object type associated with the input function.

#Step 2: Calculate the future value following the given formula.


#Note that 𝑐 = 1000. 𝑟 and 𝑛 are given by the user.

#Step 3: Display the answer.

46
Kahoot
• Go to https://kahoot.it/

• Enter the game PIN as shown on the classroom projector.

• Enter your 8-digit student ID, E.g., 55123456


Do NOT use names.

• Kahoot is separate from the weekly quiz on Canvas. Kahoot


engagement (not merit) will partially determine class participation
scores.

47

You might also like