This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # rainfall_mi is a string that contains the average number of inches of rainfall in Michigan for every month (in inches) with every month separated by a comma. | |
| # Write code to compute the number of months that have more than 3 inches of rainfall. | |
| # Store the result in the variable num_rainy_months. In other words, count the number of items with values > 3.0. | |
| # Hard-coded answers will receive no credit. | |
| rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85" | |
| rainfall_mi_split = rainfall_mi.split(",") | |
| num_rainy_months = 0 | |
| for x in rainfall_mi_split: | |
| x = float(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The variable sentence stores a string. | |
| # Write code to determine how many words in sentence start and end with the same letter, including one-letter words. | |
| # Store the result in the variable same_letter_count. | |
| sentence = "students flock to the arb for a variety of outdoor activities such as jogging and picnicking" | |
| # Write your code here. | |
| same_letter_count = sum(w[0] == w[-1] for w in sentence.split()) | |
| print(same_letter_count) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Write code to count the number of strings in list items that have the character w in it. | |
| # Assign that number to the variable acc_num. | |
| # HINT 1: Use the accumulation pattern! | |
| # HINT 2: the in operator checks whether a substring is present in a string. | |
| # Hard-coded answers will receive no credit. | |
| items = ["whirring", "wow!", "calendar", "wry", "glass", "", "llama","tumultuous","owing"] | |
| acc_num = 0 | |
| for i in items: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Write code that counts the number of words in sentence that contain either an “a” or an “e”. | |
| # Store the result in the variable num_a_or_e. | |
| # Note 1: be sure to not double-count words that contain both an a and an e. | |
| # HINT 1: Use the in operator. | |
| # HINT 2: You can either use or or elif. | |
| # Hard-coded answers will receive no credit. | |
| sentence = "python is a high level general purpose programming language that can be applied to many different classes of problems." | |
| num_a_or_e = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels. | |
| # For this problem, vowels are only a, e, i, o, and u. Hint: use the in operator with vowels. | |
| s = "singing in the rain and playing in the rain are two entirely different situations but both can be fun" | |
| vowels = ['a','e','i','o','u'] | |
| # Write your code here. | |
| num_vowels = sum([1 for i in s if i in vowels]) | |
| print(num_vowels) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1. Write code to add ‘horseback riding’ to the third position (i.e., right before volleyball) in the list sports. | |
| sports = ['cricket', 'football', 'volleyball', 'baseball', 'softball', 'track and field', 'curling', 'ping pong', 'hockey'] | |
| sports.insert(2, 'horseback riding') | |
| # 2. Write code to take ‘London’ out of the list trav_dest. | |
| trav_dest = ['Beirut', 'Milan', 'Pittsburgh', 'Buenos Aires', 'Nairobi', 'Kathmandu', 'Osaka', 'London', 'Melbourne'] | |
| trav_dest.pop(7) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1. Below are a set of scores that students have received in the past semester. | |
| # Write code to determine how many are 90 or above and assign that result to the value a_scores. | |
| scores = "67 80 90 78 93 20 79 89 96 97 92 88 79 68 58 90 98 100 79 74 83 88 80 86 85 70 90 100" | |
| scores_split = scores.split(" ") | |
| a_scores = 0 | |
| for x in scores_split: | |
| x = float(x) | |
| if x >= 90: | |
| a_scores += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1. The textfile, travel_plans.txt, contains the summer travel plans for someone with some commentary. | |
| # Find the total number of characters in the file and save to the variable num. | |
| fileref = open("travel_plans.txt","r") | |
| num = 0 | |
| for i in fileref: | |
| num += len(i) | |
| fileref.close() | |
| # 2. We have provided a file called emotion_words.txt that contains lines of words that describe emotions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1. At the halfway point during the Rio Olympics, the United States had 70 medals, Great Britain had 38 medals, China had 45 medals, Russia had 30 medals, and Germany had 17 medals. Create a dictionary assigned to the variable medal_count with the country names as the keys and the number of medals the country had as each key’s value. | |
| medal_count = {'United States': 70, 'Great Britain': 38, 'China': 45, 'Russia': 30, 'Germany': 17} | |
| print(medal_count) | |
| # 2. Given the dictionary swimmers, add an additional key-value pair to the dictionary with "Phelps" as the key and the integer 23 as the value. Do not rewrite the entire dictionary. | |
| swimmers = {'Manuel':4, 'Lochte':12, 'Adrian':7, 'Ledecky':5, 'Dirado':4} | |
| swimmers['Phelps'] = 23 | |
| print(swimmers) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1. The dictionary Junior shows a schedule for a junior year semester. | |
| # The key is the course name and the value is the number of credits. | |
| # Find the total number of credits taken this semester and assign it to the variable credits. | |
| # Do not hardcode this – use dictionary accumulation! | |
| Junior = {'SI 206':4, 'SI 310':4, 'BL 300':3, 'TO 313':3, 'BCOM 350':1, 'MO 300':3} | |
| credits = 0 | |
| for v in Junior.values(): | |
| credits += v | |
| print(credits) |