0
0
Pythonprogramming~10 mins

Accessing values using keys in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Accessing values using keys
Start with dictionary
Provide key to access
Check if key exists
Return value
End
This flow shows how a dictionary value is accessed by a key: check if the key exists, then return the value or handle missing key.
Execution Sample
Python
my_dict = {'apple': 5, 'banana': 3}
value = my_dict['apple']
print(value)
This code gets the value for key 'apple' from the dictionary and prints it.
Execution Table
StepActionKey ProvidedKey Exists?Value RetrievedOutput
1Create dictionary--{'apple': 5, 'banana': 3}-
2Access value'apple'Yes5-
3Print value---5
💡 Finished accessing and printing the value for key 'apple'.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
my_dictundefined{'apple': 5, 'banana': 3}{'apple': 5, 'banana': 3}{'apple': 5, 'banana': 3}
valueundefinedundefined55
Key Moments - 2 Insights
What happens if the key does not exist in the dictionary?
If the key is missing, Python raises a KeyError. This is shown by the 'Key Exists?' check in the flow. To avoid errors, use methods like dict.get() or check key presence before access.
Why do we use square brackets [] to access dictionary values?
Square brackets tell Python to look up the value for the given key in the dictionary. This is shown in Step 2 where 'apple' is inside [].
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'value' after Step 2?
Aundefined
B3
C5
DKeyError
💡 Hint
Check the 'Value Retrieved' column at Step 2 in the execution_table.
At which step is the dictionary created?
AStep 2
BStep 1
CStep 3
DAfter Step 3
💡 Hint
Look at the 'Action' column in the execution_table for when the dictionary is created.
If we try to access my_dict['orange'], what would happen?
ARaise KeyError
BReturn 0
CReturn None
DReturn 'orange'
💡 Hint
Recall the 'Key Exists?' decision in the concept_flow and what happens if key is missing.
Concept Snapshot
Access dictionary values using keys with square brackets: value = dict[key]
If key exists, returns the value; else raises KeyError.
Use dict.get(key) to avoid errors and get None or default.
Keys must be immutable types like strings or numbers.
Accessing values is fast and direct.
Full Transcript
This lesson shows how to get values from a dictionary using keys. We start with a dictionary that stores pairs of keys and values. To get a value, we provide the key inside square brackets. Python checks if the key exists. If yes, it returns the value. If not, it raises an error. The example code creates a dictionary with fruits and counts, then gets the count for 'apple' and prints it. Variables change as the dictionary is created and the value is retrieved. Beginners often wonder what happens if the key is missing or why square brackets are used. The quiz asks about variable values at steps and what happens with missing keys. Remember, use dict.get() to safely access keys that might not exist.