Exercises for Module 1: Intro to Python#

This set of exercises works through some basic python functionality. Just a note that we have used some functions from a module called numpy to create the exercises, but nothing you write should need it although you can use it if you’d like. We’ll be learning more about numpy in the next module.

import numpy as np
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 import numpy as np

ModuleNotFoundError: No module named 'numpy'

1.) Write code to translate a boolean value to a string. Specifically, if the testval is True then print “Yes” and if it is False then print “No”#

testval = bool(np.random.choice([0, 1]))
# TODO: Your code here
message = None
# ...
print(message)

2.) You will be given a random integer, and your goal is to return the same value, but as a negative number. Notice, the number you are given may already be negative#

testval = np.random.random_integers(-100, 100)
# TODO: Your code here
negval = None
print(testval, negval)

3.) Given a list of random integers, return them sorted from low to high.#

NOTE: I do not want you to write your own sorting algorithm, but want you to look up how to do this using the python standard library

random_vals = np.random.random_integers(-100, 100, 10)
# TODO: Your code here
sorted_vals = None
print(sorted_vals)

4.) Given a list of US locations with the format: “CityName, StateAbbrev” filter out any that are not in Arizona (AZ).#

city_list = [
    "New York, NY",
    "Chattanooga, TN",
    "Hobart, MN",
    "Kingman, AZ",
    "Yachats, OR",
    "Bisbee, AZ",
    "Muskogee, OK"
]
#TODO: Your code here
az_list = None
#...
print(az_list)

5.) The following code doesn’t work - can you fix it?#

def multiply(a, b):
    # TODO: Your code in here
    a * b

print(multiply(5, 10))

6.) Time for a coding interview classic, FizzBuzz#

The rules of the game:

  • print numbers from 1 to 100

  • if the number is divisible by 3 print “Fizz”

  • if the number is divisible by 5 print “Buzz”

  • if the number is divisible by both 3 and 5 print “FizzBuzz”

  • otherwise, print the number

testvals = np.arange(1, 101)
#TODO: Your code here
# ...
for v in testvals:
    print('NONE')

7.) Write a function that takes a list of numbers and returns the mean (average) of the list#

testvals = [1,2,3,4,5,6,7,8,9,10]

def average(vals):
    #TODO: your code here
    pass

print(average(testvals))