How to access value for key “Price” in the following Python Dictionary: [1]
mystock = {"Product": "Earphone","Price": 800,"Quantity": 50,"InStock" :"Yes"}
A. mystock[“Product”]
B. mystock(“Product”)
C. mystock[Product]
D. mystock(Product)
Answers
Answered by
3
Answer:
mystock["Product"]
Explanation:
You need to tell the program you're talking about a string here which is "Product" and not an identifier called "Product".
You can check this for yourself,
#assigning an identifier with a value
a=5
#define a dictionary and set one of the keys equal to the identifier's value,
d={1:"nopsies", 2:"grow up", 5:"apples", "5":"ohno"}
#Use the key to get the value from dictionary and print it
print(d[a])
print(d[5])
print(d["5"])
You will notice that print(d[a]) and print(d[5]) will return the same values i,e "apples" but "5" will return "ohno" since a is an identifier with value 5
BrainlyProgrammer:
perfect answer!!!
Similar questions