Bird
Raised Fist0

Which of the following is the correct syntax to check if a key 'slot1' exists in a Python dictionary named availability?

easy🧠 Conceptual Q3 of Q15
LLD - Design — Hotel Booking System
Which of the following is the correct syntax to check if a key 'slot1' exists in a Python dictionary named availability?
Aif availability.contains('slot1'):
Bif availability.has_key('slot1'):
Cif availability.exists('slot1'):
Dif 'slot1' in availability:
Step-by-Step Solution
Solution:
  1. Step 1: Recall Python dictionary key existence syntax

    In Python 3.x, the correct way to check if a key exists is using the 'in' keyword.
  2. Step 2: Analyze options

    if 'slot1' in availability: uses 'in' correctly; 'has_key' was removed in Python 3; 'contains' and 'exists' are invalid methods.
  3. Final Answer:

    if 'slot1' in availability: -> Option D
  4. Quick Check:

    Python dict key check uses 'in' keyword [OK]
Quick Trick: Use 'in' keyword to check dict keys in Python 3+ [OK]
Common Mistakes:
MISTAKES
  • Using has_key() in Python 3
  • Using non-existent methods like contains()
  • Confusing syntax with other languages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes