zoukankan      html  css  js  c++  java
  • Python:从入门到实践--第六章--字典--练习

    #1.人:使用一个字典来存储一个熟人的信息;包括姓,名,年龄和居住的城市。将字典中的每项信息都打印出来
    friend = {
        'last_name':'',
        'first_name':'脑壳',
        'age':'23',
        'live_city':'北京',
        }
    print('姓名:'+friend['last_name'] + friend['first_name'] + '
    '+ '年龄:'+friend['age'] +
    '
    ' + '居住城市:'+friend['live_city'])
    
    print('
    ')
    #2.喜欢的数字:使用一个字典来存储一些人喜欢的数字,打印每个人的名字和喜欢的数字。
    datas = {'Mike':'250','Lucy':'520','Neko':'3344','Neo':'666'}
    print('Mike favorite number is :' + datas['Mike'])
    print('Lucy favorite number is :' + datas['Lucy'])
    print('Neko favorite number is :' + datas['Neko'])
    print('Neo favorite number is :' + datas['Neo'])
    
    print('
    ')
    #3.词汇表:Python字典可用于模拟现实生活中的字典,单位了避免混淆,我们将后者称为词汇表。
    #将已学过的编程词汇作为词汇表中的键,并将它们的含义作为值存储在词汇表中
    #以整洁的方式打印每个词汇及含义
    
    words = {'for':'循环方式','print':'打印输出在屏幕显示','pop()':'从列表结尾中删除数据','sort':'按字母从小到大永久排序'}
    
    print('for:' + words['for'])
    print('print:' + words['print'])
    print('pop():' + words['pop()'])
    print('sort:' + words['sort'])
    #1.词汇表:Python字典可用于模拟现实生活中的字典,单位了避免混淆,我们将后者称为词汇表。
    #将已学过的编程词汇作为词汇表中的键,并将它们的含义作为值存储在词汇表中
    #以整洁的方式打印每个词汇及含义
    #遍历并打印出整个字典的键和值
    
    words = {'for':'循环方式','print':'打印输出在屏幕显示','pop()':'从列表结尾中删除数据','sort':'按字母从小到大永久排序'}
    
    for key,value in words.items():
        print(key + ' : ' + value )
        
    
    print('
    ')
    #2.河流:创建一个字典,在其中存储三条大河流及流经的国家
    #使用循环打印出来
    
    rivers = {'Changjianghuanghe':'China','Nile':'Egypt','Der Rhein':'Germany'}
    
    for river,country in rivers.items():
        print('the ' + river + ' runs through ' + country)
        
        
    print('
    ')
    #3.调查:创建一个应该会接受调查的人员名单,其中有些人已包含在字典中,而其他人未包含在字典中
    #遍历这个人员名单,对于已参与调查的人,打印一条消息表示感谢。对于还未参与的人,打印一条消息邀请他参与调查
    
    users = {'Mike':'apple','Neo':'banana','Lucy':'strawberry','Jan':'watermelon'}
    new_users = ['Mk','Mv','Mike','Lucy','zhangsan']
    
    for name in users.keys():
        if name in new_users:
            print(name + ',thank you for your cooperation!')
        else:
            print(name + ',can I invite you for a report?')
    
    
            
    #课本上例子改进后的程序
    
    favorite_languages = {
        'jen':['python','ruby'],
        'sarah':['C'],
        'edward':['ruby','go'],
        'phil':['python','haskell'],
        }
    
    for name,languages in favorite_languages.items():
        if len(languages) <=1:
            print('
    ' + name.title() + "'s favorite languages is:")
            for language in languages:
                print("	" + language.title())
        else:
            print('
    ' + name.title() + "'s favorite languages are:")
            for language in languages:
                print("	" + language.title())
                
    
    print('
    ')
    #1.朋友:创建三个表示人的字典,然后将这三个字典都储存在一个命名为people的列表里
    #遍历列表,并将每个人的信息打印出来
    friend1 = {
        'last_name':'',
        'first_name':'脑壳',
        'age':'22',
        'live_city':'北京',
        }
    friend2 = {
        'last_name':'',
        'first_name':'小波',
        'age':'23',
        'live_city':'上海',
        }
    friend3 = {
        'last_name':'',
        'first_name':'短命',
        'age':'21',
        'live_city':'贵州',
        }
        
    people = [friend1,friend2,friend3]
    for friend in people:
        print('姓名:' + friend['last_name'] + friend['first_name'] + '
    ' +'年龄:' + friend['age']
         + '
    ' + '城市:' + friend['live_city'])
         
    
    print('
    ')
    #城市:创建一个命名为cities的字典,其中将三个城市作为键;对于每个城市都创建一个字典,并在其中包含该城市
    #所属于的国家,人口约数和该城市的事实,并将每座城市的这些信息打印出来
    cities = {
        '北京':{'country':'China','population':'十万','fact':"It's a beautiful city. "},
        '旧金山':{'country':'Americ','population':'二十万','fact':"It's a good place for films."},
        '巴黎':{'country':'France','population':'十万','fact':"It’s a romantic place."},
        }
         
    for city,messages in cities.items():
        print('城市:'+ city + '
    ' + '属于:' + messages['country'] +'
    ' + '人口:'+messages['population']
        + '
    ' + '事实:' + messages['fact'])
        
     
    
    #课本上例子改进后的程序
    favorite_languages = {'jen':['python','ruby'],'sarah':['C'],'edward':['ruby','go'],'phil':['python','haskell'],}
    for name,languages in favorite_languages.items():if len(languages) <=1:print('
    ' + name.title() + "'s favorite languages is:")for language in languages:print("	" + language.title())else:print('
    ' + name.title() + "'s favorite languages are:")for language in languages:print("	" + language.title())
    print('
    ')#1.朋友:创建三个表示人的字典,然后将这三个字典都储存在一个命名为people的列表里#遍历列表,并将每个人的信息打印出来friend1 = {'last_name':'马','first_name':'脑壳','age':'22','live_city':'北京',}friend2 = {'last_name':'梁','first_name':'小波','age':'23','live_city':'上海',}friend3 = {'last_name':'罗','first_name':'短命','age':'21','live_city':'贵州',}people = [friend1,friend2,friend3]for friend in people:print('姓名:' + friend['last_name'] + friend['first_name'] + '
    ' +'年龄:' + friend['age'] + '
    ' + '城市:' + friend['live_city']) 
    print('
    ')#城市:创建一个命名为cities的字典,其中将三个城市作为键;对于每个城市都创建一个字典,并在其中包含该城市#所属于的国家,人口约数和该城市的事实,并将每座城市的这些信息打印出来cities = {'北京':{'country':'China','population':'十万','fact':"It's a beautiful city. "},'旧金山':{'country':'Americ','population':'二十万','fact':"It's a good place for films."},'巴黎':{'country':'France','population':'十万','fact':"It’s a romantic place."},} for city,messages in cities.items():print('城市:'+ city + '
    ' + '属于:' + messages['country'] +'
    ' + '人口:'+messages['population']+ '
    ' + '事实:' + messages['fact'])
    
     
  • 相关阅读:
    Beta冲刺 5
    Beta冲刺 4
    Beta冲刺 3
    Beta冲刺 2
    Beta冲刺 1
    项目评测博客
    Beta冲刺前准备
    Alpha 冲刺11——总结
    Alpha冲刺10
    Alpha冲刺9
  • 原文地址:https://www.cnblogs.com/geeker-xjl/p/10635298.html
Copyright © 2011-2022 走看看