Program Console Vignaankosh.com
Execution Panel
Console is empty.
Check whether a key exists in dictionary is an interactive PYTHON dry run visualizer from the Dict programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.
This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.
# Check key existence in dictionary
user = {
"course": "Python Basics",
"duration": "4 weeks",
"enrolled": 150,
"rating": 4.8
}
print("Dictionary:", user)
# Method 1: 'in' operator (most common)
keys_to_check = ["course", "instructor", "enrolled", "price"]
print("\nUsing 'in' operator:")
for key in keys_to_check:
if key in user:
print(f" '{key}' exists with value: {user[key]}")
else:
print(f" '{key}' does NOT exist")
# Method 2: get() method
print("\nUsing get() method:")
value = user.get("email", "Not found")
print(f" email: {value}")
value = user.get("phone", "Not found")
print(f" phone: {value}")
# Method 3: try-except
print("\nUsing try-except:")
try:
print(f" age: {user['age']}")
print(f" salary: {user['salary']}")
except KeyError as e:
print(f" KeyError: {e} not found")
# Method 4: Using setdefault (gets value or sets default)
phone = user.setdefault("phone", "N/A")
print(f"\nUsing setdefault(): phone = {phone}")
print(f"Dictionary after setdefault: {user}")