Python第四章
2020-12-24 03:28
标签:ike ext 循环 black ack player 语句 really like magicians = [‘alice‘, ‘david‘, ‘carolina‘] magicians = [‘alice‘, ‘david‘, ‘carolina‘] magicians = [‘alice‘, ‘david‘, ‘carolina‘] magicians = [‘alice‘, ‘david‘, ‘carolina‘] magicians = [‘alice‘, ‘david‘, ‘carolina‘] message = "Hello Python world!" print(message) magicians = [‘alice‘, ‘david‘, ‘carolina‘] magicians = [‘alice‘, ‘david‘, ‘carolina‘] for magician in magicians: pizzas = [‘pepperoni‘, ‘hawaiian‘, ‘veggie‘] animals = [‘dog‘, ‘cat‘, ‘pig‘] for value in range(1, 5): for value in range(1, 6): numbers = list(range(1, 6)) squares = [] digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] squares = [value**2 for value in range(1, 11)] players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘] players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘] players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘] players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘] players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘] players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘] my_foods = [‘pizza‘, ‘falafel‘, ‘carrot cake‘] my_foods = [‘pizza‘, ‘falafel‘, ‘carrot cake‘] my_foods = [‘pizza‘, ‘falafel‘, ‘carrot cake‘] dimensions = (200, 50) dimensions = (200, 50) dimensions = (666, 66) dimensions = (200, 50) dimensions = (400, 100) numbers = range(1, 21) # 或者用numbers = list(range(1, 21)) numbers = range(1, 1000001) list = range(1, 20, 2) list = range(3, 33, 3) list = range(1, 11) numbers = [n**3 for n in range(1, 11)] numbers = range(1, 11) favorite_pizzas = [‘pepperoni‘, ‘hawaiian‘, ‘veggie‘] my_foods = [‘pizza‘, ‘falafel‘, ‘carrot cake‘] menu_items = (‘rockfish sandwich‘, ‘ice cream‘, ‘halibut nuggets‘, ‘smoked salmon chowder‘, ‘salmon burger‘, ‘crab cakes‘) print(menu_items[0]) Python第四章 标签:ike ext 循环 black ack player 语句 really like 原文地址:https://www.cnblogs.com/XDZ-TopTan/p/13211979.html4.1 遍历整个列表
for magician in magicians:
print(magician)4.1.2 在for循环中执行更多的操作
for magician in magicians:
print(magician.title() + ", that was a great trick!")
magicians = [‘alice‘, ‘david‘, ‘carolina‘]
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can‘t wait to see your next trick, " + magician.title() + ".\n")4.1.3 在for循环结束后执行一些操作
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can‘t wait to see your next trick, " + magician.title() + ".\n")
print("Thank you, everyone. That was a great magic show!")4.2.1 忘记缩进
for magician in magicians:print(magician) # 忘记缩进会报错必须按照下一行输出
print(magician)
4.2.2 忘记缩进额外的代码行
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can‘t wait to see your next trick, " + magician.title() + ".\n") # 此处忘记缩进,最后导致只在最后一次循环输出打印这一行内容4.2.3 不必要的缩进
print(message) # 此处不需要缩进,否则会有报错提示,必须按照下一行语句输出打印
4.2.4 循环后不必要的缩进
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can‘t wait to see your next trick, " + magician.title() + ".\n")
print("Thank you, everyone. That was a great magic show!") # 此处不需要缩进,不然结果每次循环都会打印输出此行内容4.2.5 遗漏了冒号
for magician in magicians # 若此行不加冒号会导致语法错误,必须按照下一行语句输出打印
print(magician)4-1 按要求打印输出披萨列表
for pizza in pizzas:
print(pizza)
print("\n")
for pizza in pizzas:
print("I like " + pizza + "pizza.")
print("\nI really love pizza!")4-2 按要求打印动物列表
for animal in animals:
print(animal)
print("A " + animal + " will make a great pet.")
print("\nAny of these animals would make a great pet!")4.3 创建数值列表
4.3.1 使用函数range()
print(value) # 打印1-4
print(value) # 打印1-54.3.2 使用函数range()创建数字列表
print(numbers)
numbers = list(range(2, 11, 2))
print(numbers)
squares = []
for value in range(1, 11):
square = value**2
squares.append(square)
print(squares)
for value in range(1, 11):
squares.append(value**2)
print(squares)4.3.3 对数字列表执行简单的统计计算
print(sum(digits))4.3.4 列表解析
print(squares)4.4.1 切片
print(players[0:3])
print(players[1:4])
print(players[:4])
print(players[2:])
print(players[-3:])4.4.2 遍历切片
print("Here are the first three players on my team:")
for player in players[:3]:
print(player.title())4.4.3 复制列表(省略起始索引和终止索引)
friend_foods = my_foods[:]
print("My favorite foods are:")
print(my_foods)
print("\nMy fiend‘s favorite foods are:")
print(friend_foods)
friend_foods = my_foods[:]
my_foods.append(‘cannoli‘)
friend_foods.append(‘ice cream‘)
print("My favorite foods are:")
print(my_foods)
print("\nMy fiend‘s favorite foods are:")
print(friend_foods)
friend_foods = my_foods # 这样会得到两个一样的列表
my_foods.append(‘cannoli‘)
friend_foods.append(‘ice cream‘)
print("My favorite foods are:")
print(my_foods)
print("\nMy fiend‘s favorite foods are:")
print(friend_foods)4.5.1 定义元组(元组:不可变的列表)
print(dimensions[0])
print(dimensions[1])dimensions[0] = 250 # 元组不能修改,即不能给元组的元素赋值,这样运行会出现类型错误
dimensions[1] = 100 # 元组不能修改,即不能给元组的元素赋值,这样运行会出现类型错误
4.5.2 遍历元组中的所有值
for dimension in dimensions:
print(dimension)4.5.3 修改元组变量
print("Original dimensions:")
for dimension in dimensions:
print(dimension)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)4-3 使用一个for 循环打印数字1~20(含)
for number in numbers:
print(number)4-4 创建一个列表并打印
lists = range(1, 1000001)
for n in lists:
print(n)
4-5 计算1~1 000 000的总和
print(min(numbers))
print(max(numbers))
print(sum(numbers))4-6 创建列表(0~20)并打印其中的奇数
for number in list:
print(number)4-7 创建列表:3的倍数(其中包含3~30内能被3整除的数字)并打印
for number in list:
print(number)4-8 打印1-10的立方数
numbers = []
for n in list:
numbers.append(n**3)
print(numbers)4-9 立方解析打印1-10的立方数
print(numbers)4-10 切片
print("\nThe first three items in the list are:\n")
print(numbers[:3])
for n in numbers[:3]:
print(n)
print("\nThe items from tne middle of the list are:\n")
print(numbers[3:6])
for n in numbers[3:6]:
print(n)
print("\nThe last three items in the list are:\n")
print(numbers[-3:])
for n in numbers[-3:]: # 要取最后一个元素,后面只能用空;如果用-1,无法打印最后一个元素
print(n)4-11 按要求打印输出披萨列表
friend_pizzas = favorite_pizzas[:]
favorite_pizzas.append("meat lover‘s")
friend_pizzas.append(‘pesto‘)
print("\nMy favorite pizzas are:\n")
for pizza in favorite_pizzas:
print(pizza)
print("\nMy friend‘s favorite pizzas are:\n")
for pizza in friend_pizzas:
print(pizza)4-12 使用多个循环输出打印列表
friend_foods = my_foods[:]
my_foods.append(‘cannoli‘)
friend_foods.append(‘ice cream‘)
print("My favorite foods are:")
for food in my_foods:
print(food)
print("\nMy fiend‘s favorite foods are:")
for food in friend_foods:
print(food)4-13 按要求打印餐馆的实物
print("\nYou can choose the following menu items:")
for food in menu_items:
print("- " + food)menu_items[0] = ‘vegetables‘ # 无法修改元组内的元素
menu_items = (‘rockfish sandwich‘, ‘ice cream‘, ‘halibut nuggets‘, ‘smoked salmon chowder‘, ‘black cod tips‘, ‘king crab legs‘)
print("\n Our menu has been updated.")
print("\nYou can choose the following items:")
for food in menu_items:
print("- " + food)