5.1.2 Accessing Values by Key & KeyError

Unit: Unit 5: Dictionaries and Sets • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Access values using square bracket notation d[key]
server_node = {
    "hostname": "prod-api-01",
    "ip_address": "10.0.4.15",
    "region": "us-east-1",
    "cpu_cores": 16
}

# 1. Access existing keys
host = server_node["hostname"]
cores = server_node["cpu_cores"]

# 2. Check key existence before accessing using 'in'
has_gpu = "gpu_count" in server_node

print("Host:", host)
print("Cores:", cores)
print("Has GPU configured:", has_gpu)
Open in Live Interactive Python Sandbox →