day 7 python
day 7 python
DAY 7
TOPIC – 67
day 9 Goals : what we will make by the end of the day
dicitionary
Demo
________________________________________________________________________________
_
Topic – 68
the python Dicitionary : Deep Dive
A dictionary in Python functions similarly to a dictionary in real life. It's a data structure that allows us to associate a key
to a value and pair the two pieces of data together.
This is how you create a dictionary in Python:
# An example dictionary
colours = {
"apple": "red",
"pear": "green",
"banana": "yellow"
}
print(colours["pear"])
#Will print "green"
my_empty_dictionary = {}
colours["peach"] = "pink"
colours["apple"] = "green"
This is how to loop through a dictionary and print all the keys:
This is how to loop through a dictionary and print all the values:
example
# Creating a dictionary
programming_dictionary = {
"Bug": "An error in a program that prevents the program from running as
expected.",
"Function": "A piece of code that you can easily call over and over again.",
}
# Retrieving a value from a dictionary
print(programming_dictionary["Function"])
# Adding more items to a dictionary
programming_dictionary["Loop"] = "The action of doing something over and over
again."
# Creating an empty dictionary
empty_dictionary = {}
# Wipe an existing dictionary
# programming_dictionary = {}
# print(programming_dictionary)
# Edit an item in a dictionary
programming_dictionary["Bug"] = "A moth in your computer."
# print(programming_dictionary)
# Loop through a dictionary
for key in programming_dictionary:
print(key)
print(programming_dictionary[key])
coading exercise
Grading Program
**DO NOT** modify lines 1-7 to change the existing student_scores dictionary.
Topic -69
nesting Lists and Dicitionaries
You can mix and match various data types to achieve your desired structure.
my_dictionary = {
key1: [List],
key2: Value,
}
PAUSE 1
See if you can figure out how to print out "Lille" from the nested List called travel_log.
travel_log = {
"France": ["Paris", "Lille", "Dijon"],
"Germany": ["Stuttgart", "Berlin"],
}
Hint 1
To get this part: ["Paris", "Lille", "Dijon"] You would need: travel_log["France"]
PAUSE 2
Do you remember how to get items that are nested deeply in a list? Try to print "D" from the list nested_list.
Hint 2
nested_list[2]
nested_list[2][1]
my_dictionary = {
key1: Value,
key2: {Key: Value, Key: Value},
}
PAUSE 3
Figure out how to print out "Stuttgart" from the following list:
travel_log = {
"France": {
"cities_visited": ["Paris", "Lille", "Dijon"],
"total_visits": 12
},
"Germany": {
"cities_visited": ["Berlin", "Hamburg", "Stuttgart"],
"total_visits": 5
},
}
capitals = {
"France": "Paris",
"Germany": "Berlin",
}
# Nested List in Dictionary
# travel_log = {
# "France": ["Paris", "Lille", "Dijon"],
# "Germany": ["Stuttgart", "Berlin"],
# }
# print Lille
# print(travel_log["France"][1])
nested_list = ["A", "B", ["C", "D"]]
# print(nested_list[2][1])
# Nested dictionary in a dictionary
travel_log = {
"France": {
"cities_visited": ["Paris", "Lille", "Dijon"],
"total_visits": 12
},
"Germany": {
"cities_visited": ["Berlin", "Hamburg", "Stuttgart"],
"total_visits": 5
},
}
print(travel_log["Germany"]["cities_visited"][2])
Topic – 70
The goal is to build a blind auction program.
Demo
https://appbrewery.github.io/python-day9-demo/
Functionality
•Each person writes their name and bid.
•The program asks if there are others who need to bid. If so, then the computer clears the output (prints several
blank lines) then loops back to asking name and bid.
•Each person's name and bid are saved to a dictionary.
•Once all participants have placed their bid, the program works out who has the highest bid and prints it.
Hint 1
Hint 2
The values that come from the input() function are Strings, you'll need to use the int() function to convert it to a number.
Flowchart
If you want to see my flowchart, you can download it here.
START
Show logo from art.py
Ask for Name input
Ask for Bid Price
Add Name and Bid into a dictionary as the
key and value.
Ask if there are other
users who want to bid
Yes
Clear the screen
Find the highest bid
in the dictionary and
declare them as the
winner
No
Copy as Image
Clear Default
Style