Python編程作業【第二週】(一)

3-1 names

names = ['zhaobinqi', 'zhuzhiru', 'zongjie']
for name in names:
    print (name)

3-2 greet words

names = ['zhaobinqi', 'zhuzhiru', 'zongjie']
for name in names:
    print (name.title() + ', nice to meet you!')

3-3 my list

vehicles = ['bike', 'car', 'train', 'subway']
for vehicle in vehicles:
    print ("I love taking " + vehicle)

3-4 honored guests

persons = ['father', 'mother', 'zhaobinqi']
for person in persons:
    print (person + ", hello! I would like to invite you to eat dinner with me!")

3-5 change the list

persons = ['father', 'mother', 'zhaobinqi']
for person in persons:
    print (person + ", hello! I would like to invite you to eat dinner with me!")
print ("\nSorry to hear that mother can not be in present")
persons.remove("mother")
persons.append("grandpa")
for person in persons:
    print (person + ", hello! I would like to invite you to eat dinner with me!")

3-6 add new names to the list

persons = ['father', 'mother', 'zhaobinqi']
for person in persons:
    print (person + ", hello! I would like to invite you to eat dinner with me!")
print ("\nSorry to hear that mother can not be in present")
persons.remove("mother")
persons.append("grandpa")
for person in persons:
    print (person + ", hello! I would like to invite you to eat dinner with me!")
print ("Glad to say that I've found another bigger table and I would like to invite another three persons")
persons.insert(0, "zongjie")
persons.insert(2, "zhuzhiru")
persons.append("zouyuanhao")
for person in persons:
    print (person + ", hello! I would like to invite you to eat dinner with me!")

3-7 delete the list

persons = ['father', 'mother', 'zhaobinqi']
for person in persons:
    print (person + ", hello! I would like to invite you to eat dinner with me!")
print ("\nSorry to hear that mother can not be in present")
persons.remove("mother")
persons.append("grandpa")
for person in persons:
    print (person + ", hello! I would like to invite you to eat dinner with me!")
print ("Glad to say that I've found another bigger table and I would like to invite another three persons")
persons.insert(0, "zongjie")
persons.insert(2, "zhuzhiru")
persons.append("zouyuanhao")
for person in persons:
    print (person + ", hello! I would like to invite you to eat dinner with me!")
print ("Sorry to say that I could only invite 2 guests to my dinner")
print  ("Sorry, " + persons.pop() + ", I con't invite you.")
print  ("Sorry, " + persons.pop() + ", I con't invite you.")
print  ("Sorry, " + persons.pop() + ", I con't invite you.")
print  ("Sorry, " + persons.pop() + ", I con't invite you.")
for person in persons:
    print (person + ", hello! You are still on the list!")
del persons[0]
del persons[0]
print (persons)

3-8 the world

countries = ["China", "America", "British", "Canada", "France"]
print (countries)
print (sorted(countries))
countries.reverse()
print (countries)
countries.sort()
print (countries)

3-9 length

countries = ["China", "America", "British", "Canada", "France"]
print (len(countries))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章