This file contains 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 =len(file.read()) | |
fileref.close() | |
# 2. We have provided a file called emotion_words.txt that contains lines of words that describe emotions. | |
# Find the total number of words in the file and assign this value to the variable num_words. |
This file contains 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 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
#Read in the contents of the file SP500.txt which has monthly data for 2016 and 2017 | |
#about the S&P 500 closing prices as well as some other financial indicators, | |
#including the “Long Term Interest Rate”, which is interest rate paid on 10-year U.S. government bonds. | |
#Write a program that computes the average closing price (the second column, labeled SP500) | |
#and the highest long-term interest rate. Both should be computed only for the period from June 2016 through May 2017. | |
#Save the results in the variables mean_SP and max_interest | |
with open("SP500.txt", "r") as file: | |
new = file.readlines() | |
closing_price = [] |
This file contains 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 x in Junior: | |
credits = credits + Junior[x] | |
This file contains 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 | |
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" | |
file = rainfall_mi.split(",") | |
print(file) | |
num_rainy_months = 0 | |
for x in file: |
This file contains 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
#Create one conditional so that if “Friendly” is in w, then “Friendly is here!” should be assigned to the variable wrd. | |
#If it’s not, check if “Friend” is in w. If so, the string “Friend is here!” should be assigned to the variable wrd, | |
#otherwise “No variation of friend is in here.” should be assigned to the variable wrd | |
w = "Friendship is a wonderful human experience!" | |
for x in w: | |
if "Friendly" in w: | |
wrd = str("Friendly is here!") | |
elif "Friend" in w: | |
wrd = str("Friend is here!") |
This file contains 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 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 a function named total that takes a list of integers as input, | |
#and returns the total value of all those integers added together. | |
def total(lst): | |
accum=0 | |
for x in lst: | |
accum=accum+x | |
return accum | |
lst=[1,2,3,4] | |
total(lst) |
This file contains 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
#Provided is a list of tuples. Create another list called t_check that contains the third element of every tuple. | |
lst_tups = [('Articuno', 'Moltres', 'Zaptos'), ('Beedrill', 'Metapod', 'Charizard', 'Venasaur', 'Squirtle'), ('Oddish', 'Poliwag', 'Diglett', 'Bellsprout'), ('Ponyta', "Farfetch'd", "Tauros", 'Dragonite'), ('Hoothoot', 'Chikorita', 'Lanturn', 'Flaaffy', 'Unown', 'Teddiursa', 'Phanpy'), ('Loudred', 'Volbeat', 'Wailord', 'Seviper', 'Sealeo')] | |
t_check = [] | |
for x in lst_tups: | |
t_check.append(x[2]) | |
print (t_check) | |
#Below, we have provided a list of tuples. |
This file contains 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
#!/bin/bash | |
##################################################### | |
# Name: Bash CheatSheet for Mac OSX | |
# | |
# A little overlook of the Bash basics | |
# | |
# Usage: | |
# | |
# Author: J. Le Coupanec | |
# Date: 2014/11/04 |
OlderNewer