Python Introduction
Contents
Python Introduction¶
This is a demo assignment that is openly available for the Data Science in Practice Course.
How to complete assignments¶
Whenever you see:
# YOUR CODE HERE
raise NotImplementedError()
You need to replace (meaning: delete) these lines of code with some code that answers the questions and meets the specified criteria. Make sure you remove the ‘raise’ line when you do this (or your notebook will raise an error, regardless of any other code, and thus fail the grading tests).
You should write the answer to the questions in those cells (the ones with # YOUR CODE HERE
), but you can also add extra cells to explore / investigate things if you need / want to.
Any cell with assert
statements in it is a test cell. You should not try to change or delete these cells. Note that there might be more than one assert that tests a particular question.
If a test does fail, reading the error that is printed out should let you know which test failed, which may be useful for fixing it.
Note that some cells, including the test cells, may be read only, which means they won’t let you edit them. If you cannot edit a cell - that is normal, and you shouldn’t need to edit that cell.
All outside packages/modules that will be used will be specified. You may not use other libraries in the assignments.
Finally, note that questions have points as specified in the detailed instructions.
Introduction¶
The purpose of this assignment is to make sure you have the tools you’ll need to use for COGS108. Notably, we’ll be using Python, Jupyter notebooks, and git/GitHub. Since we’re using datahub, you won’t need a local version of Jupyter or Python on your computer. So, for this assignment, we’ll focus on getting you set up on GitHub.
Most assignments will be completed completely within the Jupyter Notebook and submitted on datahub; however, for this first assignment, we want you to be comfortable in GitHub. Tasks to complete Part 1 below will require work outside of this notebook.
Python (5 points)¶
This part of the assignment is focused on some practice with Python, and with practicing working with the format of the assignments.
This class assumes some prior knowledge of Python. In the following questions, you will need to work with basic (standard library) data types (floats, lists, dictionaries, etc.) and control flow (conditionals, loops, functions, etc). If the questions in this section are totally unfamiliar to you, you may need to revisit some practice materials to catch up with some of the programming.
Through these questions, we will also prompt you to use a couple slightly more advanced standard library functions (for example, enumerate
and zip
), that may be new to you.
Each question should be answerable with a relatively small number of lines of code, up to about 5-7 lines at most.
If you are having any trouble, remember to visit the course tutorials (https://github.com/COGS108/Tutorials). Assignment questions often follow the structure of examples provided in the tutorials, and a large number of relevant links and external materials are also indexed in the tutorials.
# PRINTING VARIABLES
# A reminder that you can (and should) print and check variables as you go.
# This allows you to check what values they hold, and debug if anything unexpected happens.
# Define a variable
math_result = 2 * 4
# Print out the value(s) of a variable.
print(math_result)
Q1: Defining Variables (0.25 points)
Create the following variables:
a variable called
my_int
that stores the integer 29a variable called
my_float
that stores the float 7.29a variable called
my_string
that stores the string ‘COGS108’a variable called
my_bool
that stores the booleanTrue
# Create the variables
# YOUR CODE HERE
raise NotImplementedError()
# Tests that check that each variable is of the right type
assert isinstance(my_int, int)
assert isinstance(my_float, float)
assert isinstance(my_string, str)
assert isinstance(my_bool, bool)
Q2: Defining Variables: Lists & Tuples (0.25 points)
Define a list called var_a
, that contains individual letters a-j (inclusively).
Define a tuple called var_b
, that contains the numbers 1-10 (inclusively).
# YOUR CODE HERE
raise NotImplementedError()
# These tests check the variables are defined
assert var_a
assert var_b
# These tests check that the variables are the right data types
assert isinstance(var_a, list)
assert isinstance(var_b, tuple)
# These tests check the variables have the right values
assert var_a == ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
assert var_b == (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Q3: Defining Variables: Dictionaries (0.5 points)
Create a Python dict called dictionary
where the keys are the elements in var_a
and the values are the corresponding elements in var_b
. (Note: Use var_a
and var_b
you created above.)
Hints:
The
zip
function may be useful.You might also make use of a Python dictionary comprehension.
# YOUR CODE HERE
raise NotImplementedError()
assert dictionary
# There are also some hidden tests that check the contents of dictionary
Q4: Value Comparisons (0.5 points)
Store values in the variables comp_val_1
, comp_val_2
,comp_val_3
, and comp_val_4
such that the assert cell will pass silently (meaning the tests will pass and not produce an error).
# YOUR CODE HERE
raise NotImplementedError()
assert comp_val_1 < comp_val_2
assert comp_val_3 != comp_val_4
assert comp_val_4 >= comp_val_1
Q5: Control Flow (0.5 points)
Loop through the provided list my_list
. For each element, check if it is an even number. If the element is an even number, append the INDEX of that element to the list inds
.
Note that you are adding the index to inds
, not the element itself. (Reminder: Python uses zero-based indexing, so the index of the first element in the list is 0.)
Hints:
To check if a number is even, you can use the modulo
%
operator.To loop through an iterable, keeping track of the index, you can use the
enumerate
function.
# These variables are provided to you.
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
inds = []
# YOUR CODE HERE
raise NotImplementedError()
assert inds
# There is also a hidden test that checks the contents of 'inds'
Q6: Indexing (0.5 points)
Using the four lists provided in the cell below, complete the following indexing:
Use forward indexing to store the second value in
list_1
toindex_1
Use negative indexing to store the last value in
list_2
toindex_2
Store the first three values of
list_3
toindex_3
Store the last two values of
list_4
toindex_4
# provided for you
list_1 = [10, 20, 30, 40]
list_2 = [13, 15, 17, 19, 21, 23]
list_3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
list_4 = [21, 9, 98, 289, 938]
# YOUR CODE HERE
raise NotImplementedError()
assert isinstance(index_1, int)
assert isinstance(index_2, int)
assert isinstance(index_3, list)
assert isinstance(index_4, list)
Q7: Looping through Dictionaries (0.75 points)
Using the students
dictionary provided below, write a for loop that loops across the dictionary and collects all subject numbers (ex. ‘S2’) where the dictionary value is False
.
Imagine, for example, the dictionary indicates whether a student has completed an assignment, and we wanted to get a list of the students who had not yet completed the assignment.
To answer this question, use a for loop across the students
dictionary. You then need to get the associated value in each iteration, and check if it is True
. If it is True
, you can use continue
to skip ahead to the next iteration. Otherwise, append the subject number (i.e. ‘S2’) to a list called incomplete
.
# This dictionary provided for you
students = {
'S1' : True,
'S2' : False,
'S3' : True,
'S4' : False,
}
# YOUR CODE HERE
raise NotImplementedError()
assert isinstance(incomplete, list)
assert len(incomplete) == 2
Q8: Functions I (0.5 points)
Write a function return_odd
that will take a list as its input and return a list of all the odd values as its output.
Note that this differs from what you did above in two ways: (1) it returns the values, not the index, and (2) it’s looking for odd values, not even.
For example:
>>> return_odd([1, 2, 3])
[1, 3]
>>> return_odd([2, 4, 6])
[]
# YOUR CODE HERE
raise NotImplementedError()
assert return_odd([2, 4, 6]) == []
assert return_odd([1, 2, 3]) == [1,3]
Q9: Functions II (0.5 points)
Write a function squared_diff
that takes two number inputs and returns the squared difference of the two numbers i.e., \((a - b)^2\). For example:
>>> squared_diff(2, 4)
4
>>> squared_diff(10, 1)
81
# YOUR CODE HERE
raise NotImplementedError()
assert squared_diff(2, 4) == 4
assert squared_diff(10, 1) == 81
# There are also hidden tests that check more cases of squared_diff
Q10: Putting it all together (0.75 points)
Here, we’ll update the values in dictionary
, storing the output in a dictionary called other_dictionary
. This new dictionary will have the same keys, but some values will be updated.
The values in other_dictionary
should be updated, such that if the value in the original dictionary
is…
odd: update the the value stored in the dictionary to store the squared difference of the original value and ‘10’. Remember, you created a function to do this above.)
even: store the original value (from
dictionary
).
Hints:
to loop through key-value pairs in a dictionary, check out the
.items
methodYou created a
squared_diff
function above.
# run this cell to remind yourself what is stored in `dictionary`
dictionary.items()
# YOUR CODE HERE
raise NotImplementedError()
assert other_dictionary
# There are hidden tests that check the contents of 'other_dictionary'.
The End!¶
This is the end of the first assignment!
The goal here was to check your basic Python understanding. From here, assignments will be more data science centric, using lots of pandas
and working with data!
Have a look back over your answers, and also make sure to Restart & Run All
from the kernel menu to double check that everything is working properly. You can also use the ‘Validate’ button above, which runs your notebook from top to bottom and checks to ensure all assert
statements pass silently. When you are ready, submit on datahub!