6-1
person={
"first_name":"Yuan",
"last_name":"Fang",
"city":"Guangzhou",
"age":20,
}
print(person)
6-2
friends={
"Fang Yuan":19980114,
"Zhang Tongrui":19980713,
"Wang Zeyuan":19980715,
"Zhao Minglai":19981001,
}
for friend in friends.keys():
print("Dear "+friend+" ,is your favorite number "+str(friends[friend])+" ?")
6-3
dictionary={
"Fang":"family name",
"Yuan":"first name",
"month":1,
"day":14,
"year":1998
}
for source in dictionary.keys():
print(source+" : "+str(dictionary[source]))
6-4
dictionary={
"Fang":"family name",
"Yuan":"first name",
"month":1,
"day":14,
"year":1998,
}
for source,definition in dictionary.items():
print(source+" : "+str(definition))
dictionary["Zhang"]="family name"
dictionary["Tongrui"]="first name"
dictionary["month_2"]=7
dictionary["day_2"]=13
dictionary["year_2"]=1998
for source,definition in dictionary.items():
print(source+" : "+str(definition))
先写到这,困了,明天继续。。。。。。
更新:
6-5
cities={
"Beijing":"China",
"Paris":"France",
"Washington":"USA",
}
for city,country in cities.items():
print(city+" is in "+country+".")
for city in cities.keys():
print(city)
for country in cities.values():
print(country)
6-6
favorite_languages={
"jen":"python",
"sarah":"c",
"edward":"ruby",
"phil":"python",
}
people=["lily","sarah","phil","cindy","tom"]
for person in people:
if person in favorite_languages.keys():
print("Thank you!")
else:
print("Please take the test!")
6-7
person_1={
"first_name":"Yuan",
"last_name":"Fang",
"city":"Guangzhou",
"age":20,
}
person_2={
"first_name":"Tongrui",
"last_name":"Zhang",
"city":"Guangzhou",
"age":19,
}
person_3={
"first_name":"Zeyuan",
"last_name":"Wang",
"city":"Beijing",
"age":19,
}
people=[person_1,person_2,person_3]
for person in people:
print(person)
6-8
FY={
"type":"dog",
"owner":"Cody"
}
Lily={
"type":"cat",
"owner":"Cindy"
}
Sandy={
"type":"rabbit",
"owner":"Tom"
}
pets=[FY,Lily,Sandy]
print(pets)
6-9
favorite_places={
"Fang Yuan":["Paris","Chengdu"],
"Zhang Tongrui":["Paris","Beijing"],
"Wang Zeyuan":["Jilin"],
}
for person,places in favorite_places.items():
print(person,end=":")
print(places)
6-10
friends={
"Fang Yuan":[1998,1,14],
"Zhang Tongrui":[1998,7,13],
"Wang Zeyuan":[1998,7,15],
"Zhao Minglai":[1998,10,1],
}
for friend,information in friends.items():
print(friend,end=":")
print(information)
6-11
cities={
"Beijing":{
"country":"China",
"population":"a lot",
},
"Pairs":{
"country":"France",
"population":"quite a few",
},
"Changchun":{
"country":"China",
"population":"not that much",
},
}
for city,information in cities.items():
print(city,end=":")
print(information)