GG274 Homework 1: A Python Intro#

Purpose of Homework#

Welcome to your first weekly homework assignment! You are encouraged to refer to lecture content and liberally use course resources such as the discussion board and office hours.

Homeworks build off of content explored in lecture and in lab and give you an opportunity to practice with the material. Homeworks will contain tasks that focus on tools and knowledge described in lecture and lab that week. Data science knowledge is often cumulative, and you will be asked to refer to prior weeks’ content (such as loading in datasets, which is something you’re going to do a lot of in this course).

Homeworks may also be continuations of the shorter labs done during that week, and will feature a relatively greater emphasis on application of tools, inquiry of concepts, and interpretation of your data science findings. Homeworks will feature a mix of coding and written response questions.

Logistics#

Due date: The homework is due 23:59 on Monday, January 19. (Normally the homework would be due before the next class, but we know students are still adding courses.)

You will submit your work on MarkUs. To submit your work:

  1. Download this file (Homework_1.ipynb) from JupyterHub. (See our JupyterHub Guide for detailed instructions.)

  2. Submit this file to MarkUs under the hw1 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: Filling in some Python code#

This week we started learning Python by introducing variables and a few operators.

For this homework, we’re going to keep things short and ask you to define a few variables and fill in some operations to learn how to write a run very simple Python code in Jupyter notebooks.

In the cell below, replace the ellipsis (...) with the number of courses you’re taking this semester.

num_courses = ...  # Replace the ... with your number of courses

# We've provided this code to check your work. Try running this cell!
print(f"You are taking {num_courses} course(s).")

You should see something like You are taking 5 course(s). in the output when you run the cell.

Next, we have two new variables, first_name and last_name. Replace each ... with your first and last name, respectively.

Be sure to surround your name with quotes, e.g. "David", not just David. This tells Python that your name should be interpreted as data (i.e., a name) rather than code or a variable.

first_name = ...  # Replace the ... with your first name, surrounded by quotes
last_name = ...   # Replace the ... with your last name, surrounded by quotes

# We've provided this code to check your work. Try running this cell!
print(f"Hello {first_name} {last_name}, and welcome to GGR274!")

Did you see a welcome message with your name appear? If not, please ask for help if you get stuck!

Task 2: A bit of computing#

Now, let’s see how we can use Python like a calculator, where we write code instead of pushing buttons. Consider the following problem:

At the time of writing, the population of Canada is 41,548,505, and the population of Ontario is 16,212,685 (Statistics Canada). What percentage of Canada’s total population lives in Ontario?

To answer this problem, we need to do the following: divide Ontario’s population by Canada’s population, and then (to convert to a percentage), multiply the result by 100.

While you could do this on a calculator, we want you to use Python instead! In the cell below, define two variables:

  • canada_pop that stores the population of Canada as an int

  • ontario_pop that stores the population of Ontario as an int

You can copy and paste the population numbers from the above paragraph, except you’ll need to delete the commas, since Python expects just a sequence of digits for a number.

Now, using canada_pop and ontario_pop compute the percentage of Canada’s population that lives in Ontario, rounded to two decimal places, and store the result in a variable called ontario_percentage.

Remember from the lecture how to assign and use variables:

var1 = 3
var2 = 2
five = var1 + var2

We also discussed the round() function which you can use to round ontario_percentage.

You’ll find the following table of Python arithmetic operations helpful (more on this in next week’s class):

Operation

Symbol

Example Python code

Addition

+

3 + 2

Subtraction

-

3 - 2

Multiplication

*

3 * 2

Division

/

3 / 2

# Create the three variables as described above



# We've provided this code to check your work. Try running this cell!
print(f"Ontario's population is {ontario_pop} and Canada's population is {canada_pop}")
print(f"Ontario has {ontario_percentage}% of Canada's Total Population")

Note: you should see a percentage with two of decimal places.

Task 3: Written Response#

Finally, we want to give you practice using Markdown to create text inside a Jupyter notebook.

Create a new Markdown cell (same process as Lab 1), and inside the cell write answers to the following questions:

  1. What kinds of data are you interested in working with this semester?

  2. What are your first impressions of our course software: Python, Jupyter notebooks, and JupyterHub?

  3. What are you looking forward to in GGR274?

  4. What are you looking forward to be able to do after you complete GGR274?