# 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)
# [x] 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'}
records= {}
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"]]
for lst in data:
records[lst[1]] = lst[0], lst[2]
print(records)
# [x] 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
ID = input('enter ID number:')
try:
print(records[ID])
except KeyError:
print('ID not found.')
# If the ID is not in the dictionary, display the exception message
# [x] 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'}
ID = input('enter ID number of employee to delete:')
try:
records.pop(ID)
print(ID,'has been fired')
except KeyError:
print('employee ID doesn\'t exist')
# ask user for an ID
# if ID is not in the dictionary, the exception message will be displayed
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]))
# [x] 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']}
employ = {}
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"]]
for lst in data:
employ[lst[1]] = [lst[0],lst[2]]
print(employ)
# [x] 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']}
print('{:^20s}|{:^20s}|{:^20s}'.format('Name','ID','Email'))
print('--------------------------------------------------------------')
for ID in records:
print('{:^20f}|{:^20s}|{:^20s}'.format(ID,records[ID][0], records[ID][1]))
# Display the header
# Display the data (sorted by ID)
# [x] 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 ID in records:
records[ID][1] = records[ID][1][0:-3] + 'org'
print(records)
print('{:^20s}|{:^20s}|{:^20s}'.format('Name','ID','Email'))
print('--------------------------------------------------------------')
for ID in records:
print('{:^20f}|{:^20s}|{:^20s}'.format(ID,records[ID][0], records[ID][1]))
# Display the updated dictionary
# Display the header
# Display the data (sorted by ID)
# [ ] 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']}
# 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}
item = input('enter grocery item to update the price')
if item in groceries:
try:
groceries[item] = int(input('enter a new price'))
except ValueError:
print('ERROR: invalid input for new price')
finally:
print(groceries)
else:
print('ERROR: item not found in grocery list')
# [x] 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']}
employ_ct = 0
for ID in records:
employ_ct += 1
print(employ_ct)