python从入门到实践第六章的练习题作业

'''
6-1 人:使用一个字典来存储一个熟人的信息,包括名、姓、年龄和居住的城市。
该字典应包含键 first_name、 last_name、 age 和 city。将存储在该字典中的每项信息都
打印出来。
'''
acquaintance = {'first_name':'pan','last_name':'tuhong','age':20,'city':'zhanjiang'}
print(acquaintance)

'''
6-2 喜欢的数字:使用一个字典来存储一些人喜欢的数字。请想出 5 个人的名字,
并将这些名字用作字典中的键;想出每个人喜欢的一个数字,并将这些数字作为值存储
在字典中。打印每个人的名字和喜欢的数字。为让这个程序更有趣,通过询问朋友确保
数据是真实的。
'''
favourate = {'pantuhong':13,'cc':6,'mm':68,'fafa':168,'ly':33}
print('pantuhong'+'\'s favourate number is '+ str(favourate['pantuhong']))
print('cc'+'\'s favourate number is '+ str(favourate['cc']))
print('mm'+'\'s favourate number is '+ str(favourate['mm']))
print('fafa'+'\'s favourate number is '+ str(favourate['fafa']))
print('ly'+'\'s favourate number is '+ str(favourate['ly']))

'''
6-3 词汇表: Python 字典可用于模拟现实生活中的字典,但为避免混淆,我们将后
者称为词汇表。
 想出你在前面学过的 5 个编程词汇,将它们用作词汇表中的键,并将它们的含
义作为值存储在词汇表中。
 以整洁的方式打印每个词汇及其含义。为此,你可以先打印词汇,在它后面加
上一个冒号,再打印词汇的含义;也可在一行打印词汇,再使用换行符(\n)
插入一个空行,然后在下一行以缩进的方式打印词汇的含义
'''
vocabulary = {'int':'integer number','char':'the base element of string','class':'a base form of OOP'
				,'struct':'you can use this to develop your own data struct','printf':'a function to print something on the screen'}

print('int'+' : '+vocabulary['int'])
print('char'+' : '+vocabulary['char'])
print('class'+' : '+vocabulary['class'])
print('struct'+' : '+vocabulary['struct'])
print('printf'+' : '+vocabulary['printf'])

'''
6-4 词汇表 2:既然你知道了如何遍历字典,现在请整理你为完成练习 6-3 而编写
的代码,将其中的一系列 print 语句替换为一个遍历字典中的键和值的循环。确定该循
环正确无误后,再在词汇表中添加 5 个 Python 术语。当你再次运行这个程序时,这些
新术语及其含义将自动包含在输出中。
'''
vocabulary = {'int':'integer number','char':'the base element of string','class':'a base form of OOP'
				,'struct':'you can use this to develop your own data struct','printf':'a function to print something on the screen'}
for key,value in vocabulary.items():
	print(key+ ' : ' + value)

'''
6-5 河流:创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键—
值对可能是'nile': 'egypt'。
 使用循环为每条河流打印一条消息,如“The Nile runs through Egypt.”。
 使用循环将该字典中每条河流的名字都打印出来。
 使用循环将该字典包含的每个国家的名字都打印出来
'''
rivers = {'nile':'Egypt','volga':'Russia','Changjiang':'China'}
for river,country in rivers.items():
	print('The '+river.title()+' runs through '+country+'.')

print('\nThe Rivers:')
for river in rivers.keys():
	print(river)

print('\nThe countries:')
for country in rivers.values():
	print(country)

'''
6-6 调查:在 6.3.1 节编写的程序 favorite_languages.py 中执行以下操作。
 创建一个应该会接受调查的人员名单,其中有些人已包含在字典中,而其他人
未包含在字典中。
 遍历这个人员名单,对于已参与调查的人,打印一条消息表示感谢。对于还未
参与调查的人,打印一条消息邀请他参与调查。
'''
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}

for name, language in favorite_languages.items():
	print(name.title() + "'s favorite language is " +language.title() + ".")


people = ['jen','siry','lily','phil']
for person in favorite_languages:
	if person in people:
		print('Thanks for accept my survey, '+person.title()+'!')
	else:
		print('Would you like to take my survey, '+ person.title()+'?')

'''
6-7 人:在为完成练习 6-1 而编写的程序中,再创建两个表示人的字典,然后将这
三个字典都存储在一个名为 people 的列表中。遍历这个列表,将其中每个人的所有信
息都打印出来。
'''

acquaintance = {'first_name':'pan','last_name':'tuhong','age':20,'city':'zhanjiang'}
#print(acquaintance)

people2 = {'first_name':'may','last_name':'lingling','age':'24','city':'kunming'}
people3 = {'first_name':'maro','last_name':'siry','age':'14','city':'tokyo'}
people = [acquaintance,people2,people3]

for person in people[:]:
	print(person)

'''
6-8 宠物:创建多个字典,对于每个字典,都使用一个宠物的名称来给它命名;在
每个字典中,包含宠物的类型及其主人的名字。将这些字典存储在一个名为 pets 的列
表中,再遍历该列表,并将宠物的所有信息都打印出来。
'''
maoni = {'type':'cat','owner':'mm'}
xiaogou = {'type':'dog','owner':'gg'}
xiaoji = {'type':'hen','owner':'siry'}
pets= []
pets.append(maoni)
pets.append(xiaogou)
pets.append(xiaoji)

for pet in pets:
	print(pet)

'''
6-9 喜欢的地方:创建一个名为 favorite_places 的字典。在这个字典中,将三个
人的名字用作键;对于其中的每个人,都存储他喜欢的 1~3 个地方。为让这个练习更有
趣些,可让一些朋友指出他们喜欢的几个地方。遍历这个字典,并将其中每个人的名字
及其喜欢的地方打印出来。
'''
favorite_places = {'ly' : {'zhanjiang','guilin','beijing'},'siry' : {'kunming','tibet','lijiang'},'mory' : {'xiameng','hangzhou','suzhou'}}
print('ly \'s favorite_places is '+str(favorite_places['ly']))
print('siry \'s favorite_places is '+str(favorite_places['siry']))
print('mory \'s favorite_places is '+str(favorite_places['mory']))

'''
6-11 城市:创建一个名为 cities 的字典,其中将三个城市名用作键;对于每座城
市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该城市
的事实。在表示每座城市的字典中,应包含 country、 population 和 fact 等键。将每座
城市的名字以及有关它们的信息都打印出来。
'''

cities = {'zhanjiang':{'country':'China','population':'more than 900 million','fact':'hygiene city'}
			,'paris':{'country':'French','population':'about 1100 million','fact':'international city'}
			,'New York':{'country':'U.S.A','population':'more than 2000 million','fact':'international city'}}

for key,value in cities.items():
	print(key+' : '+str(value))

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值