Python 1 Review/Warmup Drills
Instructions:
Fork and clone this lab, then complete the drills below.
1. Nested for-loops
Create a new python file called warmup1.py and complete the following:
- Write a function called
Yesthat takes an int parameterexcitement_level. This should print out “Yes!”excitement_leveltimes. For example,Yes(10)should print out “Yes!” ten times. - Write a function called
Nothat takes an int parameterdisappointment_level. This should print out “No” with the number of “o”s equal todisappointment_level. For example,No(10)should print out “Noooooooooo”. - Write a function called
ClassStartTimethat takes an integer parameterstart_timewhich represents a class’s starting hour in 24-hour time. It should have the following behavior:- If the class begins after 1 pm, call your
Yesfunction with anexcitement_levelequal to the number of hours past noon. SoClassStartTime(18)(6pm) should print “Yes” 6 times, andClassStartTime(13)should print “Yes” one time. - If the class begins before 1 pm, call your
Nofunction with adisappointment_levelequal to the number of hours before 1pm. SoClassStartTime(12)should print “No” andClassStartTime(7)should print “Noooooo”.
- If the class begins after 1 pm, call your
- Write a function called
ClassSchedulethat takes a dictionary parameter calledschedule, where the key is the string name of the class, and the value is the start time of the class. For each class inschedule, print “(class) is at (start time):00.” on one line, then the result ofClassScheduleon the next line. For example
fall2015_schedule = {
'Math' : 9,
'History' : 7,
'Computer Science' : 15
}
ClassSchedule(fall2015_schedule)
should print
Math is at 9:00.
Noooo
History is at 7:00.
Noooooo
Computer Science is at 15:00.
Yes!
Yes!
Yes!
2. Loops and Lists Drills
- Given the following list:
daily_temperatures = [83, 86, 85, 88, 86, 87, 92, 93]
Write a loop that prints each item in the list.
- Write a function called
PrintTemperaturesthat takes a list parameter calleddaily_temperaturesand prints each item in the list. - Write a function called
AverageTemperaturethat takes a list parameter calleddaily_temperaturesand returns the average temperature in the list. - Write a functions called
CountHotDaysthat takes a list parameter calleddaily_temperaturesand returns the number of temperatures that are above 90. - Write a functions called
ModeTemperaturethat takes a list parameter calleddaily_temperaturesand returns the most common temperature in the list. If there is more than one mode, return the first mode found. - Write a function called
MedianTemperaturethat takes a list parameter calleddaily_temperaturesand returns the median element of the list.
3. Loops and Dictionaries Drills
Given the following dictionary:
abc_name_to_age = {
'aaron' : 30,
'betty' : 14,
'cindy' : 67,
'duane' : 18,
'edgar' : 17,
'frank' : 65
}
- Write a loop that prints out all the names in the
name_to_agedictionary. - Write a loop that prints out each person’s name along with their age in the form of “(name) is (food)”, e.g. “aaron is 30”
- Write a function called
AverageAgethat takes a dictionary parameter calledname_to_ageand returns the average age in the list. - Write a function called
CountRetiredthat takes a dictionary parameter calledname_to_ageand returns the number of people whose age is above 64. - Write a function called
Minorsthat takes a dictionary parameter calledname_to_ageand returns a list of the names of the people under 18 years old.
Given the following dictionary:
our_favorite_foods = {
'victoria' : 'pizza',
'tim' : 'sushi',
'cookie monster' : 'cookies',
'pooh' : 'honey',
'jiro' : 'sushi',
'michelangelo' : 'pizza',
'caesar' : 'pizza',
'wimpy' : 'hamburger',
'joseph' : 'frosting'
}
- Write a loop that prints out each person’s name along with their favorite food in the form of “(name) loves (food)”, e.g. “victoria loves pizza”
- Write a loop that prints out each person’s name along with their favorite food in the form of “(name) Loves (food)”, with each word capitalized, e.g. “Victoria Loves Pizza” and “Cookie Monster Loves Cookies”.
4. Creating Lists and Dictionaries
- Write a function called
Deltasthat takes a list parameter calleddaily_temperaturesand returns a list of the differences between each temperature. For example,Deltas([73, 75, 70])should return[2, -5]. - Write a function called
GetUniqueFavoritesthat takes a dictionary parameter calledname_to_favorite_foodand returns a list of all the unique favorite foods. - Write a function called
PopularFoodthat takes a dictionary parameter calledname_to_favorite_foodand returns the most frequent favorite food in the list. If there is a tie, return the first in alphabetical order. For example,PopularFood(favorite_foods)should return “pizza”.