《Python编程 从入门到实践》第六章习题选做

6-1 人

message_person = {
    'first_name': 'Hua',
    'last_name': 'Li',
    'age': 20,
    'city': 'Guangzhou'
}
for key, value in message_person.items():
    print(key, " : ", value)

输出:

first_name  :  Hua
last_name  :  Li
age  :  20
city  :  Guangzhou

6-5 河流

rivers = {
    'nile': 'egypt',
    'Yangtze River': 'China',
    'Amazonian River': 'Brazil',
    'Thames': 'England'
}
for river, country in rivers.items():
    print("The " + river.title() + " runs through " + country.title() + ".")
for river in rivers.keys():
    print(river.title())
for country in rivers.values():
    print(country.title())

输出:

The Nile runs through Egypt.
The Yangtze River runs through China.
The Amazonian River runs through Brazil.
The Thames runs through England.
Nile
Yangtze River
Amazonian River
Thames
Egypt
China
Brazil
England

6-7 人

message_person1 = {
    'first_name': 'Hua',
    'last_name': 'Li',
    'age': 20,
    'city': 'Guangzhou'
}
message_person2 = {
    'first_name': 'Lin',
    'last_name': 'Wang',
    'age': 22,
    'city': 'Beijing'
}
message_person3 = {
    'first_name': 'Wen',
    'last_name': 'Cai',
    'age': 30,
    'city': 'Shanghai'
}
people = [message_person1, message_person2, message_person3]
for person in people:
    for key, value in person.items():
        print(key, " : ", value)

输出:

first_name  :  Hua
last_name  :  Li
age  :  20
city  :  Guangzhou
first_name  :  Lin
last_name  :  Wang
age  :  22
city  :  Beijing
first_name  :  Wen
last_name  :  Cai
age  :  30
city  :  Shanghai

6-10 城市

cities = {
    'Guangzhou': {
        'country': 'China',
        'population': '14 million',
        'fact': 'Guangzhou is the capital of Guangdong, also known as Huacheng and Yangcheng'
    },
    'New York': {
        'country': 'U.S.A',
        'population': '7 million',
        'fact': 'New York is a multi ethnically diverse city.'
    },
    'London': {
        'country': 'Britain',
        'population': '60 million',
        'fact': 'London has a large number of scenic spots and museums.'
    }
}
for name, city in cities.items():
    print(name + " belongs to " + city['country'] + ", with a population of " + city['population'] + ".")
    print(city['fact'] + "\n")

输出:

Guangzhou belongs to China, with a population of 14 million.
Guangzhou is the capital of Guangdong, also known as Huacheng and Yangcheng
New York belongs to U.S.A, with a population of 7 million.
New York is a multi ethnically diverse city.
London belongs to Britain, with a population of 60 million.
London has a large number of scenic spots and museums.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值