3-1 姓名:
代码:
names = ['Hong siyuan', 'lin dongwei', 'chen peipeng', 'li yinwei']
for name in names:
print(name.title())
运行结果:
3-2 问候语:
代码:
names = ['Hong siyuan', 'lin dongwei', 'chen peipeng', 'li yinwei']
for name in names:
print("Hello, " + name.title())
运行结果:
3-3 自己的列表:
代码:
transportions = ['Honda motorcycle', 'ship', 'car', 'bus']
for transportion in transportions:
print("I would like to own a " + transportion + "!")
运行结果:
3-4 嘉宾名单:
代码:
names = ['Hong siyuan', 'lin dongwei', 'chen peipeng', 'li yinwei']
for name in names:
print(name.title() + ", Would you ilke to have a dinner with me?")
运行结果:
3-5 修改嘉宾名单:
代码:
names = ['Hong siyuan', 'lin dongwei', 'chen peipeng', 'li yinwei']
for name in names:
print(name.title() + ", Would you ilke to have a dinner with me?")
print("\n\n" + names[0].title() + " could not take part in this dinner.\n\n")
names[0] = "yang guidong"
for name in names:
print(name.title() + ", Would you ilke to have a dinner with me?")
运行结果:
3-6 添加嘉宾:
代码:
names = ['Hong siyuan', 'lin dongwei', 'chen peipeng', 'li yinwei']
for name in names:
print(name.title() + ", Would you ilke to have a dinner with me?")
print("\n\nI find a larger dinner table!So I want more people to join in it!\n\n")
names.insert(0, "fang xiaona")
names.insert(2, "li huikeng")
names.append("huang jiahui")
for name in names:
print(name.title() + ", Would you ilke to have a dinner with me?")
运行结果:
3-7 减缩名单:
代码:
names = ['Hong siyuan', 'lin dongwei', 'chen peipeng', 'li yinwei']
for name in names:
print(name.title() + ", Would you ilke to have a dinner with me?")
print("\n\nI find a larger dinner table!So I want more people to join in it!\n\n")
names.insert(0, "fang xiaona")
names.insert(2, "li huikeng")
names.append("huang jiahui")
for name in names:
print(name.title() + ", Would you ilke to have a dinner with me?")
print("\n\nSorry, everyone!I only invite two people to join in it.\n\n")
while len(names) > 2:
name = names[0]
names.pop(0)
print("I'm sorry, " + name.title() + ". I could not invite you to join in it.")
print("\n\n")
for name in names:
print(name.title() + ", you're still in my list!")
del names[0]
del names[0]
print("\n\nThe list is ", names, ".")
运行结果:
3-8 放眼世界:
代码:
#coding:gbk
places = ['Beijing', 'Shanghai', 'Lundon', 'Paris', 'New York', 'Madrid']
#原始打印
for place in places:
print(place)
#sorted打印
print("\n\n")
for place in sorted(places):
print(place)
#原始打印进行检验顺序不变
print("\n\n")
for place in places:
print(place)
#sorted,反向打印
print("\n\n")
for place in sorted(places, reverse=True):
print(place)
#原始打印进行检验顺序不变
print("\n\n")
for place in places:
print(place)
#反向打印
print("\n\n")
places.reverse()
for place in places:
print(place)
#再次反向打印
print("\n\n")
places.reverse()
for place in places:
print(place)
#sort打印
print("\n\n")
places.sort()
for place in places:
print(place)
#sort,反向打印
print("\n\n")
places.sort(reverse=True)
for place in places:
print(place)
运行结果:
3-9 晚餐嘉宾:
代码:
names = ['Hong siyuan', 'lin dongwei', 'chen peipeng', 'li yinwei']
for name in names:
print(name.title())
print("\n\nThe number of the poeple i invited is " + str(len(names)) +".")
运行结果:
4-1 披萨 :
代码:
pizzas = ['vegetable', 'fruit', 'mashroom']
for pizza in pizzas:
print("I like " + pizza + " pizza.")
print("\nI really love pizza!")
运行结果:
4-2 动物:
代码:
animals = ['cat', 'dog', 'horse']
for animal in animals:
print("A " + animal + " would make a great pet.")
print("\nAny of these animals would run")
运行结果:
4-3 数到20:
代码:
for number in range(1,21):
print(number)
运行结果:
4-5 计算1~1000000的总和:
代码:
number = list(range(1,1000001))
print(min(number))
print(max(number))
print(sum(number))
运行结果:
4-6 奇数:
代码:
numbers = range(1, 20, 2)
for number in numbers:
print(number)
运行结果:
4-7 3的倍数:
代码:
numbers = range(3, 31, 3)
for number in numbers:
print(number)
运行结果:
4-8 立方:
代码:
numbers = range(1, 11)
cubes = [x**3 for x in numbers]
for cube in cubes:
print(cube)
运行结果:
4-10 切片:
代码:
favorite_foods = ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
print(favorite_foods[:3])
print(favorite_foods[2:])
print(favorite_foods[1:4])
运行结果:
4-11 你的披萨和我的披萨 :
代码:
pizzas = ['vegetable', 'fruit', 'mashroom']
friend_pizzas = pizzas[:]
pizzas.append('fish')
friend_pizzas.append('beef')
print("my favorite pizzas are:")
for pizza in pizzas:
print(pizza)
print("\nmy friend's favorite pizzas are:")
for pizza in friend_pizzas:
print(pizza)
运行结果: