python编程 从入门到实践 第六章 动手试一试参考答案 含运行结果-字典

坚持,共勉!

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
#6-1
print("6-1")
persons = {'first_name':'congcong',
          'last_name':'yang',
          'age':18,
          'city':'guangzhou'
          }
print(persons)
print(persons['first_name'])
print(persons['last_name'])
print(persons['age'])
print(persons['city'])

#6-2
print('\n6-2')
love_number = {'bibi':1,
               'congcong':9,
               'wendy':8,
               'xiaobobo':2,
               'zhen':7}
print(love_number)

#6-3
print('\n6-3')
glossarys = {'class':'类',
            'object':'对象',
            'package':'包',
            'value':'值',
            'define':'定义'
            }

print('class:'+ glossarys['class'])
print('object:'+glossarys['object'])
print('package:' + glossarys['package'])
print('value' + glossarys['value'])
print('define' + glossarys['define'])
print('class:' + "\n" + glossarys['class'])
print('object:' + '\n' + glossarys['object'])
print('package:' + '\n' + glossarys['package'])
print('value:' + '\n' + glossarys['value'])
print('define:' + '\n' + glossarys['define'])

#6-4
print("6-4")
glossarys['abstract base class'] = '抽象基类'
glossarys['annotation'] = '注解'
glossarys['argument'] = '参数'
glossarys['asynchronous generator'] = '异步生成器'
glossarys['asynchronous generator iterator'] = '异步生成器迭代器'
for key,value in glossarys.items():
    print("\nkey: " + key)
    print("\nvalue: " + value)

#6-5
#打印key和value值
print('\n6-5')
three_big_river = {'nile':'egypt','changjiang':'china','amazon':'south america'}
for river,country in  three_big_river.items():
    print('\nThe ' + river + ' runs through' + country)

#打印key值
for river in three_big_river.keys():
    print(river)

#打印value值
for country in three_big_river.values():
    print(country)

 

#6-6
print('6-6')
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() + ".")
persons = ['bobo','congcong','jen','sarah']
for name in persons:
    #print(name.title())
    if name not in favorite_languages:
        print (name + ' please take our poll!')
    else:
        print(name + ' thany you for taking the poll!')

 

#6-7
print("6-7")
persons = {'first_name':'congcong',
          'last_name':'yang',
          'age':18,
          'city':'guangzhou'
          }
persons_1 = {'first_name':'bobo',
            'last_name':'yang',
            'age':19,
            'city':'guangzhou'
            }
persons_2 = {'first':'xiaobobo',
            'last_name':'yang',
            'age':20,
            'city':'guangzhou'
            }
people = [persons,persons_1,persons_2]
for person in people:
    print(person)

 

#6-8
print('6-8')
wangcai = {'dog':'kind',
          'clever':'owner'
         }
xiaoyu ={'fish': 'kind',
         'bobo':'owner'
        }
xiaohong = {'cat':'kind',
          'xiaobobo':'owner'
          }
pets = [wangcai,xiaoyu,xiaohong]
for pet in pets:
    print(pet)

#6-9 喜欢的地方,字典中包含列表
print('\n6-9')
favorite_places = {'bobos':['guangzhou','hangzhou'],
                   'yang':['sh'],#记得加[],啊,我一开始就是忘记了加[],导致if语句不成立
                   'cong':['beijing','zhuhai'],
                   }
for name,places in favorite_places.items():
    #print(len(places))
    if len(places) >1 :
        print('\n' + name.title() + "'s favoriate places are:")
        for place in places:
            print ('\t' + place.title())
    else:
        print('\n' + name.title() +"'s favoriate place is:")
        print('\t' + place.title())

#6-10
#6-2
#print('\n6-2')
love_number = {'bibi':1,
               'congcong':9,
               'wendy':8,
               'xiaobobo':2,
               'zhen':7}
print(love_number)
print('\n6-10')
love_numbers = {'bibi':[1,2,3,4],
               'congcong':[9,1,2],
               'wendy':[8,3],
               'xiaobobo':[2,3],
               'zhen':[7,1]
               }
for name,numbers in love_numbers.items():
    #print(numbers)
    print('\n' +name.title() + ":")
    for number in numbers:
        print(number)


#6-11城市
print("\n6-11")
cities = {
    'guangzhou': {
    'country':'china',
    'population':'13billion',
    'fact': 'beatiful city',
     },

    'shenzhen' : {
    'country' : 'china',
    'population' : '13billion',
    'fact' : 'near sea',
    },

    "shanghai": {
    'country' : 'china',
    'population': '13billion',
    'fact' : 'big',
    }
}
for city,info in cities.items():
    print("\ncity:" + city)
    country = info['country']
    population = info['population']
    fact = info['fact']

    print('\tpopulation:' + population.title())
    print('\tfact:' + fact.title())

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值