Chapter 6: Dictionaries Flashcards
create a simple dictionary and call a value
alien_0 = {‘color’: ‘green’, ‘points’: 5}
print(alien_0[‘color’])
print(alien_0[‘points’])
what are key-value pairs
a set of values associated with each other
each key is connected to a value, and you can use a key to access the value associated with that key
access a value from a dictionary by putting it into a variable and then printing an f-string
new_points = alien_0[‘points’]
print(f”You just earned {new_points} points”)
add new key-value pairs
alien_0['x_position'] = 0 alien_0['y_position'] = 25
start with an empty dictionary and fill it
alien_0 = {}
alien_0['color'] = 'green' alien_0['points'] = 5
modify the values in the dictionary
alien_0[‘color’] = ‘yellow’
print(f” The alien is now {alien_0[‘color’]}”)
Track the position of an alien that moves at different speeds. Store a value representing the alien’s current speed and then use it to determine how far to the right the aliens should move.
#move the alien to the right #determine how far to move the alien based on its current speed if alien_0['speed'] =='slow': x_increment = 1 elif alien_0['speed'] == 'medium': x_increment = 2 else: #this must be a fast alien x_increment = 3
#the new position is the old position plus the increment. alien_0['x_position'] = alien_0['x_position'] + x_increment
print(f”New position: {alien_0[‘x_position’]}”)
use the del statement to completely remove a key-value pair
alien_0 = {‘color’: ‘green’, ‘points’: 5}
del alien_0[‘points’]
make a dictionary of people with similar values, and then writhe a variable used in an f-string that states something about one of the people’s values from the dictionary
favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', }
language = favorite_languages[‘sarah’].title()
print(f”Sarah’s favorite language is {language}”)
what does .get()
sets a default value that will be returned if a requested key doesn’t exist
create a dictionary and use .get() to set a default value if a key doesn’t exist
alien_0 = {‘color’: ‘green’, ‘speed’: ‘slow’}
point_value = alien_0.get(‘points’, ‘No point value assigned.’)
print(point_value)
store a person’s username, first name, and last name in a dictionary
then write a for loop to access it with the use of two f-strings separating what the key and value are
user_0 = { 'username': 'efermi', 'first': 'enrico', 'last': 'fermi', }
for key, value in user_0.items():
print(f”\nKey: {key}”)
print(f”Value: {value}”)
what does .item() do
it returns a list of key-value pairs
use the dictionary that compares similar values to write a for loop with f-strings
favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', }
for name, language in favorite_languages.items():
print(f”\t{name.title()}, I see you love {language}!”)
loop through just the keys in a dictionary
favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', }
for name in favorite_languages.keys():
print(name)