Python 3.4 - Coding Practice
from collections import defaultdict my_dict = {'name': 'Jeff', 'job':'teacher', 'age': 49, 'salary': 50000} print(my_dict.get('job')) print(my_dict['name']) print(my_dict.get('rank', 'not found')) ##this will print the second value if the first is not found print(sorted(my_dict.items())) print(sorted(my_dict.keys())) my_dict['last_name'] = 'Johnson' my_dict['middle name'] = 'Bozo' # notice that the key doesn't have to be connected print(my_dict) del my_dict['age'] print(my_dict) my_dict.clear() print(my_dict) print('------------------------------') my_dict2 = {'name': 'Jeff', 'job':'teacher', 'age': 49, 'salary': 50000} for i in my_dict2.keys(): print(i) print('------------------------------') for i in my_dict2.values(): print(i) print('------------------------------') for k, v in my_dict2.items(): print(k, 'is from', v) print('------------------------------') my_dict3 = {} my_dict3 = defaultdict(str) print(my_dict3[3])
/Users/mattfassnacht/PycharmProjects/untitled10/venv/bin/python /Users/mattfassnacht/PycharmProjects/untitled10/string.py
teacher
Jeff
not found
[('age', 49), ('job', 'teacher'), ('name', 'Jeff'), ('salary', 50000)]
['age', 'job', 'name', 'salary']
{'name': 'Jeff', 'job': 'teacher', 'age': 49, 'salary': 50000, 'last_name': 'Johnson', 'middle name': 'Bozo'}
{'name': 'Jeff', 'job': 'teacher', 'salary': 50000, 'last_name': 'Johnson', 'middle name': 'Bozo'}
{}
------------------------------
name
job
age
salary
------------------------------
Jeff
teacher
49
50000
------------------------------
name is from Jeff
job is from teacher
age is from 49
salary is from 50000
------------------------------
Process finished with exit code 0
Comments