字典中的key,只能用不可变的数据类型,数字,字符串,元组皆可以。在存储时将key值进行哈希,所以只能用不可变的数据类型。
1. len( )返回字典的键值对
>>> b={"name":"wang da lu","age":23,"gender":"boy"}
>>> len(b)
3
2. values( )
>>> b={"name":"wang da lu","age":23,"gender":"boy"}
>>> for each in b.values():
print(each)
wang da lu
23
boy
3. keys( )
>>> b={"name":"wang da lu","age":23,"gender":"boy"}
>>> for each in b.keys():
print(each)
name
age
gender
>>> b={"name":"wang da lu","age":23,"gender":"boy"}
>>> for each in b: #默认为key
print(each)
name
age
gender
4. items( )
>>> b={"name":"wang da lu","age":23,"gender":"boy"}
>>> for each in b.items():
print(each)
('name', 'wang da lu')
('age', 23)
('gender', 'boy')
5.
>>> b={"name":"wang da lu","age":23,"gender":"boy"}
>>> for A,B in b.items():
print("Hello:%s,Hello:%s" %(A,B))
Hello:name,Hello:wang da lu
Hello:age,Hello:23
Hello:gender,Hello:boy