drinks = [
{
"type": "water",
"calories": 0,
"number_consumed": 5
},
{
"type": "orange juice",
"calories": 220,
"number_consumed": 3
},
{
"type": "gatorade",
"calories": 140,
"number_consumed": 1
}
]
[{'type': 'water', 'calories': 0, 'number_consumed': 5},
{'type': 'orange juice', 'calories': 220, 'number_consumed': 3},
{'type': 'gatorade', 'calories': 140, 'number_consumed': 1}]
# Exercise 1
# Loop through the list of dictionaries to print out the total number of calories consumed for each beverage type
# Planning:
# calories * number of units consume = total for that beverage type
# One weird trick to solving looping problems == blow off the loop, forgetaboutit
# How do we blow off the loop?
# Make a variable holding only the first item
drink = drinks[0]
drink
{'type': 'water', 'calories': 0, 'number_consumed': 5}
drinktype = drink["type"]
calories = drink["calories"]
number_consumed = drink["number_consumed"]
total_calories = number_consumed * calories
print(f"{total_calories} is the total calories from drinking {number_consumed} units of {drinktype}")
0 is the total calories from drinking 5 units of water
for drink in drinks:
drinktype = drink["type"]
calories = drink["calories"]
number_consumed = drink["number_consumed"]
total_calories = number_consumed * calories
print(f"{total_calories} is the total calories from drinking {number_consumed} units of {drinktype}")
0 is the total calories from drinking 5 units of water
660 is the total calories from drinking 3 units of orange juice
140 is the total calories from drinking 1 units of gatorade
# list of lists
x = [
[1, 2, 3],
[2, 3, 4]
]
first_row = x[0]
first_row
[1, 2, 3]
# Second element of the first row?
first_row[1]
2
# Second element of the first row?
x[0][1]
2
beatles = [
"john",
"ringo",
"george",
"paul"
]
beatles
['john', 'ringo', 'george', 'paul']
# Let's access the last character of the second string on the list
second_on_list = beatles[1]
second_on_list
'ringo'
# Let's access the last character of the second string on the list
second_on_list[-1]
'o'
beatles[1] # gives us the second string on the list
beatles[1][-1] # gives us the last character of the second stirng on the list of beatles
'o'
ds_topics = {
"fundamentals": {
"beginning": "data science pipeline",
"middle": "ds skills in demand",
"end": "git"
},
"sql": {
"beginning": "what is sql?",
"middle": "joins and subqueries",
"end": "case"
},
"python": {
"beginning": "how to run python",
"middle": "loops and data structures",
"end": "python assessment"
}
}
ds_topics
{'fundamentals': {'beginning': 'data science pipeline',
'middle': 'ds skills in demand',
'end': 'git'},
'sql': {'beginning': 'what is sql?',
'middle': 'joins and subqueries',
'end': 'case'},
'python': {'beginning': 'how to run python',
'middle': 'loops and data structures',
'end': 'python assessment'}}
{'fundamentals': {'beginning': 'data science pipeline',
'middle': 'ds skills in demand',
'end': 'git'},
'sql': {'beginning': 'what is sql?',
'middle': 'joins and subqueries',
'end': 'case'},
'python': {'beginning': 'how to run python',
'middle': 'loops and data structures',
'end': 'python assessment'}}
# Directly access the middle topic of the python module
ds_topics["python"]
{'beginning': 'how to run python',
'middle': 'loops and data structures',
'end': 'python assessment'}
python_module = ds_topics["python"]
python_module["middle"]
'loops and data structures'
ds_topics["python"]["middle"]
'loops and data structures'
{'beginning': 'how to run python',
'middle': 'loops and data structures',
'end': 'python assessment'}
ds_topics.get("python").get("middle")
'loops and data structures'
# Use a loop to output access the middle topic of the python module
# What if the data we're processing is WAY too big to read it all and we need to loop through hundreds/thousands
ds_topics
{'fundamentals': {'beginning': 'data science pipeline',
'middle': 'ds skills in demand',
'end': 'git'},
'sql': {'beginning': 'what is sql?',
'middle': 'joins and subqueries',
'end': 'case'},
'python': {'beginning': 'how to run python',
'middle': 'loops and data structures',
'end': 'python assessment'}}
dict_keys(['fundamentals', 'sql', 'python'])
# notice that .values doesn't contain "fundamentals", "sql", "python" b/c those are the keys
ds_topics.values()
dict_values([{'beginning': 'data science pipeline', 'middle': 'ds skills in demand', 'end': 'git'}, {'beginning': 'what is sql?', 'middle': 'joins and subqueries', 'end': 'case'}, {'beginning': 'how to run python', 'middle': 'loops and data structures', 'end': 'python assessment'}])
# What if we need to loop through a dictionary
for key in ds_topics.keys():
print(key)
fundamentals
sql
python
# What if we need to loop through a dictionary
for key in ds_topics.keys():
if key.lower() == "python":
middle_section = ds_topics[key]["middle"]
print(f"In the middle of the {key} lessons, we focus on {middle_section}")
In the middle of the python lessons, we focus on loops and data structures
print("Welcome to the Codeup syllabus application where you can look up topics!")
while True:
topic_choice = input("Please input the topic you want to know more about.")
if topic_choice.lower() not in ds_topics.keys():
print("That topic is not in the records")
continue
else:
print(f"Looking up more on: {topic_choice} ...")
break
while True:
module_choice = input("Please input 'beginning', 'middle', or 'end' to output which part of that module to look up")
if module_choice not in ds_topics[topic_choice].keys():
print("That module portion is not in the records")
continue
else:
selection = ds_topics[topic_choice][module_choice]
break
print(f"In the {module_choice} part of the {topic_choice} curriculum, we cover {selection}")