GGR274 Homework 2#
Logistics#
Due date: The homework is due 23:59 on Monday, January 23.
You will submit your work on MarkUs. To submit your work:
Download this file (
Homework_2.ipynb
) from JupyterHub. (See our JupyterHub Guide for detailed instructions.)Submit this file to MarkUs under the hw2 assignment. (See our MarkUs Guide for detailed instructions.) All homeworks will take place in a Jupyter notebook (like this one). When you are done, you will download this notebook and submit it to MarkUs.
Task 1: Working with numbers#
Canada has a current population of 39,401,996, which we have stored below in the variable canada_population
.
Assuming that the population grows at a rate of 1.04% per year, write code in the cell below to calculate the expected population of Canada in 2043, exactly 20 years from now.
Round your projected number to the nearest integer using the round
function, and store the final result in a variable called canada_2043
.
(As with Homework 1, we know you could use a traditional calculator to do this. Don’t! The purpose of this exercise is to help give you practice with Python.)
Hint: try using the pow
function, where pow(x, y)
calculates “x
to the power of y
”.
canada_population = 39401996
# Write your code below. You can do it in one line, or use variables to split up
# your code into multiple steps. Just make sure your final result is stored in
# canada_2043.
# The following code is provided to help test your work.
print(f"The expected population of Canada in 2043 is {canada_2043}.")
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[4], line 10
1 canada_population = 39401996
3 # Write your code below. You can do it in one line, or use variables to split up
4 # your code into multiple steps. Just make sure your final result is stored in
5 # canada_2043.
(...)
8
9 # The following code is provided to help test your work.
---> 10 print(f"The expected population of Canada in 2043 is {canada_2043}.")
NameError: name 'canada_2043' is not defined
Task 2: Working with strings#
The following cell contains a variable charter
that contains the opening sentence of the Canadian Charter of Rights and Freedoms.
Below the variable, define the following new variables:
charter_uppercase
, astr
consisting of the same text ascharter
, but written in ALL CAPS.charter_length
, anint
that stores the length (i.e., number of characters) incharter
. This includes all punctuation marks and spaces.charter_words
, alist
ofstr
that contains the text incharter
, split up at the spaces. (Note: punctuation is included in the “words”, and that’s okay. So the last word incharter_words
will be'society.'
, not'society'
.)
charter = "The Canadian Charter of Rights and Freedoms guarantees the rights and freedoms set out in it subject only to such reasonable limits prescribed by law as can be demonstrably justified in a free and democratic society."
# Replace the ... below with your code.
...
# We have provided the following code to test your work.
print(f"The contents of charter in ALL CAPS:\n {charter_uppercase}\n")
print(f"The length of charter: {charter_length}\n")
print(f"The words in charter:\n {charter_words}")
Task 3: Working with lists#
The following variable ceo_salaries
contains (some of) the top CEO salaries from Fortune 500 companies in 2021 (AFL-CIO), in descending order.
Using this list, compute:
The number of salaries contained in the list, and store it in a variable called
num_salaries
.The maximum salary contained in the list, and store it in the variable called
max_salary
. Use themax
function to calculate this.The average of the salaries contained in the list, rounded to 2 decimal places. Store this average in a variable called
avg_salary
.
ceo_salaries = [
296247749,
212701169,
178590400,
165802037,
98734394,
84428145,
65996985,
65887214,
60703627,
57923473,
53982195,
49858280,
41860263,
40823725,
40584292,
39545072,
37809810,
36128725,
35265559,
34941635
]
# Replace the ... below with your code.
...
# We have provided the following code to test your work.
print(f"The number of salaries in the list: {num_salaries}")
print(f"The maximum salary in the list: {max_salary}")
print(f"The average salary: {avg_salary}")
Task 4: Written Responses#
Create a new Markdown cell below that answers the following questions:
Explain the difference between
=
and==
in Python.Explain the difference between the string methods
str.upper()
andstr.isupper()
. (We didn’t coverstr.isupper()
in lecture; create a new code cell and try calling it to see what it does!)