Q. Why is following code not giving correct output even when 25 is a member of the dictionary?
dic1 = {'age' : 25, 'name' : "xyz", 'salary' : 23450.5}
val = dic1['age']
if val in dic1 :
print("This is member of the dictionary")
else :
print("This is not a member of the dictionary")
Answer =
Because ‘in’ condition check only that key is present or not in dictionary. But in given program, value is passed in ‘in’ condition.
can you elaborate?
ReplyDeleteI have written.
DeleteI can
ReplyDeletewhen we use 'in' condition it check the value of 'val' which is 25 and in dictionary you can only search the keys . So 25 is not a key then it will print the 'else' statement
Correct code is
dic1 = {'age' : 25, 'name' : "xyz", 'salary' : 23450.5}
val = dic1['age']
if 'age' in dic1 :
print("This is member of the dictionary")
else :
print("This is not a member of the dictionary")
???
DeleteThe answer is correct duhhh
DeleteThank your so much
ReplyDeleteWelcome : )
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )