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!
range_1 = [0,1,2,3,4,5,6]
for i in range_1:
print(f" colors: {colors[i]}")
# 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"
print(f" {colors[2]} & {colors[4]}")
# D. Print the two new color names using list indices to access them
print(f" {colors[2]} & {colors[4]}")
# E. Print the colors variable to see the difference between printing a specific item and printing the list itself
print(f" {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")
print(candidates)
# B. Alice has informed us of a typo on their application: change their name to "Alyce"
candidates[0] = "Alyce"
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")
print(candidates)
# D. Boris's parents are concerned. Use the .index() method to print a message explaining Boris's place in the list
print(f" Mr and Mrs Boris your son is currently {candidates.index('Boris') +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')
print(candidates)
# F. Use a method to remove the very first student in the list, as they have now been accepted!
candidates.pop(0)
print(candidates)
# G. DeSean has submitted an impressive personal statement. Place them at index 1 by using .insert()
candidates.insert(1,"G. DeSean")
print(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"]
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
print(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%
house['price'] = house['price'] * 1.1
house['price']
# D. Buyers are surprised at how small the house is. Let's add a square_feet property equal to 750
house['square_ft'] = 750
# 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_ft is ${house['price']/house['square_ft']}")
# 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
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
# 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
authors['Arundhati Roy']['books'][0]
# B. We need to contact Brandon at work: access and print their work phone number
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["JK Rolwing"] = {
"genre": "Fiction",
"books": ["HP 1", "HP 1", "HP 3"],
"active": False,
"popular_characters": {
"HP": "A Wizard",
"Ron": "A Weasley",
"Hagrid": "A gaint"
}
}
authors