dict={"name":"", "age": 24, "hobby":"sing a song"}
a=dict.items()
print("a", a, type(a))
b=dict.has_key("name")
print("b==name", b, type(b))
c=dict.keys()
print("c==keys", c, type(c))
d=dict.values()
print("d==keys", d, type(d))
# setdefault和get很相似,但是get设置的默认值并不会成为dict的key:value对
e = dict.setdefault("language", "python")
print("e==keys", e, type(e))
print('dict',dict)
f = dict.get("food", "steak")
print("f==keys", f, type(f))
print('dict',dict)
# del dict["name"]
# print('dicttttttttt',dict)
# del dict
# print('dictllllllll',dict)
# for key, value in dict.items():
# print(key, value)
for key in dict:
print(key, dict[key])
输出结果:
('a', [('hobby', 'sing a song'), ('age', 24), ('name', '')], <type 'list'>)
('b==name', True, <type 'bool'>)
('c==keys', ['hobby', 'age', 'name'], <type 'list'>)
('d==keys', ['sing a song', 24, ''], <type 'list'>)
('e==keys', 'python', <type 'str'>)
('dict', {'hobby': 'sing a song', 'age': 24, 'name': '', 'language': 'python'})
('f==keys', 'steak', <type 'str'>)
('dict', {'hobby': 'sing a song', 'age': 24, 'name': '', 'language': 'python'})
('hobby', 'sing a song')
('age', 24)
('name', '')
('language', 'python')