Friend_Information = {'first_name':'zhang','last_name':'san','age':'32','city':'shanghai'}
print(Friend_Information)
print(Friend_Information['first_name'])
print(Friend_Information['last_name'])
print(Friend_Information['age'])
print(Friend_Information['city'])
{'first_name': 'zhang', 'last_name': 'san', 'age': '32', 'city': 'shanghai'}
zhang
san
32
shanghai
>>>
favorite_languages = {
'zhangsan':'python',
'lisi':'c',
'wangwu':'c++',
'liuqiang':'pyhton',
'wangwei':'java',
'xiaoguang':'htnl',
}
for name,language in favorite_languages.items():
print(name.title(),'very like',language.title()+'.')
Zhangsan very like Python.
Lisi very like C.
Wangwu very like C++.
Liuqiang very like Pyhton.
Wangwei very like Java.
Xiaoguang very like Htnl.
>>>
items()
返回由字典项 ((键, 值) 对) 组成的一个新视图。
favorite_languages = {
'zhangsan':'python',
'lisi':'c',
'wangwu':'c++',
'liuqiang':'pyhton',
'wangwei':'java',
'xiaoguang':'html',
}
for name,language in favorite_languages.items():
## print(name.title(),'very like',language.title()+'.')
print('\nname:',name.title())
print('language:',language.title())
print()
name: Zhangsan
language: Python
name: Lisi
language: C
name: Wangwu
language: C++
name: Liuqiang
language: Pyhton
name: Wangwei
language: Java
name: Xiaoguang
language: Html
>>>
Python 字典 keys() 函数以列表返回一个字典所有的键。
good_friends =['zhangsan','xiaoguang']
favorite_languages = {
'zhangsan':'python',
'lisi':'c',
'wangwu':'c++',
'liuqiang':'pyhton',
'wangwei':'java',
'xiaoguang':'html',
}
for name in favorite_languages.keys():
print(name.title())
print()
if name in good_friends:
print('Hi',name.title(),'I see your favorite languages is',favorite_languages[name].title(),'!')
print()
Zhangsan
Hi Zhangsan I see your favorite languages is Python !
Lisi
Wangwu
Liuqiang
Wangwei
Xiaoguang
Hi Xiaoguang I see your favorite languages is Html !
>>>
按顺序遍历字典中所有的键
函数sorted()可用来获得按照特定顺序排列的健列表。
遍历字典中所有的值
方法values(),返回一个值的列表
set() 函数
创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。
可使Python找出独一无二的元素
(1)函数:通过“函数名()”的方式进行调用。
(2)方法:通过“对象.方法名”的方式进行调用。
本人是初学Pyhton,写博客是为了练习和自我复习,
有些知识点可能会有错误,还请大牛们多多指出,
题目出处:Python编程从入门到实践