Day 5 - pre-class assignment

Goals for today's pre-class assignment

  • 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 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 3. 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 [ ]:
# Display a specific YouTube video, with a given width and height.  
# WE STRONGLY RECOMMEND that you can watch the video in full-screen mode
# (much higher resolution) by clicking the little box in the bottom-right 
# corner of the video.

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

Question 1: 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 2: 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

Question 3: What questions do you have, if any, about any of the topics discussed in this assignment after watching the videos, reading the link to the Python tutorial, and trying to write the programs?

Put your answer here!

Question 4: Do you have any further questions or comments about this material, or anything else that's going on in class?

Put your answer here!

Question 5: How long did this assignment take you to complete?

Put your answer here!

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 5, and upload it there.

See you in class!


In [ ]: