# 4.1 遍历整个列表
#### 任务:把魔术师名单中每个名字打印出来
#### 魔术师名单 = ['alice','david','carolina']
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
## 4.1.1 深入研究循环
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
## 4.1.2 在for循环中执行更多操作
任务:打印出
Alice,that was a great trick!
David,that was a great trick!
Carolina,that was a great trick!
使用for循环+字符串+title()
magicians = ['alice','david','carolina']
for magician in magicians:
print(f"{magician.title()},that was a great trick!")
任务:打印出
Alice,that was a great trick!
I can't wait to see your next trick,Alice.
David,that was a great trick!
I can't wait to see your next trick,David.
Carolina,that was a great trick!
I can't wait to see your next trick,Carolina.
magicians = ['alice','david','carolina']
for magician in magicians:
print(f"{magician.title()},that was a great trick!")
print(f"I can't wait to see your next trick,{magician.title()}.\n")
## 4.1.3 在for循环结束后执行一些操作
任务:打印出
Alice,that was a great trick!
I can't wait to see your next trick,Alice.
David,that was a great trick!
I can't wait to see your next trick,David.
Carolina,that was a great trick!
I can't wait to see your next trick,Carolina.
Thank you,everyone.That was a great magic show!
magicians = ['alice','david','carolina']
for magician in magicians:
print(f"{magician.title()},that was a great trick!")
print(f"I can't wait to see your next trick,{magician.title()}.\n")
print("Thank you,everyone.That was a great magic show!")
## 4.2 避免缩进错误
### 4.2.1 忘记缩进
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
### 4.2.2 忘记缩进额外的代码行
magicians = ['alice','david','carolina']
for magician in magicians:
print(f"{magician.title()},that was a great trick!")
print(f"I can't wait to see your next trick,{magician.title()}.\n")
magicians = ['alice','david','carolina']
for magician in magicians:
print(f"{magician.title()},that was a great trick!")
print(f"I can't wait to see your next trick,{magician.title()}.\n")
### 4.2.3 不必要的缩进
message = "Hello py"
print(message)
message = "Hello py"
print(message)
### 4.2.4 循环后不必要的缩进
### 4.2.5 遗漏了冒号
magicians = ['alice','david','carolina']
for magician in magicians
print(f"{magician.title()},that was a great trick!")
magicians = ['alice','david','carolina']
for magician in magicians:
print(f"{magician.title()},that was a great trick!")
# 4.3 创建数值列表
## 4.3.1 range() 和list()
range(x) = range(0,x)
range(x,y)
range(x,y,z)
list(range(6))
list(range(0,6))
list(range(1,6))
list(range(0,6,2)) #步长为1,间隔为1
list(range(0,6,1))
## 4.3.2 使用range() 创建数字列表
list(range(0,6))
number = list(range(0,6))
print(number)
任务:打印1~10的偶数,步长为2
[2,4,6,8,10]
even_numbers = list(range(2,11,2))
print(even_numbers)
任务:打印1~10的奇数,步长为2
odd_numbers = list(range(1,11,2))
print(odd_numbers)
任务:创建一个列表,包含1~10的平方
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,45,67,89,0,2]
max(digits)
min(digits)
sum(digits)
## 4.3.4 列表解析
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)
squares = [value**2 for value in range(1,11)] #列表解析
print(squares)
## 4.4 使用列表的一部分
### 4.4.1 切片
players = ['charles','martina','michael','florence','eli']
打印列表的第一个元素(索引0)
players = ['charles','martina','michael','florence','eli']
print(players[0])
打印列表的第1,2,3个元素(索引0,1,2)
players = ['charles','martina','michael','florence','eli']
print(players[0:3]) #左闭右开
打印列表的第2,3,4个元素(索引1,2,3)
players = ['charles','martina','michael','florence','eli']
print(players[1:4]) #左闭右开
打印列表的第1,2,3,4个元素(索引0,1,2,3)
players = ['charles','martina','michael','florence','eli']
print(players[:4]) #省略开始索引
打印列表的第3,4,5个元素(索引2,3,4)
players = ['charles','martina','michael','florence','eli']
print(players[2:]) #省略终止索引
打印列表的最后3个元素(负数索引)
players = ['charles','martina','michael','florence','eli']
print(players[-3:]) #返回列表末尾相应距离的元素
让python每隔2个元素就提取1个
players = ['charles','martina','michael','florence','eli']
print(players[1:4])
players = ['charles','martina','michael','florence','eli']
print(players[1:4:1])
players = ['charles','martina','michael','florence','eli']
print(players[0:4:2])
## 4.4.2 遍历切片
任务:打印出以下结果
Here are the first three players on my team:
Charless
Martina
Michael
players = ['charles','martina','michael','florence','eli']
print("Here are the first three players on my team:")
for player in players[:3]: #索引切片内容
print(player.title())
players = ['charles','martina','michael','florence','eli']
print("Here are the first three players on my team:")
for player in players[0: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("\nMy friend'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("\nMy friend'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("\nMy friend's favorite foods are:")
print(friend_foods)
# 4.5 元组
## 4.5.1 定义元组--不可变的列表 ()
dimensions = (200,50)
print(dimensions[0])
print(dimensions[1])
dimensions = (200,) #元组里面可以只有一个元素,
print(dimensions[0])
dimensions = (200,50)
dimensions[0] = 250 #元组不能修改
## 4.5.2 遍历元组中的所有值
dimensions = (200,50)
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("\nModified dimensions:")
for dimension in dimensions:
print(dimension)