Day 6 - pre-class assignment

Goals for today's pre-class assignment

  • Use the NumPy module to manipulate arrays of data
  • Write a program using if statements and Boolean logic
  • Create loops that use the for and while statements

Assignment instructions

Watch the videos below, read through the materials linked to below the videos as well as Section 4.1 through 4.3 of the Python Tutorial, and complete the assigned programming problems. Please get started early, and come to office hours if you have any questions!

Recall that to make notebook cells that have Python code in them do something, hold down the 'shift' key and then press the 'enter' key (you'll have to do this to get the YouTube videos to run). To edit a cell (to add answers, for example) you double-click on the cell, add your text, and then enter it by holding down 'shift' and pressing 'enter'

This assignment is due by 11:59 p.m. the day before class, and should be uploaded into the "Pre-class assignments" dropbox folder for Day 6. Submission instructions can be found at the end of the notebook.


In [ ]:
# Imports the functionality that we need to display YouTube videos in 
# a Jupyter Notebook.  
# You need to run this cell before you run ANY of the YouTube videos.

from IPython.display import YouTubeVideo

In [ ]:
# Don't forget to watch the video in full-screen mode!

YouTubeVideo("BTXyE3KLIOs",width=640,height=360)  # numpy

Some useful numpy references

Question 1: In the cell below, import the numpy module and then create two arrays with the same number of elements in each one (pick the number of elements and the value of each yourself, making sure they're numbers and not strings!). Then, add those arrays together and store it in a third array, and print it out. Sort the values of the third array, and print that out again.


In [ ]:
import numpy as np

# put your code here!

Question 2: Now let's use numpy and pyplot together (you learned about pyplot in a prior class). We're going to use numpy's linspace method (which you can learn about by typing this:

np.linspace?

in the cell below. Use linspace to create an array called x with 10 values ranging from x=0 to $4\pi$, and then a second array called y that is the sine of x. Then, plot x vs. y with a red line!

Hint 1: use np.sin(), which you can find more about by typing np.sin? in the cell below

Hint 2: look at the pre-class notebook from previous classes - what did you do to make plots there? Alternately, check out the Pyplot tutorial.


In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt

# put your code here!

In [ ]:
# Don't forget to watch the video in full-screen mode!

YouTubeVideo("cozbOliNwSs",width=640,height=360)  # Boolean logic and if statements

Question 3: Write a program that compares three variables v1, v2, and v3 (not necessarily the variable names) and does the following using if statements:

  1. If v1 is equal to v2, print "woohoo!"
  2. If v1 has the smallest value of the three (i.e., v1 is less than both v2 and v3), print out "v1 is tiny!"
  3. As with (2), but add a nested if statement to print out a statement saying whether v2 or v3 is the largest value.

Try several values of v1, v2 and v3 and ensure that you see the correct behavior.


In [ ]:
# write your program here, using multiple cells if necessary (adding extra cells using
# the 'Cell' menu at the top of this notebook).  Don't forget that you can execute 
# your program by holding down 'shift' and pressing 'enter' in each cell!

In [ ]:
# Don't forget to watch the video in full-screen mode!

YouTubeVideo("VnTN5sFIPD0",width=640,height=360)  # loops in python

Question 4: Write two small programs that do the following:

  1. Using range() and a for loop, print out the numbers from 4 to 16 by steps of two (i.e., 2, 4, ... 16), adding up all of the numbers you have printed out.
  2. Write a while loop that does exactly the same thing.
  3. Create two numpy arrays with 10 elements, one using np.arange() and one using np.ones(), and use np.nditer() to iterate over the two arrays and print out their values.

In [ ]:
# Program 1 here

In [ ]:
# Program 2 here

In [ ]:
# Program 3 here

Assignment wrapup

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment!


In [ ]:
from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://goo.gl/forms/F1MvFMDpIWPScchr2?embedded=true" 
	width="80%" 
	height="1200px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

Congratulations, you're done!

Submit this assignment by uploading it to the course Desire2Learn web page. Go to the "Pre-class assignments" folder, find the dropbox link for Day 6, and upload it there.

See you in class!