1. 程式人生 > >Python程式設計入門到實踐Chapter-06

Python程式設計入門到實踐Chapter-06

"""
Exercise 6-1:
人

"""

my_friend = {'first_name': 'dylan',
             'last_name': 'smith',
             'age': 18,
             'city': 'shenzhen'}

print(my_friend['first_name'])
print(my_friend['last_name'])
print(my_friend['age'])
print(my_friend['city'])
"""
Exercise 6-2:
喜歡的數字

"""

favorite_number = {
    'dylan': 1,
    'jack': 2,
    'tom': 3,
    'mack': 4,
}

print("dylan's favorite number is %d" %favorite_number['dylan'])
print("jack's favorite number is %d" %favorite_number['jack'])
print("tom's favorite number is %d" %favorite_number['tom'])
print("mack's favorite number is %d" %favorite_number['mack'])
"""
Exercise 6-3:
詞彙表

"""

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

print("string: %s" %glossary['string'])
print("\ncomment: %s" %glossary['comment'])
print("\nlist: %s" %glossary['list'])
print("\nloop: %s" %glossary['loop'])
print("\ndictionary: %s" %glossary['dictionary'])
"""
Exercise 6-4:
詞彙表2

"""

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

for key, value in glossary.items():
    print("%s : %s" %(key.title(), value.title()))

glossary['float'] = 'A numerical value with a decimal component.'
glossary['conditional test'] = 'A comparison between two values.'
print('')
for key, value in glossary.items():
    print("%s : %s" %(key.title(), value.title()))
"""
Exercise 6-5:
河流

"""

rivers = {
    'nile': 'egypt',
    'mississippi': 'united states',
    'fraser': 'canada',
    'kuskokwim': 'alaska',
    'yangtze': 'china',
    }

for river, country in rivers.items():
    print("The river %s flows through %s." %(river, country))
print('')

print("The rivers included in this data set are:")
for river in rivers.keys():
    print("- %s" %river)
print('')

print("The countries included in this data set are:")
for country in rivers.values():
    print("- %s" %country)
"""
Exercise 6-6:
調查

"""

favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
    }

for name, language in favorite_languages.items():
    print("%s's favorite language is %s." %(name.title(), language.title()))
print('')

coders = ['jen', 'phil', 'a', 'b', 'c']
for coder in coders:
    if coder in favorite_languages.keys():
        print("Thank you for talking the poll %s" %(coder.title()))
    else:
        print("%s, would you like to talk a poll?" %coder.title())
"""
Exercise 6-7:
人

"""
names = []
my_friend1 = {'first_name':'dylan',
             'last_name':'smith',
             'age':18,
             'city':'shenzhen'}
names.append(my_friend1)

my_friend2 = {'first_name':'tom',
             'last_name':'jason',
             'age':19,
             'city':'newyork'}
names.append(my_friend2)

my_friend3 = {'first_name':'july',
             'last_name':'mack',
             'age':20,
             'city':'tokyo'}
names.append(my_friend3)

for people in names:
    name = people['first_name'].title() + ' ' + people['last_name'].title()
    age = str(people['age'])
    city = people['city'].title()

    print("%s from %s is %s" %(name, city, age))
"""
Exercise 6-8:
寵物

"""

pets = []

pet1 = {
    'animal type':'python',
    'name':'a',
    'owner':'dylan',
}
pets.append(pet1)

pet2 = {
    'animal type':'chiken',
    'name':'b',
    'owner':'jack',
}
pets.append(pet2)

pet3 = {
    'animal type':'puppy',
    'name':'c',
    'owner':'jason',
}
pets.append(pet3)

for pet in pets:
    print("\n" + pet['name'].title())
    for key, value in pet.items():
        print("%s: %s" %(key, value))
"""
Exercise 6-9:
喜歡的地方

"""

favorite_places = {
    'dylan': ['Tokyo', 'London', 'Paris'],
    'jack': ['Thailand', 'Japan', 'The United States'],
    'mary': ['Beijing', 'Hongkong', 'Taiwan']
}

for name, places in favorite_places.items():
    print("\nAbout %s:" %name)
    for place in places:
        print("-%s" %place)
"""
Exercise 6-10:
喜歡的數字

"""

favorite_numbers = {
    'dylan': [1, 2, 3],
    'jack': [4, 5, 6],
    'mary': [7, 8, 9]
}

for name, numbers in favorite_numbers.items():
    print("\nAbout %s" %name)
    for number in numbers:
        print(number)
"""
Exercise 6-11:
城市

"""

cities = {
    'Tokyo':{
        'country': 'Japan',
        'population': '1350',
        'fact': 'Cartoon is popular there'
    },
    'Hongkong':{
        'country': 'China',
        'population':'740',
        'fact': 'Cultures there are varied'
    },
    'Newyork':{
        'country': 'The United States',
        'population': '851',
        'fact': 'The best richest city in the world'
    }
}

for name, values in cities.items():
    print("\nAbout %s:" %name)
    for key, value in values.items():
        print(key + ': ' + value)