Python Accessing values
Accessing values
TO access the value associated with an individual key give the name of the dictionary and then place the key in a set of square brackets. If the key you're asking for is not in the dictionary, an error will occur.
You can also use the get() method, which returns None insteadof an error if the key doesn't exist. You can also specify a default value to use if the key is not in the dictionary.
TO access the value associated with an individual key give the name of the dictionary and then place the key in a set of square brackets. If the key you're asking for is not in the dictionary, an error will occur.
You can also use the get() method, which returns None insteadof an error if the key doesn't exist. You can also specify a default value to use if the key is not in the dictionary.
#Getting the value associated with a key alien_0 = {'color': 'green', 'points':5} print(alien_0['color']) print(alien_0['point']) #Getting the value with get() alien_0={'color':'green'} alien_color = alien_0.get('color') alien_points = alien_0.get('points', 0) print(alien_color) print(alien_points)
No comments:
Post a Comment