python基础字典部分+习题

想让一句话和数字一起print,那么程序中变量的结果要用str形式输出。例如:

print("the totle:"+str(new_points));#new_points 是一个数字

获取字典的值:当字典中没有某一个键的时候,使用下列方法获取值是会报错的

可以使用如下方式获取字典中没有指定的键值:这种情况下不会报错


删除键值对使用del,删除为永久删除


同上一题

当字典中的一个键要关联多个值的时候,可以在字典中嵌套一个列表

也可以在字典中嵌套字典:


这个print()可以替换成:

for key,value in x.pets.items():
    print(key+" is "+value);

输出形式为:first_name is tiffany

                    last_name is yang

                    city is beijing这种形式

6-9:可以将地方存在一个列表中

cities = {
	'beijing':{'country':'china','population':100000,'fact':'big'},
	'tokyo':{'country':'japan','population':10000,'fact':'lively'},
	'london':{'country':'england','population':50000,'fact':'clean'}
}
for city,info in cities.items():
	print(city.title());
	for key,value in info.items():
		print(str(key)+" is "+str(value));

输出:



猜你喜欢

转载自blog.csdn.net/wo8vqj68/article/details/80014531