names = ['Micheal','James','Black','Smith']
for name in names:
print(name)
Micheal
James
Black
Smith
names = ['Micheal','James','Black','Smith']
for name in names:
print(name + ", nice to meet you!")
Micheal, nice to meet you!
James, nice to meet you!
Black, nice to meet you!
Smith, nice to meet you!
vehicles = ['bycicle','motorcycle','car']
brands = ['GIANT','Honda','Benz']
for num in range(0,3):
print("I would like to own a " + brands[num] + " " +vehicles[num] + ".")
I would like to own a GIANT bycicle.
I would like to own a Honda motorcycle.
I would like to own a Benz car.
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
3-5-1 已完成练习3-4时编写的程序为基础,在程序末尾添加一条print语句,指出哪位嘉宾无法赴约。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\n" + celebrities[2] + " can't have dinner together.")
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
K.J. Apa can't have dinner together.
3-5-2 修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\n" + celebrities[2] + " can't have dinner together.")
del celebrities[2]
celebrities.append("Rachel Antonoff")
3-5-2 再次打印一系列消息,向名单中的每位嘉宾发出邀请。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\n" + celebrities[2] + " can't have dinner together.\n")
del celebrities[2]
celebrities.append("Rachel Antonoff")
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
K.J. Apa can't have dinner together.
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
Rachel Antonoff, May I have the plea sure of inviting you to dinner?
3-6-1 以完成练习3-4或练习3-5时编写的程序为基础,在程序末尾添加一条print语句,指出你找到了一个更大的餐桌。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\nI booked a bigger table.")
3-6-2 使用insert()将一位新嘉宾添加到名单开头。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\nI booked a bigger table.")
celebrities.insert(0,"Bar Refaeli")
3-6-3 使用insert()将另一位新嘉宾添加到名单中间。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\nI booked a bigger table.")
celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
3-6-4 使用append()将最后一位新嘉宾添加到名单末尾。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\nI booked a bigger table.")
celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
celebrities.append("Daiane Conterato")
3-6-5 打印一系列消息,向名单中的每位嘉宾发出邀请。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
print("I booked a bigger table.\n")
celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
celebrities.append("Daiane Conterato")
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
I booked a bigger table.
Bar Refaeli, May I have the plea sure of inviting you to dinner?
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
Cacee Cobb, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
Daiane Conterato, May I have the plea sure of inviting you to dinner?
3-7-1 已完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
print("I booked a bigger table.\n")
celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
celebrities.append("Daiane Conterato")
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\nI only can invite two guests because the table newly bought can't be delivered in time.")
I booked a bigger table.
Bar Refaeli, May I have the plea sure of inviting you to dinner?
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
Cacee Cobb, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
Daiane Conterato, May I have the plea sure of inviting you to dinner?
I only can invite two guests because the table newly bought can't be delivered in time.
3-7-2 使用pop()不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
print("I booked a bigger table.\n")
celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
celebrities.append("Daiane Conterato")
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\nI only can invite two guests because the table newly bought can't be delivered in time.\n")
while len(celebrities) > 2:
name = celebrities.pop()
print(name + ", I sorry that I can't invite you to have dinnner together.")
I booked a bigger table.
Bar Refaeli, May I have the plea sure of inviting you to dinner?
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
Cacee Cobb, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
Daiane Conterato, May I have the plea sure of inviting you to dinner?
I only can invite two guests because the table newly bought can't be delivered in time.
Daiane Conterato, I sorry that I can't invite you to have dinnner together.
Padma Lakshmi, I sorry that I can't invite you to have dinnner together.
K.J. Apa, I sorry that I can't invite you to have dinnner together.
Cacee Cobb, I sorry that I can't invite you to have dinnner together.
Ed Burns, I sorry that I can't invite you to have dinnner together.
3-7-3 对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
print("I booked a bigger table.\n")
celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
celebrities.append("Daiane Conterato")
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\nI only can invite two guests because the table newly bought can't be delivered in time.\n")
while len(celebrities) > 2:
name = celebrities.pop()
print(name + ", I sorry that I can't invite you to have dinnner together.")
for celebrity in celebrities:
print(celebrity + ", I hope you can still have dinner with me.")
I booked a bigger table.
Bar Refaeli, May I have the plea sure of inviting you to dinner?
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
Cacee Cobb, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
Daiane Conterato, May I have the plea sure of inviting you to dinner?
I only can invite two guests because the table newly bought can't be delivered in time.
Daiane Conterato, I sorry that I can't invite you to have dinnner together.
Padma Lakshmi, I sorry that I can't invite you to have dinnner together.
K.J. Apa, I sorry that I can't invite you to have dinnner together.
Cacee Cobb, I sorry that I can't invite you to have dinnner together.
Ed Burns, I sorry that I can't invite you to have dinnner together.
Bar Refaeli, I hope you can still have dinner with me.
Aaron Eckhart, I hope you can still have dinner with me.
3-7-4 使用del将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
print("I booked a bigger table.\n")
celebrities.insert(0,"Bar Refaeli")
celebrities.insert(3,"Cacee Cobb")
celebrities.append("Daiane Conterato")
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\nI only can invite two guests because the table newly bought can't be delivered in time.\n")
while len(celebrities) > 2:
name = celebrities.pop()
print(name + ", I sorry that I can't invite you to have dinnner together.")
for celebrity in celebrities:
print(celebrity + ", I hope you can still have dinner with me.")
del celebrities[0]
del celebrities[0]
print("\nThe list is:")
print(celebrities)
I booked a bigger table.
Bar Refaeli, May I have the plea sure of inviting you to dinner?
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
Cacee Cobb, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
Daiane Conterato, May I have the plea sure of inviting you to dinner?
I only can invite two guests because the table newly bought can't be delivered in time.
Daiane Conterato, I sorry that I can't invite you to have dinnner together.
Padma Lakshmi, I sorry that I can't invite you to have dinnner together.
K.J. Apa, I sorry that I can't invite you to have dinnner together.
Cacee Cobb, I sorry that I can't invite you to have dinnner together.
Ed Burns, I sorry that I can't invite you to have dinnner together.
Bar Refaeli, I hope you can still have dinner with me.
Aaron Eckhart, I hope you can still have dinner with me.
The list is:
[]
3-8-1 将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。
Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]
3-8-2 按原始排列顺序打印该列表。
Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]
print(Resort)
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']
3-8-3 使用sorted()按字母顺序打印这个列表。
Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]
print(Resort)
print()
print(sorted(Resort))
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']
['Aegean Sea', 'Pink Sands', 'Rose Lake', 'Santorini', 'The blue hole']
3-8-4 再次打印该列表,核实排列顺序确实未变。
Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]
print(Resort)
print()
print(sorted(Resort))
print()
print(Resort)
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']
['Aegean Sea', 'Pink Sands', 'Rose Lake', 'Santorini', 'The blue hole']
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']
3-8-5 使用sorted()按与字母顺序相反的顺序打印这个列表。
Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]
print(sorted(Resort,reverse = True))
['The blue hole', 'Santorini', 'Rose Lake', 'Pink Sands', 'Aegean Sea']
3-8-6 再次打印该列表,核实排列顺序确实未变。
Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]
print(sorted(Resort,reverse = True))
print()
print(Resort)
['The blue hole', 'Santorini', 'Rose Lake', 'Pink Sands', 'Aegean Sea']
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']
3-8-7 使用reverse()修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。
Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]
print(Resort)
Resort.reverse()
print()
print(Resort)
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']
['The blue hole', 'Rose Lake', 'Pink Sands', 'Aegean Sea', 'Santorini']
3-8-8 使用reverse()再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。
Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]
print(Resort)
Resort.reverse()
print()
print(Resort)
Resort.reverse()
print()
print(Resort)
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']
['The blue hole', 'Rose Lake', 'Pink Sands', 'Aegean Sea', 'Santorini']
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']
3-8-9 使用sort()修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了。
Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]
print(Resort)
Resort.sort()
print()
print(Resort)
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']
['Aegean Sea', 'Pink Sands', 'Rose Lake', 'Santorini', 'The blue hole']
3-8-10 使用sort()修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。
Resort = ["Santorini","Aegean Sea","Pink Sands","Rose Lake","The blue hole"]
print(Resort)
Resort.sort(reverse = True)
print()
print(Resort)
['Santorini', 'Aegean Sea', 'Pink Sands', 'Rose Lake', 'The blue hole']
['The blue hole', 'Santorini', 'Rose Lake', 'Pink Sands', 'Aegean Sea']
celebrities = ["Aaron Eckhart","Ed Burns","K.J. Apa","Padma Lakshmi"]
for celebrity in celebrities:
print(celebrity + ", May I have the plea sure of inviting you to dinner?")
print("\nI total invite " + str(len(celebrities)) + " guests.")
Aaron Eckhart, May I have the plea sure of inviting you to dinner?
Ed Burns, May I have the plea sure of inviting you to dinner?
K.J. Apa, May I have the plea sure of inviting you to dinner?
Padma Lakshmi, May I have the plea sure of inviting you to dinner?
I total invite 4 guests.
words = ["cat","apple","dog","boy"]
words.insert(0,"egg")
word = words.pop()
words.append(word)
del words[len(words)-1]
words.remove(words[0])
words.sort()
words.reverse()
print(sorted(words))
['apple', 'cat', 'dog']
words = ["apple","boy"]
del words[0]
del words[1]
print(words)
Traceback (most recent call last):
File "Test.py", line 4, in
del words[1]
IndexError: list assignment index out of range
修改为
words = ["apple","boy"]
del words[0]
del words[0] # del words[1] is wrong.
print(words)
[]