您的位置:首页 > 编程语言

【HW】 第六章作业 2018.3.21

2018-03-21 23:48 274 查看
Python代码:#Chapter 6 by szh 2018.3.21
print("\n6.1")
person1 = {'first_name':'Alan', 'last_name':'Turing', 'age':28, 'city':'London'}
for k in person1.keys():
print(k, ':', person1[k])

print("\n6.2")
favorite_num = {'Alice':3, 'Bob':7, 'Jack':11, 'John':13, 'Eric': 17}
for k, v in favorite_num.items():
print(k, ':', v)

print("\n6.3")
key_words = {'&':'and', '|':'or', '^':'xor', '~':'not'}
for k, v in key_words.items():
print(k + "\n\t" + v)

print("\n6.4")
for k, v in key_words.items():
print(k + "\n\t" + v)
key_words['int'] = 'Integer'
key_words['float'] = 'Single-precision floating-point'
key_words['double'] = 'Double-precision floating-point'
key_words['char'] = 'character'
for k, v in key_words.items():
print(k + "\n\t" + v)

print("\n6.5")
rivers = {'nile':'Egypt', 'Amazon':'Brazil', 'Yangzi River':'China'}
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)

print("\n6.7")
person2 = {'first_name':'Albert', 'last_name':'Einstein', 'age':58, 'city':'Washington'}
people = [person1, person2]
for person in people:
for k in person.keys():
print(k, ':', person[k])

print("\n6.8")
cat = {'Doli':'Alice'}
dog = {'hero':'Bob'}
rabbit = {'milk':'Jack'}
pets = [cat, dog, rabbit]
for pet in pets:
for k, v in pet.items():
print(k + ':' + v)

print("\n6.9")
fp1 = {'Alice':['London', 'Beijing', 'Zhuhai']}
fp2 = {'Bob': ['Paris', 'Washington']}
fp3 = {'Eric': ['Moscow', 'Tokyo']}
favorite_places = [fp1, fp2 ,fp3]
for favorite_place in favorite_places:
for k, v in favorite_place.items():
print(k + ":", end = '')
for place in v:
print(place + ' ', end = '')
print()

输出结果:6.1
first_name : Alan
last_name : Turing
age : 28
city : London

6.2
Alice : 3
Bob : 7
Jack : 11
John : 13
Eric : 17

6.3
&
and
|
or
^
xor
~
not

6.4
&
and
|
or
^
xor
~
not
&
and
|
or
^
xor
~
not
int
Integer
float
Single-precision floating-point
double
Double-precision floating-point
char
character

6.5
The nile runs through Egypt.
The Amazon runs through Brazil.
The Yangzi River runs through China.
nile
Amazon
Yangzi River
Egypt
Brazil
China

6.7
first_name : Alan
last_name : Turing
age : 28
city : London
first_name : Albert
last_name : Einstein
age : 58
city : Washington

6.8
Doli:Alice
hero:Bob
milk:Jack

6.9
Alice:London Beijing Zhuhai
Bob:Paris Washington
Eric:Moscow Tokyo

输出结果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息