Python编程:从入门到实践 练习答案 chapter06

Python编程:从入门到实践 练习答案 chapter06,第1张

Python编程:从入门实践 练习答案 chapter06
20211030

6-1
person = {
    'first_name': 'S',
    'last_name': 'G',
    'age': 21,
    'city': 'SY', 
    }
print(person['first_name'])
print(person['last_name'])
print(person['age'])
print(person['city'])

6-2
nums = {
        'CJR': 1,
        'SZX': 2,
        'JY': 3,
        'LM': 4,
        'YR':5,
        'SXX': 6,
        }
print(nums['CJR'])
print(nums['SZX'])
print(nums['JY'])
print(nums['LM'])
print(nums['YR'])
print(nums['SXX'])

6-3
glossary = {
       'list': '1',
       'tuple': '2',
       'dict': '3',
       'string': '4',
       'set': '5',
       }
print('list:' + glossary['list'])
print('tuple:' + glossary['tuple'])
print('dict:' + glossary['dict'])
print('string:' + glossary['string'])
print('set:' + glossary['set'])

6-4
glossary = {
       'list': '1',
       'tuple': '2',
       'dict': '3',
       'string': '4',
       'set': '5',
       }
glossary['access'] = '6'
glossary['add'] = '7'
glossary['create'] = '8'
glossary['modify'] = '9'
glossary['delete'] = '10'

for term in glossary:
    print(term + ': ' + glossary[term])

6-6
rivers = {
    'mississippi': 'us',
    'yangtze': 'cn',
    'nile': 'egypt',
    }

for river in rivers:
    print('The ' + river.title() + 'runs through ' + rivers[river].title())

for r in rivers:
    print(r.title())

for n in rivers.values():
    print(n.title())

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() + ".")

print("n")

men = ['jen', 'phil', 's', 'z', 'x']

for man in men:
    if man in favorite_languages:
        print(man + ', thanks for enjoying poll!')
    else:
        print(man + ', we would like invite you take this poll')

6-7
person = {
    'first_name': 'G',
    'last_name': 'S',
    'age': 21,
    'city': 'SY', 
    }

woman = {
       'first_name': 'X',
       'last_name': 'W',
       'age': 20,
       'city': 'DL',
       }

man ={
      'first_name': 'Z',
      'last_name': 'S',
      'age': 21,
      'city': 'SD',
      }

perple = [person, woman, man]

for m in perple:
    print(m['first_name'] + ' ' + m['last_name'] + ', of ' + m['city'] 
          + ', is ' + str(m['age']) + ' years old')

6-8
pet_1 = {
    'name': 'kitty',
    'kind': 'cat',
    'host': 's',
       }

pet_2 = {
    'name': 'doggy',
    'kind': 'dog',
    'host': 'g'
    }

pets = [pet_1, pet_2]

for pet in pets:
    print(pet['name'].title() + ' is a ' + pet['kind'] 
          + ", and its owner is " + pet['host'].title())


6-9
favorite_places = {
    's': ['sd', 'sz', 'la'],
    'z': ['cs', 'sz'],
    'x': ['gz', 'bj'],
    }

for man, place  in favorite_places.items():
    print(man.title() + "'s favorite places are :" )
    if len(place) > 1:
        for p in place:
            print(p)

6-10
nums = {
        'CJR': [1, 3],
        'SZX': [2, 4],
        'JY': [3,],
        'LM': [4,],
        'YR':[5,7],
        'SXX': [6, 8],
        }
for man, num in nums.items():
    print("n" + man + "'s favorite nums are: ")
    for n in num:
        print(n)
    
6-10
cities = {
    'sd': {
        'nation': 'cn',
        'population': '1,335,900',
        'fact': 'hometown',
        },
    'cs': {
        'nation': 'cn',
        'population': '12,966,836',
        'fact': 'capital of hn, high school city',
        },
    'sy': {
        'nation': 'cn',
        'population': '8,100,000',
        'fact': 'university city',
        },
    }

for city, info in cities.items():
    print('n' + city + ': ')
    for k, v in info.items():
        print(k + ': ' + v)

6-12
...




欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4830232.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-10
下一篇 2022-11-10

发表评论

登录后才能评论

评论列表(0条)

保存