#Conner Kahn
#12/14/21
#I learned about dictionaries
# Create a dictionary of contacts; names as keys, phone numbers as values
contacts = {"Suresh Datta": "345-555-0101", "Colette Browning": "483-555-0119", "Skey Homsi": "485-555-0195"}
# Ask user for a name, then display the number
name = input("Enter a name: ")
# If name is not in the contacts dictionary, a KeyError exception will be raised
number = contacts[name]
print("Number is: {:s}".format(number))
# Create a dictionary of contacts; names as keys, phone numbers as values
contacts = {"Suresh Datta": "345-555-0101", "Colette Browning": "483-555-0119", "Skey Homsi": "485-555-0195"}
# Ask user for a name, then display the number
name = input("Enter a name: ")
# If name is not in the contacts dictionary, the exception message will be displayed
try:
number = contacts[name]
print("Number is: {:s}".format(number))
except KeyError as exception_object:
print("{:s} was not found in contacts".format(name))
# Create a dictionary of contacts; names as keys, phone numbers as values
contacts = {"Suresh Datta": "345-555-0101", "Colette Browning": "483-555-0119", "Skey Homsi": "485-555-0195"}
# Ask user for a name and number
name = input("Enter a name: ")
number = input("Enter a number: ")
# If the name exists in the dictionary, the number will be updated
# If the name does NOT exist in the dictionary, a new name:number pair will be added
contacts[name] = number
print("Updated contact:", contacts)
# Create a dictionary of contacts; names as keys, phone numbers as values
contacts = {"Suresh Datta": "345-555-0101", "Colette Browning": "483-555-0119", "Skey Homsi": "485-555-0195"}
# Ask user for a name and number
name = input("Enter a name: ")
# If the name is not in the contacts dictionary, display the exception message
try:
number = contacts.pop(name)
print("{:s}: {:s} was deleted from contacts".format(name, number))
except KeyError as exception_object:
print("{:s} was not found in contacts".format(name))
print("Updated contact:", contacts)
# Create a dictionary of grocery items and associated prices
groceries = {'Bread':2.26, 'Milk':3.62, 'Chocolate':1.59}
# Display the price for the item name
item = 'Bread'
print("{} price = {:.2f}".format(item, groceries[item]))
# Add a new key:value pair to the dictionary
groceries['Banana']= 1.00
print('Adding Banana:')
print(groceries)
# Modify a dictionary element
groceries['Banana'] = 1.10
print('Modifying Banana:')
print(groceries)
# Remove a dictionary element
print("Removing: '{}':{:.2f}".format('Banana', groceries.pop('Banana')))
print(groceries)
# [ ] The `data` list contains information about a company's employees.
# Use the `data` list and an appropriate loop to create a dictionary of employees.
# Use IDs (as keys) and names (as values).
# Ignore the email addresses for now.
# The created dictionary should look like:
# {57394: 'Suresh Datta', 48539: 'Colette Browning', 58302: 'Skye Homsi', 48502: 'Hiroto Yamaguchi', 48291: 'Tobias Ledford', 48293: 'Jin Xu', 23945: 'Joana Dias', 85823: 'Alton Derosa'}
data = [["Suresh Datta", 57394, "suresh@example.com"], ["Colette Browning", 48539, "colette@example.com"], ["Skye Homsi", 58302, "skye@example.com"], ["Hiroto Yamaguchi", 48502, "hiroto@example.com"], ["Tobias Ledford", 48291, "tobias@example.com", "Tamara Babic", 58201, "tamara@example.com"], ["Jin Xu", 48293, "jin@example.com"], ["Joana Dias", 23945, "joana@example.com"], ["Alton Derosa", 85823, "alton@example.com"]]
dicdata = {}
for x in data:
dicdata[x[0]] = x[1]
print(dicdata)
# [ ] Use the `records` dictionary in a program that 1. asks the user for an ID and 2. prints the name of the associated employee.
# Display an appropriate message if the ID is not found in the dictionary.
records = {57394: 'Suresh Datta', 48539: 'Colette Browning', 58302: 'Skye Homsi', 48502: 'Hiroto Yamaguchi', 48291: 'Tobias Ledford', 48293: 'Jin Xu', 23945: 'Joana Dias', 85823: 'Alton Derosa'}
# Ask user for an ID, then display the name
idQ = input("please input an id number")
# If the ID is not in the dictionary, display the exception message
try:
print(records[int(idQ)])
except KeyError:
print("sorry that ID wasnt found")
# [ ] Use the `records` dictionary in a program to 1. ask the user for an ID then 2. delete the employee record associated with the ID
# The program should display an appropriate message if the ID is not found in the dictionary.
# hint: Try/Catch may be helpful here
records = {57394: 'Suresh Datta', 48539: 'Colette Browning', 58302: 'Skye Homsi', 48502: 'Hiroto Yamaguchi', 48291: 'Tobias Ledford', 48293: 'Jin Xu', 23945: 'Joana Dias', 85823: 'Alton Derosa'}
# ask user for an ID
idQ = input("please input an id number")
idQ = int(idQ)
# if ID is not in the dictionary, the exception message will be displayed
try:
deletedID = records.pop(idQ)
print(idQ, records[idQ], "Was Deleted")
except KeyError:
print(idQ, "Wasn't in our records")
D = {'Name':'Skye', 'Age':35, 'Temperature':98.7, 'Last Name': 'Babic'}
# Iterate over the keys of D
for key in D.keys():
print("D[{}] = '{}'".format(key, D[key]))
D = {'Name':'Skye', 'Age':35, 'Temperature':98.7, 'Last Name': 'Babic'}
# Iterate over the sorted keys of D
# Keys sorted alphabetically because they are all strings
for key in sorted(D.keys()):
print("D[{}] = '{}'".format(key, D[key]))
D = {'Name':'Skye', 'Age':35, 'Temperature':98.7, 'Last Name': 'Babic'}
for value in D.values():
print(value)
D = {'Name':'Skye', 'Age':35, 'Temperature':98.7, 'Last Name': 'Babic'}
for (key, value) in D.items():
print(key,':', value)
D = {'Name':'Skye', 'Age':35, 'Temperature':98.7, 'Last Name': 'Babic'}
for key, value in D.items():
print(key,':', value)
# Create a dictionary of grocery items and associated prices
groceries = {'Bread':2.26, 'Milk':3.62, 'Chocolate':1.59}
# Display the price for the items in a sorted order
for item in sorted(groceries.keys()):
print("{} = {:.2f}".format(item, groceries[item]))
# [ ] The `data` list contains information about a company's employees
# Use the `data` list and an appropriate loop to create a dictionary of
# IDs (as keys): [name, email] (as values)
# The resulting dictionary should look like:
# {57394: ['Suresh Datta', 'suresh@example.com'], 48539: ['Colette Browning', 'colette@example.com'], 58302: ['Skye Homsi', 'skye@example.com'], 48502: ['Hiroto Yamaguchi', 'hiroto@example.com'], 48291: ['Tobias Ledford', 'tobias@example.com'], 48293: ['Jin Xu', 'jin@example.com'], 23945: ['Joana Dias', 'joana@example.com'], 85823: ['Alton Derosa', 'alton@example.com']}
data = [["Suresh Datta", 57394, "suresh@example.com"], ["Colette Browning", 48539, "colette@example.com"], ["Skye Homsi", 58302, "skye@example.com"], ["Hiroto Yamaguchi", 48502, "hiroto@example.com"], ["Tobias Ledford", 48291, "tobias@example.com", "Tamara Babic", 58201, "tamara@example.com"], ["Jin Xu", 48293, "jin@example.com"], ["Joana Dias", 23945, "joana@example.com"], ["Alton Derosa", 85823, "alton@example.com"]]
dictiondata = {}
for x in data:
y = []
y.append(x[0])
y.append(x[2])
dictiondata[x[1]] = y
print(dictiondata)
# [ ] Write a program to display the content of the `records` dictionary as shown here
# Make sure to display the header the dictionary makes sense to the viewer
# Note the IDs are sorted in an ascending order
'''
Name | ID | Email
________________________________________________________
Joana Dias | 23945 | joana@example.com
Tobias Ledford | 48291 | tobias@example.com
Jin Xu | 48293 | jin@example.com
Hiroto Yamaguchi | 48502 | hiroto@example.com
Colette Browning | 48539 | colette@example.com
Suresh Datta | 57394 | suresh@example.com
Skye Homsi | 58302 | skye@example.com
Alton Derosa | 85823 | alton@example.com
'''
records = {57394: ['Suresh Datta', 'suresh@example.com'], 48539: ['Colette Browning', 'colette@example.com'], 58302: ['Skye Homsi', 'skye@example.com'], 48502: ['Hiroto Yamaguchi', 'hiroto@example.com'], 48291: ['Tobias Ledford', 'tobias@example.com'], 48293: ['Jin Xu', 'jin@example.com'], 23945: ['Joana Dias', 'joana@example.com'], 85823: ['Alton Derosa', 'alton@example.com']}
# Display the header
print('{:^20s} {:^20s} {:>20s}'.format('Name','ID','Email'))
# Display the data (sorted by ID)
for x in sorted(records.items()):
a = x[1][0]
b = str(x[0])
c = x[1][1]
print('{:^20s} {:^20s} {:>20s}'.format(a,b,c))
# [ ] The company's domain has changed from (example.com) to (example.org)
# Write a program to modify the email addresses in the `records` dictionary to reflect this change
records = {57394: ['Suresh Datta', 'suresh@example.com'], 48539: ['Colette Browning', 'colette@example.com'], 58302: ['Skye Homsi', 'skye@example.com'], 48502: ['Hiroto Yamaguchi', 'hiroto@example.com'], 48291: ['Tobias Ledford', 'tobias@example.com'], 48293: ['Jin Xu', 'jin@example.com'], 23945: ['Joana Dias', 'joana@example.com'], 85823: ['Alton Derosa', 'alton@example.com']}
for x in sorted(records.items()):
y = x[1][1].replace('.com', '.org')
x[1][1] = y
# Display the updated dictionary
print('{:^20s} {:^20s} {:>20s}'.format('Name','ID','Email'))
for x in sorted(records.items()):
a = x[1][0]
b = str(x[0])
c = x[1][1]
print('{:^20s} {:^20s} {:>20s}'.format(a,b,c))
# [ ] You want to send a mass email to all company employees, so you need a list of all the email addresses in `records`
# Write a program to extract the email addresses from the `records` dictionary and store them in a list
# The output list should look like:
# ['suresh@example.com', 'colette@example.com', 'skye@example.com', 'hiroto@example.com', 'tobias@example.com', 'jin@example.com', 'joana@example.com', 'alton@example.com']
# Hint: use the `.values()` method
records = {57394: ['Suresh Datta', 'suresh@example.com'], 48539: ['Colette Browning', 'colette@example.com'], 58302: ['Skye Homsi', 'skye@example.com'], 48502: ['Hiroto Yamaguchi', 'hiroto@example.com'], 48291: ['Tobias Ledford', 'tobias@example.com'], 48293: ['Jin Xu', 'jin@example.com'], 23945: ['Joana Dias', 'joana@example.com'], 85823: ['Alton Derosa', 'alton@example.com']}
emails = []
for x in records.items():
emails.append(x[1][1])
print(emails)
# Create a dictionary of grocery items and associated prices
groceries = {'Bread':2.26, 'Milk':3.62, 'Chocolate':1.59}
item = input("Please enter an item name: ")
# Using .keys()
if (item in groceries.keys()):
print("Price of {} is: ${:4.2f}".format(item, groceries[item]))
elif (item not in groceries):
print("Price not in the dictionary")
# Create a dictionary of grocery items and associated prices
groceries = {'Bread':2.26, 'Milk':3.62, 'Chocolate':1.59}
item = input("Please enter a item: ")
# Without using .keys()
if (item in groceries):
print("Price of {} is: ${:4.2f}".format(item, groceries[item]))
elif (item not in groceries):
print("Price not in the dictionary")
# Create a dictionary of grocery items and associated prices
groceries = {'Bread':2.26, 'Milk':3.62, 'Chocolate':1.59}
price = float(input("Please enter an exact price: "))
if (price in groceries.values()):
print("There is a matching grocery")
else:
print("There are no groceries matching this price")
# Create 2 equal but not identical dictionaries
D1 = {0:'number 0', 1:'number 1', 2:'number 2'}
D2 = {1:'number 1', 0:'number 0', 2:'number 2'}
print("Equality: D1 == D2 ?", D1 == D2)
print("Identity: D1 is D2 ?", D1 is D2)
# Create 2 equal and identical dictionaries
D1 = {0:'number 0', 1:'number 1', 2:'number 2'}
D2 = D1
D1[0] = 'changed number'
print("Equality: D1 == D2 ?", D1 == D2)
print("Identity: D1 is D2 ?", D1 is D2)
print("D2 after changing D1:", D2)
# Create a dictionary of grocery items and associated prices
groceries = {'Bread':2.26, 'Milk':3.62, 'Chocolate':1.59}
# The length of a dictionary = how many key:value pairs it has
print("{} groceries total".format(len(groceries)))
# Write a program that can ONLY update the PRICE of an existing grocery item in the `groceries` dictionary.
# The item and updated price should be entered by the user.
# If the user enters a new item, the program should NOT create a new dictionary item.
# It should instead display an error message.
groceries = {'Bread':2.26, 'Milk':3.62, 'Chocolate':1.59}
x = input("please input a grocery item")
if x in groceries:
y = input(("please input a new price for", x))
groceries[x] = y
else:
print("ERROR, value not found in dictionary")
print(groceries)
# [ ] Write a program to find out the number of employees stored in `records`.
records = {57394: ['Suresh Datta', 'suresh@example.com'], 48539: ['Colette Browning', 'colette@example.com'], 58302: ['Skye Homsi', 'skye@example.com'], 48502: ['Hiroto Yamaguchi', 'hiroto@example.com'], 48291: ['Tobias Ledford', 'tobias@example.com'], 48293: ['Jin Xu', 'jin@example.com'], 23945: ['Joana Dias', 'joana@example.com'], 85823: ['Alton Derosa', 'alton@example.com']}
total = 0
for x in records.items():
print(x[1][0])
total += 1
print(total)