In: Computer Science
#Python
Given a dictionary
gradeCounts = { "A": 8, "D": 3, "B": 15, "F": 2, "C": 6 }
write the Python statement(s) to print:
a) all the keys.
b) all the values.
c) all the key and value pairs.
d) all of the key and value pairs in key order.
e) the average value.
f) a chart similar to the following in which each row contains a key followed by a number of asterisks equal to the key’s data value. The rows should be printed in key order, as shown below.
A: ********
B: ***************
C: ******
D: ***
F: **
Python code:
#initializing gradeCounts dictionary
gradeCounts={"A":8,"D":3,"B":15,"F":2,"C":6}
#a)printing all the keys
print(*gradeCounts.keys())
#b)all the values
print(*gradeCounts.values())
#c)all the key and value pairs.
print(*gradeCounts.items())
#d)looping all elements in key order
for i in sorted(gradeCounts):
#printing the pair
print((i,gradeCounts[i]),end=" ")
#printing a newline
print()
#e)printing average
print(sum(gradeCounts.values())/len(gradeCounts))
#f)looping all elements in key order
for i in sorted(gradeCounts):
#printing astricks for each key
print(i,": ","*"*gradeCounts[i])
Screenshot:
Input and Output: