Python第四章
Posted xdz-toptan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python第四章相关的知识,希望对你有一定的参考价值。
4.1 遍历整个列表
magicians = [‘alice‘, ‘david‘, ‘carolina‘]
for magician in magicians:
print(magician)
4.1.2 在for循环中执行更多的操作
magicians = [‘alice‘, ‘david‘, ‘carolina‘]
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() + ".
")
4.1.3 在for循环结束后执行一些操作
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() + ".
")
print("Thank you, everyone. That was a great magic show!")
4.2.1 忘记缩进
magicians = [‘alice‘, ‘david‘, ‘carolina‘]
for magician in magicians:
print(magician) # 忘记缩进会报错必须按照下一行输出
print(magician)
4.2.2 忘记缩进额外的代码行
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() + ".
") # 此处忘记缩进,最后导致只在最后一次循环输出打印这一行内容
4.2.3 不必要的缩进
message = "Hello Python world!"
print(message) # 此处不需要缩进,否则会有报错提示,必须按照下一行语句输出打印
print(message)
4.2.4 循环后不必要的缩进
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() + ".
")
print("Thank you, everyone. That was a great magic show!") # 此处不需要缩进,不然结果每次循环都会打印输出此行内容
4.2.5 遗漏了冒号
magicians = [‘alice‘, ‘david‘, ‘carolina‘]
for magician in magicians # 若此行不加冒号会导致语法错误,必须按照下一行语句输出打印
for magician in magicians:
print(magician)
4-1 按要求打印输出披萨列表
pizzas = [‘pepperoni‘, ‘hawaiian‘, ‘veggie‘]
for pizza in pizzas:
print(pizza)
print("
")
for pizza in pizzas:
print("I like " + pizza + "pizza.")
print("
I really love pizza!")
4-2 按要求打印动物列表
animals = [‘dog‘, ‘cat‘, ‘pig‘]
for animal in animals:
print(animal)
print("A " + animal + " will make a great pet.")
print("
Any of these animals would make a great pet!")
4.3 创建数值列表
4.3.1 使用函数range()
for value in range(1, 5):
print(value) # 打印1-4
for value in range(1, 6):
print(value) # 打印1-5
4.3.2 使用函数range()创建数字列表
numbers = list(range(1, 6))
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)
squares = []
for value in range(1, 11):
squares.append(value**2)
print(squares)
4.3.3 对数字列表执行简单的统计计算
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(sum(digits))
4.3.4 列表解析
squares = [value**2 for value in range(1, 11)]
print(squares)
4.4.1 切片
players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
print(players[0:3])
players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
print(players[1:4])
players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
print(players[:4])
players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
print(players[2:])
players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
print(players[-3:])
4.4.2 遍历切片
players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
print("Here are the first three players on my team:")
for player in players[:3]:
print(player.title())
4.4.3 复制列表(省略起始索引和终止索引)
my_foods = [‘pizza‘, ‘falafel‘, ‘carrot cake‘]
friend_foods = my_foods[:]
print("My favorite foods are:")
print(my_foods)
print("
My fiend‘s favorite foods are:")
print(friend_foods)
my_foods = [‘pizza‘, ‘falafel‘, ‘carrot cake‘]
friend_foods = my_foods[:]
my_foods.append(‘cannoli‘)
friend_foods.append(‘ice cream‘)
print("My favorite foods are:")
print(my_foods)
print("
My fiend‘s favorite foods are:")
print(friend_foods)
my_foods = [‘pizza‘, ‘falafel‘, ‘carrot cake‘]
friend_foods = my_foods # 这样会得到两个一样的列表
my_foods.append(‘cannoli‘)
friend_foods.append(‘ice cream‘)
print("My favorite foods are:")
print(my_foods)
print("
My fiend‘s favorite foods are:")
print(friend_foods)
4.5.1 定义元组(元组:不可变的列表)
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
dimensions = (200, 50)
dimensions[0] = 250 # 元组不能修改,即不能给元组的元素赋值,这样运行会出现类型错误
dimensions[1] = 100 # 元组不能修改,即不能给元组的元素赋值,这样运行会出现类型错误
4.5.2 遍历元组中的所有值
dimensions = (666, 66)
for dimension in dimensions:
print(dimension)
4.5.3 修改元组变量
dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)
dimensions = (400, 100)
print("
Modified dimensions:")
for dimension in dimensions:
print(dimension)
4-3 使用一个for 循环打印数字1~20(含)
numbers = range(1, 21) # 或者用numbers = list(range(1, 21))
for number in numbers:
print(number)
4-4 创建一个列表并打印
lists = range(1, 1000001)
for n in lists:
print(n)
4-5 计算1~1 000 000的总和
numbers = range(1, 1000001)
print(min(numbers))
print(max(numbers))
print(sum(numbers))
4-6 创建列表(0~20)并打印其中的奇数
list = range(1, 20, 2)
for number in list:
print(number)
4-7 创建列表:3的倍数(其中包含3~30内能被3整除的数字)并打印
list = range(3, 33, 3)
for number in list:
print(number)
4-8 打印1-10的立方数
list = range(1, 11)
numbers = []
for n in list:
numbers.append(n**3)
print(numbers)
4-9 立方解析打印1-10的立方数
numbers = [n**3 for n in range(1, 11)]
print(numbers)
4-10 切片
numbers = range(1, 11)
print("
The first three items in the list are:
")
print(numbers[:3])
for n in numbers[:3]:
print(n)
print("
The items from tne middle of the list are:
")
print(numbers[3:6])
for n in numbers[3:6]:
print(n)
print("
The last three items in the list are:
")
print(numbers[-3:])
for n in numbers[-3:]: # 要取最后一个元素,后面只能用空;如果用-1,无法打印最后一个元素
print(n)
4-11 按要求打印输出披萨列表
favorite_pizzas = [‘pepperoni‘, ‘hawaiian‘, ‘veggie‘]
friend_pizzas = favorite_pizzas[:]
favorite_pizzas.append("meat lover‘s")
friend_pizzas.append(‘pesto‘)
print("
My favorite pizzas are:
")
for pizza in favorite_pizzas:
print(pizza)
print("
My friend‘s favorite pizzas are:
")
for pizza in friend_pizzas:
print(pizza)
4-12 使用多个循环输出打印列表
my_foods = [‘pizza‘, ‘falafel‘, ‘carrot cake‘]
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("
My fiend‘s favorite foods are:")
for food in friend_foods:
print(food)
4-13 按要求打印餐馆的实物
menu_items = (‘rockfish sandwich‘, ‘ice cream‘, ‘halibut nuggets‘, ‘smoked salmon chowder‘, ‘salmon burger‘, ‘crab cakes‘)
print("
You can choose the following menu items:")
for food in menu_items:
print("- " + food)
menu_items[0] = ‘vegetables‘ # 无法修改元组内的元素
print(menu_items[0])
menu_items = (‘rockfish sandwich‘, ‘ice cream‘, ‘halibut nuggets‘, ‘smoked salmon chowder‘, ‘black cod tips‘, ‘king crab legs‘)
print("
Our menu has been updated.")
print("
You can choose the following items:")
for food in menu_items:
print("- " + food)
以上是关于Python第四章的主要内容,如果未能解决你的问题,请参考以下文章