In Class Activity - Python#
In this notebook we will try to put some of the Python knowledge you acquired into use. For each exercise, you will see some code that has some bugs or asks you to write a little program. Simply fix the code cell to make code that runs correctly, or write the code to specification. Then add a markdown cell below your answer that says what you changed and why, or explains what your code is doing in English.
Importing and using existing functions#
Problem 0: Random number generation#
The following code imports the random number functionality in Python from the numpy library and uses to do generate random numbers. Why won’t this code run?
import numpy.random as npr
print("My random number is ", numpy.random.random())
Problem 1: Random sample#
The next line should choose a random sample of 5 elements from a list. What are the two errors?
(your answer here)
Debug the code and fix the two errors!
import numpy.random as npr
my_list = list(range(100)
npr.choice(my_list,size=10,replace=False)
Problem 2: Looking things up#
In the above example when you get it to work what does the choice() function do? The documentation for this function is available by googling for “numpy.random choice”. Add your answer in a cell below. Then look up the random() function used in Problem 0. What does it do?
Writing a function#
In the problems that remain you will need to write little function that accomplish various goals. Let’s verify your understanding of functions.
Problem 3: Defining a function#
What does this function do? Write a new cell below the function definition that calls the function using five different inputs and verify the outputs match your expectation.
def my_function(x, y):
return x+y
my_function (1,2)
Problem 4: What is not great about this function?#
This function works but is not ideal, why?
def my_function_2(x, y, z):
return x+y
my_function_2 (3, 3, 7)
Problem 5: Write your own function#
Write your own custom function named catchy_name() to write a program that takes as input two arguments that are assumed to be strings. Return a new string which is the concatenations of the first two letters of each of the inputs. For example:
catchy_name("South","Houston") -> "SoHo"
catchy_name("Jennifer", "Lopez") -> "JeLo"
def catchy_name(x, y):
# your code here
pass
catchy_name("South","Houston")
catchy_name("Jennifer", "Lopez")
Strings#
Problem 6: Cat-Dog#
Write a function called cat_dog that returns True if an input string has the words cat and dog an equal number of times. For example,
cat_dog('catdog') # should return True
cat_dog('catcat') # should return false
cat_dog('1cat1cadodog') # should return true
There are many possible answers for this. One way to start is: Google ‘python count occurrences in string’ to find an existing function or method that counts the number of times a substring appears in a string.
def cat_dog(x):
# your code here
pass
cat_dog('catdog')
cat_dog('catcat')
cat_dog('1cat1cadodog')
Lists#
Problem 7: Summing#
Write a function called summing() that takes a list of numbers as input and returns the sum of all the numbers. Use a for loop to do this.
summing([1, 2, 3]) → 6
summing([5, 11, 2]) → 18
summming([7, 0, 0]) → 7
def summing(x):
# your code here
pass
summing([5, 11, 2])
summing([7, 0, 0])
The cell below will throw an error. This is ok, but explain why you get an error below:
(your answer here)
summing([1, 'a', 3])
Problem 8: Biggest difference#
Given an array of length 1 or more of ints, return the difference between the largest and smallest values in the array. Note: the built-in min(v1, v2) and max(v1, v2) functions return the smaller or larger of two values.
big_diff([10, 3, 5, 6]) → 7
big_diff([7, 2, 10, 9]) → 8
big_diff([2, 10, 7, 2]) → 8
def big_diff(x):
# your code here
pass
diff1 = big_diff([10, 3, 5, 6])
print (diff1)
diff2 = big_diff([7, 2, 10, 9])
print (diff2)
big_diff([7, 2, 10, 9])
big_diff([2, 10, 7, 2])