Access values using keys Dry Run in PYTHON

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 Program Code

# 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")

View the complete Dict programs page.

Program Console Access values using keys Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.