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.

7 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. I can
    when 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")

    ReplyDelete
  2. Thank your so much

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post