In: Computer Science
Fruits=['mango','apple','orange','banana','grape']
prices=[15,12,17,7,19]
1. Using priceDict, find the price for 'grape'.
2. Add a price of 20 for 'pomegranate'.
3. Create a sorted list of all the prices in priceDict.
4. Calculate the average of all the prices in priceDict.
5. Update the price for 'pomegranate' to be 25.
6. Mangos has just sold out. Delete 'mango' and its price from priceDict.
Python
Python code pasted below.
Fruits=["mango","apple","orange","banana","grape"]
prices=[15,12,17,7,19]
#creating an empty dictionary priceDict
priceDict={}
for i in range(len(Fruits)):
#Adding fruits and their prices to a dictionary
priceDict[Fruits[i]]=prices[i]
print("Dictionary priceDict")
print(priceDict)
print()
#Finding the price of grape from priceDict
print("Price for grape is ",priceDict['grape'])
print()
#Add a price of 20 for pomegranate
priceDict['pomegranate']=20
#Calculate a sorted list of all the prices in priceDict
print("Sorted in order of prices")
priceDict={k: v for k, v in sorted(priceDict.items(), key=lambda
item: item[1])}
print(priceDict)
print()
#calculate the average of all prices in priceDict()
sum=0
for k,v in priceDict.items():
sum=sum+v
average=sum/len(priceDict)
print("Average of all prices=",average)
print()
#update price of pomegranate to 25
priceDict['pomegranate']=25
print("priceDict after updating the price of pomegranate to
25")
print(priceDict)
print()
#delete mango and price from priceDict
del priceDict['mango']
print("priceDict after deleting mango")
print(priceDict)
Python code in IDLE pasted for better understanding of the indent.
Output Screen