Watch the videos below, read through Sections 4.1, 4.4, and 4.5 of the Python Tutorial, and complete the programming problems assigned below.
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 8. 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 [ ]:
# WATCH THE VIDEO IN FULL-SCREEN MODE
YouTubeVideo("8_wSb927nH0",width=640,height=360) # Complex 'if' statements
Question 1: In the cell below, use numpy's 'arange' method to create an array filled with all of the integers between 1 and 10 (inclusive). Loop through the array, and use if/elif/else to:
Note 1: You may need more than one if/elif/else statement to do this!
Note 2: If you have a numpy array named my_numpy_array
, you don't necessarily have to use the numpy nditer
method. You can loop using the standard python syntax as well. In other words:
for val in my_numpy_array:
print(val)
will work just fine.
In [ ]:
# put your code here.
import numpy as np
my_array = np.arange(1,11)
for val in my_array:
if val%2 == 0:
print(val, "is even")
else:
print(val, "is odd")
if val%3 == 0:
print(val, "is divisible by 3")
elif val%5 == 0:
print(val, "is divisible by 5")
else:
print(val, "wow, that's disappointing")
In [ ]:
# WATCH THE VIDEO IN FULL-SCREEN MODE
YouTubeVideo("MzZCeHB0CbE",width=640,height=360) # Complex loops
Question 2: In the space below, loop through the given array, breaking when you get to the first negative number. Print out the value you're examining after you check for negative numbers. Create a variable and set it to zero before the loop, and add each number in the list to it after the check for negative numbers. What is that variable equal to after the loop?
In [ ]:
# put your code here.
my_list = [1,3,17,23,9,-4,2,2,11,4,-7]
sum = 0
for val in my_list:
if val < 0:
break
print(val)
sum += val
print("the sum after the loop is:", sum)
Question 3: In the space below, loop through the array given above, skipping every even number with the continue statement. Print out the value you're examining after you check for even numbers. Create a variable and set it to zero before the loop, and add each number in the list to it after the check for even numbers. What is that variable equal to after the loop?
In [ ]:
# put your code here
# put your code here.
my_list = [1,3,17,23,9,-4,2,2,11,4,-7]
sum = 0
for val in my_list:
if val % 2 == 0:
continue
print(val)
sum += val
print("the sum after the loop is:", sum)
Question 4: Copy and paste your code from question #2 above and change it in two ways:
else
clause after the end of the loop (not the end of the if
statement!) that prints out "yay, success!" if the loop completes successfully, but not if it breaks.Verify that if you use the original array, the print statement in the else
clause doesn't work!
In [ ]:
# put your code here!
my_list = [1,3,17,23,9,-4,2,2,11,4,-7]
sum = 0
for val in my_list:
if val > 99: # should never be called, because the values are too small!
break
print(val)
sum += val
else:
print("yay, success!")
print("the sum after the loop is:", sum)
In [ ]:
from IPython.display import HTML
HTML(
"""
<iframe
src="?embedded=true"
width="80%"
height="1200px"
frameborder="0"
marginheight="0"
marginwidth="0">
Loading...
</iframe>
"""
)