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 17.

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#

Next week, we’ll be introducing the Python programming language more formally. For this homework, we’re going to keep things short and ask you to fill in some bits of code and run it, just to get used to the process.

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

num_courses = 5  # 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 = 'David'  # Replace the ... with your first name, surrounded by quotes
last_name = 'Liu'   # 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 39,395,674, and the population of Ontario is 15,308,317 (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, calculate this percentage, and store the result in a variable called ontario_percentage. 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. Remember from the lab that you can perform a calculation and store the result in a variable in a single line of code:

five = 3 + 2

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

# Replace the ... with an arithmetic expression to calculate the desired percentage
ontario_percentage = 15308317 / 39395674 * 100


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

Note: you should see a percentage with lots of decimal places. Don’t worry about rounding for this problem; we’ll discuss how to round numbers in Python next week.

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?