Python编程 从入门到实践 练习6-7~练习6-12

6-7 人

# 使用字典存储熟人的信息
person1 = {
	'first_name': 'william',
	'last_name': 'cliton',
	'age': 20,
	'city': 'new york',
	}
# 再创建两个表示人的字典
person2 = {
	'first_name': 'leonhard',
	'last_name': 'euler',
	'age': 16,
	'city': 'basel',
	}
person3 = {
	'first_name': 'claude',
	'last_name': 'shannon',
	'age': 24,
	'city': 'petoskey',
	}
people = [ person1, person2, person3]
# 遍历列表将信息打印出来
for person in people:
	print(person)

6-7输出

6-8 宠物

# 创建多个宠物字典
cloris = {
	'type': 'cat',
	'host name': 'carmen',
	}
brinkley = {
	'type': 'dog',
	'host name': 'bob',
	}
laura = {
	'type': 'turtle',
	'host name': 'alice',
	}
pets = [cloris, brinkley, laura]
for pet in pets:
	print(pet)

6-8输出

6-9 喜欢的地方

# 创建一个存储三个人喜欢的地方的字典

favorite_places = {
	'john': {
		'yellow river',
		'mount tai',
		'daming lake',
		},
# 注意此处需要有,分隔字典中的键-值对
	'david': {
		'shimen senic area',
		'mount wutai',
		},
		
	'jack': {
		'the oriental peral tower',
		},
	}
for name, places in favorite_places.items():
	print(name.title() + ':')
	for place in places:
		print('\t' + place.title())

6-9输出

6-10 喜欢的数字

# 使用一个字典来存储一些人喜欢的数字
love_numbers = {
	'ford': [
		4,
		],
	'david': [
		6,
		],
	'louis': [
		7,
		],
	'sam':[
		1,
		0,
		9,
		],
	'bob':[
		7,
		],
	}
for person, numbers in love_numbers.items():
	if len(numbers) == 1:
		print(person.title() + "'s favorite number is " + str(numbers[0]))
	elif len(numbers) > 1:
		print(person.title() + "'s favorite numbers are: ")
		for number in numbers:
			print('\t' + str(number))

6-10输出

6-11 城市

# 存储城市的国家,人口数,一个事实
cities = {
	'beijing': {
		'country': 'china',
		'population': 21536000,
		'fact': 'capital of china',
		},
	'paris': {
		'country': 'france',
		'population': 2870000,
		'fact': 'capital of france',
		},
	'berlin':{
		'country': 'germany',
		'population': 3500000,
		'fact': 'capital of germany',
		},
	}
# 打印城市名字以及有关信息
for city, info in cities.items():
	print(city.title() + ':')
	for message, data in info.items():
		print('\t' + message + ': ' + str(data).title())

6-11输出

6-12

6-10满足6-12所提的要求,是对6-2进行的扩展。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值