Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.
exam questions

Exam PCAP All Questions

View all questions & answers for the PCAP exam

Exam PCAP topic 1 question 19 discussion

Actual exam question from Python Institute's PCAP
Question #: 19
Topic #: 1
[All PCAP Questions]

What would you used instead of XXX if you want to check weather a certain 'key' exists in a dictionary called dict? (Choose two.)

  • A. 'key' in dict
  • B. dict ['key'] != None
  • C. dict.exists ('key')
  • D. 'key' in dict.keys ( )
Show Suggested Answer Hide Answer
Suggested Answer: AD 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
JeyTlenJey
3 months, 4 weeks ago
Selected Answer: AD
Correct: A,D dict={'Mom': 5551234567, 'Dad': 5557654321, "Son":None} print( dict.keys()) #dict_keys(['Mom', 'Dad', 'Son]) if 'Mom' in dict: print("Key exists") #ok if dict['Son'] != None : print("Key exists") #empty if dict.exists('Mom'): print("Key exists") #AttributeError: 'dict' object has no attribute 'exists' if 'Mom' in dict.keys(): print("Key exists") #ok
upvoted 1 times
...
Oracleist
9 months, 3 weeks ago
A,D C is not working at all B is checking if the value of key is different than None
upvoted 2 times
...
Acid_Scorpion
1 year, 2 months ago
Selected Answer: AD
A - is correct D - is correct B is can't be correct, as it checks value, not key
upvoted 2 times
...
kosa997
1 year, 4 months ago
A is wrong - this takes the value, doesn't check if key exists
upvoted 1 times
...
Bubu3k
1 year, 5 months ago
B fails in this particular case: dict = {"A1": 1, "A2": 2, "A3": 3, "A4": None} key = "A4" print(dict[key]!=None) So it's AD
upvoted 3 times
...
mlsc01
1 year, 9 months ago
Selected Answer: AD
Only A and D are correct. Technically there can be a key which has None as its value, then option B will fail, because it checks for the presence of a value, not the key itself. ######## sample code ######## d = {'key1': 1, 'key2': None} k = 'key2' print(d) if k in d: print(f'\'{k}\' exists in dict') else: print(f'\'{k}\' does not exist in dict') if k in d.keys(): print(f'\'{k}\' exists in dict') else: print(f'\'{k}\' does not exist in dict') # wrong way because it check for the presence of value, not the key itself if d.get(k) is not None: print(f'\'{k}\' exists in dict <-- using wrong way') else: print(f'\'{k}\' does not exist in dict <-- using wrong way') # wrong way because it check for the presence of value, not the key itself if d[k] != None: print(f'\'{k}\' exists in dict <-- using wrong way') else: print(f'\'{k}\' does not exist in dict <-- using wrong way')
upvoted 2 times
...
Netspud
1 year, 9 months ago
Selected Answer: AD
3 of them work, I vote AD (B is a bit ugly!) dict = {'key': 'Farts'} try: if 'key' in dict: print("Key exists (A)") if dict['key']!= None: print("Key exists (B)") if 'key' in dict.keys(): print("Key exists (D)") if dict.exists ('key'): print("Key exists (C)") except: pass Key exists (A) Key exists (B) Key exists (D)
upvoted 1 times
...
rotimislaw
2 years ago
Selected Answer: AD
A&D are most Python. B also returns True but it's a check if a key isn't None and no if a key exists so I'd cut that answer first. > dict = {'key' : 'value'} > print('key' in dict) True > print(dict['key'] != None) True > print(dict.exists('key')) Traceback (most recent call last): File "./prog.py", line 4, in <module> AttributeError: 'dict' object has no attribute 'exists' > print('key' in dict.keys()) True
upvoted 3 times
...
jaimebb
2 years ago
Selected Answer: BD
The only one that not works it is C, all the others works correctly.
upvoted 2 times
...
Dav023
2 years, 1 month ago
Selected Answer: AB
A, B and D are rights!
upvoted 2 times
...
ciccio_benzina
2 years, 2 months ago
'a' works also (there are a lot of mistakes in this website)
upvoted 3 times
...
Jnanada
2 years, 3 months ago
A. 'key' in dict D. 'key' in dict.keys ( )
upvoted 2 times
...
PremJaguar
2 years, 4 months ago
the print statement looks like PYTHON 2!!!
upvoted 1 times
...
bebi
2 years, 4 months ago
A and D are the correct answers. B needs try/except.
upvoted 1 times
...
macxsz
2 years, 6 months ago
Selected Answer: AD
A. 'key' in dict D. 'key' in dict.keys ( )
upvoted 2 times
...
efemona
2 years, 6 months ago
A, B & D are correct, but B checks if a key value is Not None. The most pythonic answer is A and D which checks the dictionary keys
upvoted 4 times
...
Mokel
2 years, 6 months ago
AD is the correct answer
upvoted 1 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...