Python编程:从入门到实践第二版答案(第六章)

这段代码示例展示了如何在Python中操作字典,包括访问和打印字典元素,遍历字典的键值对,以及通过键获取值。此外,还演示了列表和字典的组合使用,例如存储个人信息、宠物类型及其主人、喜欢的地方和朋友的电话号码等。整个代码涵盖了基础的数据操作和结构。
摘要由CSDN通过智能技术生成

 6-1

people = {'first_name': 'john', 'last_name': 'james', 'age': '22', 'city': 'boston'}
print(people['first_name'])
print(people['last_name'])
print(people['age'])
print(people['city'])

6-2

friend_number = {
    'day': '1',
    'zyf': '7',
    'wzy': '22',
    'lxd': '0'
    }
print(f"day favorite num is {friend_number['day']}.")
print(f"zyf favorite num is {friend_number['zyf']}.")
print(f"wzy favorite num is {friend_number['wzy']}.")
print(f"lxd favorite num is {friend_number['lxd']}.")

6-3

language = {
    'Keyword': '编程语言中 具有特殊意义的词。',
    'object': 'Python中具有3个属性的数据值——唯一标识,数据类型和值。',
    'Str(string)': '字符串的数据类型。',
    'Int': '整数的数据类型。'
    }
for
print(f"Keyword:{language['Keyword']}.")
print(f"object:{language['object']}.")
print(f"Str(string):{language['Str(string)']}.")
print(f"Int:{language['Int']}.")

 6-4

language = {
    'Keyword': '编程语言中 具有特殊意义的词。',
    'object': 'Python中具有3个属性的数据值——唯一标识,数据类型和值。',
    'Str(string)': '字符串的数据类型。',
    'Int': '整数的数据类型。'
    }
for program, meaning in language.items():
    print(f"{program}:{meaning}")

6-5

river_country = {
    'nile': 'egypt',
    'Yangtze River': 'china',
    'don': 'russia',
    'danube': 'france'
    }
for river, country in river_country.items():
    print(f"The {river} through {country}")
for river in river_country.keys():
    print(river)
for country in river_country.values():
    print(country)

6-6

favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
    }
names = ['jen', 'day', 'wzy', 'edward', 'zyf', 'phil']
for name in names:
    if name in favorite_languages.keys():
        print(f"{name},thank you for talking the poll!")
    else:
        print(f"{name},please talk your poll!")

 

6-7

people1 = {'first_name': 'john',
          'last_name': 'james',
          'age': '22',
          'city': 'boston'
          }
people2 = {'first_name': 'harden',
          'last_name': 'kevin',
          'age': '26',
          'city': 'new york'
          }
people3 = {'first_name': 'curry',
          'last_name': 'payton',
          'age': '32',
          'city': 'xian'
          }
people = [people1, people2, people3]
for person in people:
    name = person['first_name']+ " "+ person['last_name']
    print(f"{name},living in {person['city']},is {person['age']} years old.")

6-8

pet_1 = {'type': 'cat',
         'owner': "john",
          }
pet_2 = {'type': 'dog',
         'owner': "james",
          }
pet_3 = {'type': 'bird',
         'owner': "harden",
          }
pets = [pet_1, pet_2, pet_3]
for this in pets:
    print(f"{this['type']} belong {this['owner']}.")

6-9

favorite_places = {
    'day': ['xian', 'beijing', 'shanghai'],
    'lxd': ['tianjin', 'tokyo'],
    'zcy': ['new york', 'boston'],
}
for name,places in favorite_places.items():
    print(f"{name} love:")
    for place in places:
        print(place)

6-10

friend_number = {
    'day': ['1', '2', '5'],
    'zyf': ['7'],
    'wzy': ['22', '10', '15'],
    'lxd': ['0', '11']
    }
for name,nums in friend_number.items():
    print(f"\n{name}:")
    for num in nums:
        print(f"\t{num}")

6-11

cities = {
    'beijing': {'country': 'china', 'people': '21_000_000', 'about': 'imperial palace'},
    'new york': {'country': 'america', 'people':'16_000_000', 'about': 'central park'},
    'paris': {'country':'france', 'people': '15_000_000', 'about': 'Eiffel Tower'}
}
for city_name, city_about in cities.items():
    print(f"\n{city_name}:")
    print(f"\tcountry : {city_about['country']}")
    print(f"\tpeople :{city_about['people']}")
    print(f"\thave {city_about['about']}")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机务猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值