Keys
In [1]:
patient_data = {
'patient_id': 12345,
'name': 'John Doe',
'age': 45,
'diagnosis': 'Hypertension',
'medications': ['Lisinopril', 'Amlodipine']
}
keys = patient_data.keys()
print("Keys:", keys)
Keys: dict_keys(['patient_id', 'name', 'age', 'diagnosis', 'medications'])
In [2]:
# Using values() to get all values in the dictionary
values = patient_data.values()
print("Values:", values)
Values: dict_values([12345, 'John Doe', 45, 'Hypertension', ['Lisinopril', 'Amlodipine']])
In [3]:
# Iterate over key-value pairs
for key, value in patient_data.items():
print(f"{key}: {value}")
patient_id: 12345 name: John Doe age: 45 diagnosis: Hypertension medications: ['Lisinopril', 'Amlodipine']
In [4]:
# Function to calculate BMI
def calculate_bmi(weight, height):
return weight / (height ** 2)
def is_senior(age):
return age >= 65
healthcare_data = {
calculate_bmi: (70, 1.75), # weight in kg, height in meters
is_senior: 45 # age
}
for func, args in healthcare_data.items():
if isinstance(args, tuple):
print(f"{func.__name__}:", func(*args))
else:
print(f"{func.__name__}:", func(args))
calculate_bmi: 22.857142857142858 is_senior: False
In [5]:
# Dictionary with multiple patients' data
patients = {
12345: {"name": "John Doe", "age": 30, "diagnosis": "Hypertension"},
67890: {"name": "Jane Smith", "age": 25, "diagnosis": "Asthma"},
11223: {"name": "Emily Davis", "age": 40, "diagnosis": "Diabetes"}
}
def display_patients_with_condition(condition):
for patient_id, details in patients.items():
if details["diagnosis"] == condition:
print(f"Patient ID: {patient_id}")
for key, value in details.items():
print(f" {key}: {value}")
display_patients_with_condition("Hypertension")
Patient ID: 12345 name: John Doe age: 30 diagnosis: Hypertension
