【练习6-1~6-11】Python编程 从入门到实践 第2版

6-1

message={'first_name':"xue",'last_name':'heng','age':'21','city':"Jiangsu"}
for i in message:
    print(message[i])

6-2

numbers = {'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5}
for key, value in numbers.items():
    print(key,':',value)
 

6-5

river={'nile':'egypt','a':'d','b':'c'}
for keys,values in river.items():
    print(f"The {keys.title()} runs through {values.title()}.")
for i in river.keys():
    print(i.title())
for i in river.values():
    print(i.title())

 

6-6

favorite_languages={
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'phil':'python',
}

pollin=['sarah','edward']
for name in favorite_languages.keys():
    if name in pollin:
        print(f"{name.title()}, thank you for taking the poll!")
    else:
        print(f"{name.title()}, please take our poll!")

 

6-7

people={
'person1':{
'first_name':"xue",
'last_name':'heng',
'age':'21',
'city':"Jiangsu",},

'person2':{
'first_name':"h",
'last_name':'l',
'age':'20',
'city':"txl",},

'person3':{
'first_name':"s",
'last_name':'w',
'age':'24',
'city':"ecv",},
}

for info in people.values():
    full_name=f"{info['first_name']} {info['last_name']}"
    age=info['age']
    location=info['city']
    print(full_name+":")
    print(age)
    print(location+"\n")

 

6-8

pet1={'type':'teddy','master':'a'}
pet2={'type':"beagle",'master':'b'}
pet3={'type':'bull dog',"master":"c"}
pets=[pet1,pet2,pet3]
for i in pets:
    print(i)

 

6-9

favorite_places={
    'choi':['thiland','singapore'],
    'kim':['paris','newyork'],
    'lee':['iceland',],
}
for name,places in favorite_places.items():
    if len(places)==1:
        for place in places:
            print(f"{name.title()}'s favorite place is:\n{place.title()}")
        
    else:
        print(f"{name.title()}'s favorite places are:")
        for place in places:
            print(f"{place}")
    print('\n')

 

6-10

numbers = {
'a' : [1,3],
'b' : [3,5,9],
'c' : [2,9],
'd' : [2,6], 
'e' : [4,7,8],
}
for key, value in numbers.items():
    print(f"{key.title()}喜欢的数字有:")
    for evalue in value:
        print(evalue)
    print('\n')

 

 

6-11

cities={
    'a':{
    'country':'China',
    'population':'16',
    'fact':'panda',
    },
    'b':{
    'country':'Korea',
    'population':'2',
    'fact':'kpop star',
    },
    'c':{
    'country':'France',
    'population':'2',
    'fact':'burger',
    }

}
for city,info in cities.items():
    country=info['country']
    population=info['population']
    fact=info['fact']
    print(f"{city.title()}的相关信息:")
    print(f"人口数:{population}\n事实:{fact.title()}\n")
 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值