python:从入门到实践 第六章练习

python:从入门到实践 第六章练习

#6-1
friend={'first_name':'zhang','last_name':'san','age':16,'city':'Beijing'}
print(friend['first_name'])
print(friend['last_name'])
print(friend['age'])
print(friend['city'])

#6-2
lovenumbers={'zhangsan':4,'xiaowang':5,'lisi':6,'wangwu':7,'dingqi':8}
print(f"zhangsan loves {lovenumbers['zhangsan']}")
print(f"xiaowang loves {lovenumbers['xiaowang']}")
print(f"lisi loves {lovenumbers['lisi']}")
print(f"wangwu loves {lovenumbers['wangwu']}")
print(f"dingqi loves {lovenumbers['dingqi']}")

#6-3
glossary={
    'string':'A series of characters',
    'comment':'A note in program that the python interpreter ignores',
    'list':'A collection of items in a particular order',
    'loop':'Work through a collection of items,one at one time',
    'dictionary':'A collection of key-values pairs'
}

#6-4
for key,value in glossary.items():
    print(f"key:{key},value:{value}")
glossary['boolean']='an expression that  values true of false'
glossary['float']='A nummerical value with a decimal component'
for key,value in glossary.items():
    print(f"key:{key},value:{value}")

#6-5
rivers={'nile':'Egypt','longriver':'China','Yellowriver':'China'}
for river,country in rivers.items():
    print(f"The {river} runs through {country}.")

#6-6
favorite_languages={
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'pil':'python'
}
coders=['phil','josh','david','becca','sarah','matt','daielle']
for name in favorite_languages.keys():
    print(f"{name},thank you!")
    for users in coders:
        if users not in favorite_languages.keys():
            print(f"{users},do the survey!")

#6-7
people=[]
person={
    'first_name':'eric',
    'last_name':'matthes',
    'age':46,
    'city':'sitka'
}
person2={
    'first_name':'erics',
    'last_name':'john',
    'age':63,
    'city':'cidney'
}
people.append(person)
people.append(person2)
for p in people:
    print(p['age'])
    print(p['first_name'])
    print(p['last_name'])
    print(p['city'])

#6-8
pets=[]
pets1={'type':'dog','owner':'nancy'}
pets2={'type':'cat','owner':'mike'}
pets3={'type':'fish','owner':'yanglin'}
pets.append(pets1)
pets.append(pets2)
for p in pets:
    print(p['type'])
    print(p['owner'])

#6-9
favorite_places={'nancy':['qingdao','shanghai','wuxi'],
                  'mike':['chongqing','wuhan','dali'],
                  'yanglin':['jinagxi','dalian','suzhou']
                  }
for person,places in favorite_places.items():
    print(person)
    print(places)

#6-10
favorite_nums={'nancy':[1,2,3],
               'mike':[2,4,6],
               'jack':[3,5,6]
               }
for person,num in favorite_nums.items():
    print(person)
    print(num)

#6-11
cities={
    'shanghai':{'country':'China','people':10000,'fact':'beautiful'},
    'chongqing':{'country':'china','people':1000,'fact':'modern'},
    'yunan':{'country':'china','people':100,'fact':'many mushrooms'}
}
for city,information in cities.items():
    print(city)
    print(information['country'])
    print(information['people'])
    print(information['fact'])

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值