Assigment 0
Contents
17.1. Assigment 0#
Instructions: This problem set should be done individually
Answer each question in the designated space below
17.2. Write your name and simon email#
Please write names below
[Name 1]:
[email]:
NOTE:
You can edit this cell by double clicking in it and hitting
Ctrl
+enter
for it to renderThis is Mardown cell, where we type text. For more info on how to do formating on mardown please see here https://ingeh.medium.com/markdown-for-jupyter-notebooks-cheatsheet-386c05aeebed, but you don’t need to
17.3. Part A : Discussion Board#
go in ‘Welcome page’ in the course blackboard page and look for ‘Enroll in Discussion Board’.
Use your simon email (it must be your simon email) to register
(If you do not have a simon email please email me at alan.moreira@simon.rochester.edu)
Upload you headshot (closeup picture of your face) to your profile in the discussion board
you can do that by clicking on the top right, then settings, the click in the ‘+’ signal nest to your name
Answer to my “Personal introduction” post in the discussion board (search for personal introduction in the discussion board )
Do “the Economist on Python” assignment https://edstem.org/us/courses/30665/discussion/1972434
17.4. Part B#
Exercise 1: Variables
Assume you deposit 2000 at a bank at the beginning of 2019. The annual return is 5% from 2019 to 2024. How much is your 2000 worth at the end of 2024?
Assign values to the variables a, b and c by subsituting __
by the appropriate values in this example. You have to look at the expression in the coding cell under “4.Expression” to know what is appopriate
Tip: **
is the exponentiation operation 3**2=9
# this a coding cell, you run the code by hitting shift+enter or pressing the "play buttom"
#on the top
#Try running the code before doign anything to see what a coding error look like
a=__
b=__
c=__
Exercise 2: Expressions
Once you assigned the values to a, b, and c run the cell below to get the value of your networth
a*(1+b)**c
Exercise 3: Understanding sequencing
Your first two pieces of code run succefuly (I very much hope!) . You first created three variables and you then use these variables to compute an expression which returned the value of your wealth in 2024. (yay!).
Now in the cell below I want you to change the variable that represents the annual bank return from 5% to 10%
# Write down your code below
Now that you have done that, go back to the code cell in expressions, the one that says a*(1+b)**c
and recompile it by hitting shift-enter.
What did you find? write this number in the cell below
Now run the coding cell under variables and the coding cell under expressions again.
What you should have just experienced is a very important feature of how Jupyter Notebooks work
The order of the coding cells in the notebook are IRRELEVANT for coding pourposes!
What matters is the sequence that they were called. When you change the interest rate in the cell above this variable assignment substitute the earlier assignment, so when you run the expression cell again it used the most recent variable assignment.
One way to understand what is going on is to look at the little numbers in between the squares in the left of each coding cell.They tell us the sequence that each was called.
This way you know the order things were called.
Exercise 4: One important allert about the little numbers
Go back to the coding cell under “3.Variable” and change the interest to 1000%.
Don’t run the cell. What did you notice about the number in the squares? Explain in the cell below how this can be a source of confusion.
Exercise 5: Creating a cell
Which cell below?
-> to answer the question above you need to create a markdown cell (since your answer will be text)
there two way you can do this
On the top of the notebook, click on the plus signal while this cell is selected, this will create a cell below. This cell will be by default a coding cell. Now to trasnform in a Markdown cell (to write text), while having the new cell selected click on the top buttom that says “code” and select “Markdown”. There!
In colab you see that you can that you can directly choose which cell to add (it says “+Code” or “+Text”). In Colab you can also simply hover above/below the cell to see the same buttoms. Press the above to add to a cell above, or presse the buttons below ( what we want!) to add the cell below
If the cursor is activated you can hit
esc
to get out of the cell. You can also just hittingShift
+enter
or simply clickling on the white area on the left of the cell
Exercise 6: Compouding returns
Say you have 100 dollar and you invest in a stock that earned 15% in the first year and -14% in the second year.
Modify the code below so that it gives the correct answer to this question
r1=0.15
r2=-0.14
100*1+r1*1+r2
100.01
Exercise 7: Deleting a cell
Now that you did all and have all your cell properly compiled you should save by clickling on the save buttom and then import it to the class blackboard website in the Assigments tab on the left.
But before you do this, there is one more thing to do! Do you see this cell below? IT is ugly and we don’t want it there. Lets delete it!
Again two ways of doing it
Select it and click on scissors buttom at the top of the notebook ( in jupyter) or the trach can in the top right of the cell
short-cut: in jupyter click
d
twice while having the cell selected buy not active. in colab click ‘Ctrl+M’+ d
# delete me
17.5. Part c#
Exercise 1
What do you think the value of z
is after running the code below?
z = 3
z = z + 4
print("z is", z)
Answer here:
Exercise 2
Read about what the len
function does (by running len?
).
What will it produce if we give it the variable x
?
Answer here:
Check whether you were right by running the code len(x)
.
x = "something else"
# your code here -- notice the comment!
Exercise 3
We can use our introspection skills to investigate a package’s contents.
In the cell below, use tab completion to find a function from the time
module that will display the local time.
Use time.FUNC_NAME?
(where FUNC_NAME
is replaced with the
function you found) to see information about that function and
then call the function. (Hint: look for something to do with the word
local
).
import time
# your code here -- notice the comment!
Exercise 4
Try running import time as t
in the cell below, then
call the function you identified above and see if it returns the local time
Does it work?
# your code here!
Exercise 5
Create the following variables:
D
: A floating point number with the value 10,000r
: A floating point number with value 0.025T
: An integer with value 30
We will use them in a later exercise.
Use type
function on each variable together with print to show the type of each variable
# your code here!
Exercise 6
Remember the variables we created earlier?
Let’s compute the present discounted value of a payment (\( D \)) made
in \( T \) years assuming an interest rate of 2.5%. Save this value to
a new variable called PDV
and print your output.
Hint: The formula is
# your code here
Exercise 7
Verify the “trick” where the percent difference (\( \frac{x - y}{y} \)) between two numbers close to 1 can be well approximated by the difference between the log of the two numbers (\( \log(x) - \log(y) \)).
Use the numbers x
and y
below. (Hint: you will want to use the
math.log
function)
Use print so both results are displayed
x = 1.05
y = 1.02
# your code here!
Exercise 8
The code below is invalid Python code
x = 'What's wrong with this string'
Can you fix it?
Hint: Try creating a code cell below and testing things out until you find a solution.
# your code here!
Exercise 9
Using the variables x
and y
, how could you create the sentence
Hello World
?
Hint: Think about how to represent a space as a string.
x = "Hello"
y = "World"
# your code here!
Exercise 10
One of our favorite (and most frequently used) string methods is
replace
.
It substitutes all occurrences of a particular pattern with a different pattern.
For the variable test
below, use the replace
method to change the
c
to a d
.
Hint: Type test.replace?
to get some help for how to use the method
replace.
test = "abc"
# your code here!
Exercise 11
Suppose you are working with price data and encounter the value
"$6.50"
.
We recognize this as being a number representing the quantity “six dollars and fifty cents.”
However, Python interprets the value as the string
"$6.50"
. (Quiz: why is this a problem? Think about the examples above.)
In this exercise, your task is to convert the variable price
below
into a number.
Hint: Once the string is in a suitable format, you can call write
float(clean_price)
to make it a number.
price = "$6.50"
# your code here
Exercise 12
Without typing the commands, determine whether the following statements are
true or false.In particular write a list of 6 items of the type [True,False,...]
with the results you beleive are correct
Your answer:
x = 2
y = 2
z = 4
# Statement 1
x > z
# Statement 1
x == y
# Statement 3
(x < y) and (x > y)
# Statement 4
(x < y) or (x > y)
# Statement 5
(x <= y) and (x >= y)
# Statement 6
True and ((x < z) or (x < y))
Once you have evaluated whether the command is True
or False
, run the
code in Python.
# your code here!
Exercise 13
For each of the code cells below, think carefully about what you expect to be returned before evaluating the cell.
Then evaluate the cell to check your intuitions.
NOTE: For now, do not worry about what the [
and ]
mean – they
allow us to create lists which we will learn about in an upcoming lecture.
all([True, True, True])
all([False, True, False])
all([False, False, False])
any([True, True, True])
any([False, True, False])
any([False, False, False])
17.6. Submission#
Go to file and download the notebook to your machine in a designated class folder , then go to blackboard under assigment 0 and upload this file.