Task 1 - Colors of the Rainbow
# A. Make a list called colors that stores 7 strings, each a color of the rainbow
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
# B. Then, print the following string, using list indices to access the specific colors:
# "The colors of the rainbow are red, orange, yellow, green, blue, indigo, and violet"
# String concatenation might get a bit complex to read: try using an f-string for string interpolation!
print(f"Some colors are {colors[2]} and {colors[4]}")
# C. Change two of the colors to more descriptive names, such as "Lightning Yellow" or "Deep Sea Blue"
colors[2] = 'Lightning Yellow'
colors[4] = "Deep Sea Blue"
colors
# D. Print the two new color names using list indices to access them
print(colors[2])
print(colors[4])
# E. Print the colors variable to see the difference between printing a specific item and printing the list itself
print(colors)
Task 2 - The Wait List
# You will have to research some new methods to use for these challenges
# You can use print statements to investigate whether your changes have succeeded as expected
candidates = ["Alice", "Boris", "Carmen"]
# A. A promising new applicant, Diego, needs to be added to the end of the list
candidates.append("Diego")
candidates
# B. Alice has informed us of a typo on their application: change their name to "Alyce"
candidates[0] = "Alyce"
print(candidates[0])
print(candidates)
# C. One of our generous donors has a nephew, Aaron, who should be added to the very front of the list
candidates.insert(0,"Aaron")
candidates
# D. Boris's parents are concerned. Use the .index() method to print a message explaining Boris's place in the list
search = "Boris"
print(f"{search} is number {candidates.index(search)+1} on the list")
# E. Carmen is upset by the priority given to relatives of donors, and has withdrawn their application.
# Use the .remove() method to remove them from the list.
candidates.remove("Carmen")
candidates
# F. Use a method to remove the very first student in the list, as they have now been accepted!
candidates.pop(0)
candidates
# G. DeSean has submitted an impressive personal statement. Place them at index 1 by using .insert()
candidates.insert(1,"DeSean")
candidates
# H. We've been audited by the Board for Educational Fairness! Print the list of candidates.
# It should look like this: ["Alyce", "DeSean", Boris", "Diego"]
candidates
Task 3 - The Realtors
# Use the house dictionary for the following challenges
house = {
"address": "123 Fake Street",
"price": 750000,
"bedrooms": 3,
"lat/long": (38.5387, 122.6955),
"available": True
}
# A. Buyers can't find the house without a zip code! Add a zip_code property equal to "95404"
house['zipcode'] = "95404"
house
# B. Our web designers want a statement reading: "This 3 bedroom house costs 250000"
# Print that statement by accessing the values from the dictionary: do NOT "hard-code" the string!
print(f"This {house['bedrooms']} bedroom house costs ${house['price']:,}")
# C. We're getting a lot of interest in the house! Let's increase the price by 10%
print(house['price'])
house['price'] = round(house['price']*1.1)
print(house['price'])
# D. Buyers are surprised at how small the house is. Let's add a square_feet property equal to 750
house['square_feet'] = 750
print(house['square_feet'])
# E. We want to show off the value per foot. Print a message explaining the price per square foot.
print(f"The price per square foot is ${house['price']/house['square_feet']}")
# F. After 2 long hours on the market, the house has finally sold! Change availability to False. Add a status property equal to "sold"
house['available']=False
print(house['available'])
# G. Print the entire listing. You should see a dictionary similar to the following:
# {address: "123 Fake Street", price: 825000, bedrooms: 3, zip_code: "95404" square_feet: 750, status: "sold"}
print(house)
Task 4 - Python Librarian
# start with this dictionary:
authors = {
"Arundhati Roy": {
"genre": "literary fiction",
"books": [
"The God of Small Things",
"The Ministry of Utmost Happiness"
],
"active": True
},
"Brandon Sanderson": {
"genre": "fantasy",
"books": [
"The Way of Kings",
"Words of Radiance",
"Oathbringer"
],
"active": True,
"phone": {
"home": "(281) 330-8004",
"work": "(877) CASH-NOW"
}
},
"Kobo Abe": {
"genre": "absurdist fiction",
"books": [
"The Woman in the Dunes",
"The Face of Another"
],
"active": False
},
}
# A. Access and print Arundhati Roy's first book
print(authors.keys())
print(authors['Arundhati Roy'].keys())
print(authors['Arundhati Roy']['books'][0])
# B. We need to contact Brandon at work: access and print their work phone number
print(authors['Brandon Sanderson'].keys())
print(authors['Brandon Sanderson']["phone"])
print(authors['Brandon Sanderson']["phone"]['work'])
# C. Create a list composed of all the genres available in our library
list_genres = []
for people in authors.keys():
list_genres.append(authors[people]['genre'])
print(list_genres)
# D. Create a list of all books available in our library
list_books = []
for people in authors.keys():
for books in authors[people]['books']:
list_books.append(books)
print(list_books)
# E. Arundhati has decided to expand their range of genres and tackle non-fiction
# Modify their "genre" property to now be a list that also includes non-fiction
type(authors['Arundhati Roy']['genre'])
authors['Arundhati Roy']['genre']=[authors['Arundhati Roy']['genre'],'non-fiction']
print(authors['Arundhati Roy']['genre'])
# F. Brandon Sanderson is retiring. Change their active property to False.
# BONUS: This means we should no longer contact them. Delete their "phone" property
authors['Brandon Sanderson']['active'] = False
del authors['Brandon Sanderson']['phone']
print(authors['Brandon Sanderson'])
# G. Add a new author to our collection of your choice. Make sure they have:
# i. A genre property equal to a string or list of strings
# ii. A books property equal to a list of strings
# iii. An active property equal to a boolean
# iv. Another property that is also a dictionary with one or more properties
authors['Anthony Doerr']={}
authors['Anthony Doerr']['genre']='Historical Fiction'
print(type(authors['Anthony Doerr']['genre']))
authors['Anthony Doerr']['books'] = ['All the Light We Cannot See', 'Four Seasons in Rome', 'Memory Wall']
print(type(authors['Anthony Doerr']['books']))
authors['Anthony Doerr']['active']=True
print(type(authors['Anthony Doerr']['active']))
authors['Anthony Doerr']['ratings']={'All the Light We Cannot See':4.3, 'Four Seasons in Rome':3.9, 'Memory Wall':4.1}
print(type(authors['Anthony Doerr']['ratings']))
authors