Read pages 19-42 of chapter 2 of the book Blown To Bits.
3 * 5 + 5 15 + 5 20HINT: You can (you should!) check your final answers with python3.
For a mortgage with a principal amount of
The following Python function implements this equation to compute the final amount of a mortgage after a number of years, based on the principal and a given interest rate.
def final_amount(principal, interest_rate, years): return principal * (1 + interest_rate)**years
final_amount(1000, .1, 3)
Will we get an integer result or a floating point result? Why?
final_amount(1000, .1)
Does the function use a default value for the years, or does Python complain about this function call? Explain.
final_amount(3, .1, 1000)
Does Python report an error? Why or why not?
def final_amount(principal, interest_rate, years): print(principal * (1 + interest_rate)**years)
What value is stored in the variable amount if we execute
the following instruction? Why?
amount = final_amount(1000, .1, 3)
import math math.sqrt(-25)
Consider the following Python function definition that uses a loop:
def mystery(k, n): answer = 1 for i in range(n): answer = answer * k print(answer)
mystery(4, 6)
What does mystery(k, n) display, in general, if k and n are positive integers?
Using python3, see what happens if we replace the print function in mystery with a return statement instead:
def mystery1(k, n): answer = 1 for i in range(n): answer = answer * k return answer
Store the function in a file, then load it in python3 and call it with different positive integers and observe the results. In general, if k and n are positive integers, what does mystery1 return? What does this tell you about how the return statement works?
def mystery2(k, n): answer = 1 for i in range(n): answer = answer * k return answerIn general, if k and n are positive integers, what mathematical function does mystery2 return? What does this tell you about the behavior of the return statement in its new position?