Python 3.4 - Dictionaries

my_dict = {'employee_name': 'Jeff', 'job': 'Teacher', 'rank': 10, 'salary': 6000,           'favorite things': ['polo', 'horses', 'movies about horses']}

# dictionaries are made of keys with values
print(my_dict)

print(my_dict['job'])  # this prints out the item in the dict
print(my_dict['favorite things'])  # this prints out the key which is a list within the dict

my_dict2 = {1: 'Jeff', 'job': 'Teacher', 'rank': 10, 'salary': 6000,           'favorite things': ['polo', 'horses', 'movies about horses']}


print(my_dict2[1])  # an integer can be a key
print(my_dict2.get('job'))  # this will get the value of the key specified.

my_dict2['phone'] = '555-5555'
print(my_dict2)   # notice that the phone value has been added to the dictionary

my_dict.update({'employee_name': 'Paul'})

print(my_dict)  # you can see that this updated the dict with the new name

del my_dict2['rank']

print(my_dict2) # you can see the key rank has been removed.
job = my_dict.pop('job')

print(my_dict)
print(job) # notice that the job has been removed, but also alse saved as a new variable.
print(len(my_dict))

print(my_dict.values())  # this just prints out the values of the keys
print(my_dict.keys())

/Users/mattfassnacht/PycharmProjects/untitled10/venv/bin/python /Users/mattfassnacht/PycharmProjects/untitled10/math.py
{'employee_name': 'Jeff', 'job': 'Teacher', 'rank': 10, 'salary': 6000, 'favorite things': ['polo', 'horses', 'movies about horses']}
Teacher
['polo', 'horses', 'movies about horses']
Jeff
Teacher
{1: 'Jeff', 'job': 'Teacher', 'rank': 10, 'salary': 6000, 'favorite things': ['polo', 'horses', 'movies about horses'], 'phone': '555-5555'}
{'employee_name': 'Paul', 'job': 'Teacher', 'rank': 10, 'salary': 6000, 'favorite things': ['polo', 'horses', 'movies about horses']}
{1: 'Jeff', 'job': 'Teacher', 'salary': 6000, 'favorite things': ['polo', 'horses', 'movies about horses'], 'phone': '555-5555'}
{'employee_name': 'Paul', 'rank': 10, 'salary': 6000, 'favorite things': ['polo', 'horses', 'movies about horses']}
Teacher
4
dict_values(['Paul', 10, 6000, ['polo', 'horses', 'movies about horses']])
dict_keys(['employee_name', 'rank', 'salary', 'favorite things'])

Process finished with exit code 0

https://www.youtube.com/watch?v=daefaLgNkw0



Comments

Popular posts from this blog

Pyhton 3.4 - Continue

Conversations Ongoing: Valuing Modernization in Early European History

Conversations Ongoing: A Bunch of Amateurs