Task 1 - Colors of the Rainbow
# 2.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"The colors of the rainbow are {colors[0]}, {colors[1]}, {colors[2]}, {colors[3]}, {colors[4]}, {colors[5]}, and {colors[6]}")
# C. Change two of the colors to more descriptive names, such as "Lightning Yellow" or "Deep Sea Blue"
colors[0] = "Racecar Red"
colors[5] = "Moody Indigo"
# D. Print the two new color names using list indices to access them
print(f"Hey kids! Try our newest crayon colors, {colors[0]} and {colors[5]}!")
# 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
# 2.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")
# B. Alice has informed us of a typo on their application: change their name to "Alyce"
candidates[0] = "Alyce"
# 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")
# D. Boris's parents are concerned. Use the .index() method to print a message explaining Boris's place in the list
print(f"Boris is currently at index {candidates.index('Boris')} in 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")
# F. Use a method to remove the very first student in the list, as they have now been accepted!
candidates.pop(0)
# G. DeSean has submitted an impressive personal statement. Place them at index 1 by using .insert()
candidates.insert(1, "DeSean")
# 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"]
print(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["zip_code"] = "95404"
# 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%
house["price"] = house["price"] + house["price"] / 10
# D. Buyers are surprised at how small the house is. Let's add a square_feet property equal to 750
house["square_feet"] = 750
# E. We want to show off the value per foot. Print a message explaining the price per square foot.
print(f"This cozy home offers great value at only ${house['price']/house['square_feet']} per square foot!")
# 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["status"] = "sold"
# 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
# 2.4 Python Librarian
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
},
}
len('authors')
# A. Access and print Arundhati Roy's first book
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"]["phone"]["work"])
# C. Create a list composed of all the genres available in our library
genres = []
genres.append(authors["Arundhati Roy"]["genre"])
genres.append(authors["Brandon Sanderson"]["genre"])
genres.append(authors["Kobo Abe"]["genre"])
# D. Create a list of all books available in our library
books = []
books.append(authors["Arundhati Roy"]["books"][0])
books.append(authors["Arundhati Roy"]["books"][1])
books.append(authors["Brandon Sanderson"]["books"][0])
books.append(authors["Brandon Sanderson"]["books"][1])
books.append(authors["Brandon Sanderson"]["books"][2])
books.append(authors["Kobo Abe"]["books"][0])
books.append(authors["Kobo Abe"]["books"][1])
# 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
authors["Arundhati Roy"]["genre"] = ["literary fiction", "non-fiction"]
# 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"]
# 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["Nikolai Gogol"] = {
"genre": "Russian Literature",
"books": ["Dead Souls", "The Overcoat", "Government Inspector"],
"active": False,
"popular_characters": {
"Khlestakov": "A poser",
"Chichikov": "A swindler",
"Akaky Akakeivich": "An innocent victim, brother to all mankind"
}
}
# show the updated dictionary keys
authors.keys()
# show the first entry
authors['Arundhati Roy']
# show the whole dictionary
authors