美文网首页
python从入门到编程习题第六章

python从入门到编程习题第六章

作者: 数据分析历程 | 来源:发表于2020-03-28 21:00 被阅读0次

    #6-1 人

    a = {"first_name":"xiao","last_name":"ming","age":19,"city":"shanghai"}

    print(a)

    #6-2喜欢的数字

    friends_num = {"xiaoming":1,"xiaohong":9,"xiaozhang":8,"xiaoli":3}

    for k,v in favorite_nums.items():

        print(k ," favorite number is ",v)

    #6-3词汇表

    tables = {

        "del" : "删除元素",

        "insert": "指定位置增加元素",

        "pop":"删除元素并返回删除的元素",

        "append":"插入元素"

    }

    for k,v in tables.items():

        print(k +":"+ v + "\n")

    #6-4词汇表2

    tables = {

        "del" : "删除指定位置的元素",

        "insert": "指定位置增加元素",

        "pop":"删除元素并返回删除的元素",

        "append":"插入元素"

    }

    tables["expend"] = "增加一个序列的多个元素"

    tables["remove"] ="删除第一个遇到的指定值"

    tables["sort"] = "排序且改变原数据结构"

    tables["sorted"] ="排序且不改变原结构"

    tables["reverse"] = "反转且改变原数据结构"

    for k,v in tables.items():

        print(k +":"+ v + "\n")

    #6-5河流

    rivers = {"Huanghe":"ShanXi","Changjiang":"QingHai","Songhuajiang":"HeiLongJiang"}

    for k,v in rivers.items():

        print("The " +k + " runs through " + v )

    for k in rivers.keys():

        print(k)

    for v in rivers.values():

        print(v)

    #6-6调查

    favorite_languages = {

        'jen':'python',

        'sarah':'c',

        'edward':'ruby',

        'phil':'python'

    }

    names = ["xiaofang","xiaoyang","jen","sarah","xiaozhang"]

    for name in names:

        if name in favorite_languages:

            print("Hi, " + name + " Thanks for you participation!")

        else:

            print("Hi, "+ name + " We are invite you to research!")

    #6-7人

    a = {"first_name":"xiao",

        "last_name":"ming",

        "age":19,

        "city":"shanghai"}

    b = {"first_name":"xiao",

        "last_name":"gang",

        "age":18,

        "city":"beijing"}

    c = {"first_name":"xiao",

        "last_name":"hong",

        "age":21,

        "city":"guangzhou"}

    peoples = [a,b,c]

    for people in peoples:

        print(people)

    #6-8宠物

    pet1 ={"name":"huahua","type":"cat","pet owners":"xiaohong"}

    pet2 = {"name":"dagou","type":"dog","pet owners":"xiaozhang"}

    pet3 ={"name":"shuashua","type":"cat","pet owners":"xiaowang"}

    pet4 = {"name":"lulu","type":"dog","pet owners":"xiaozhao"}

    pets = [pet1,pet2,pet3,pet4]

    for pet in pets:

        print(pet)

    #6-9喜欢的地方

    favorite_places={"xiaogang":["shanghai","xian","hangzhou"],

                    "xiaoming":["beijing","shanxi","nanjing"],

                    "xiaohong":["shenzhen","hainan","shandong"]}

    for name,place in favorite_places.items():

        print("\n"+name+ " Favorite Places are :\n" +"\n".join(place))

    #6-10喜欢的数字

    friends_num = {"xiaoming":1,"xiaohong":9,"xiaozhang":8,"xiaoli":3}

    for k,v in friends_num.items():

        print(k.title(),"'s favorite number is : " ,v)

    相关文章

      网友评论

          本文标题:python从入门到编程习题第六章

          本文链接:https://www.haomeiwen.com/subject/scyluhtx.html