Program Console Vignaankosh.com
Execution Panel
Console is empty.
Access values using keys 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.
# Access values using keys
person = {"name": "Alice", "age": 25, "city": "Mumbai", "profession": "Engineer"}
# Method 1: Square bracket notation
print("Using []:")
print(f"Name: {person['name']}")
print(f"Age: {person['age']}")
# Method 2: get() method (safe - no KeyError)
print("\nUsing get():")
print(f"City: {person.get('city')}")
print(f"Salary: {person.get('salary', 'Not found')}")
# Method 3: Using variable as key
key = "profession"
print(f"\nUsing variable key '{key}': {person[key]}")
# Handling missing key with try-except
try:
print(person["country"])
except KeyError as e:
print(f"\nKeyError: {e} - key not found")