- Q:打印所有的魔术师 遍历列表
- A:使用for循环
#下面是一个演示
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
--------------------------------------------------------------------
alice
david
carolina
#下面深入的说一下循环
for magician in magicians: # 这行代码让python获取列表magicians的第一个值 并将其存储到变量magician中
print(magician) # 它让python打印magician 鉴于列表中还包含其他值 python返回到循环的第一行 依次继续打印下去
- Q:在for循环中执行更多的操作
- A:每个缩进的代码都是循环的一部分
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() +'.
')
--------------------------------------------------------------------
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.
- Q:在for循环结束后执行一些操作
- A:没有所进的代码都执行一次,不会重复操作
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!')
- Q:缩进错误
- A:
- 忘记缩进
- 忘记缩进额外的代码行
- 不必要的缩进
- 循环后不必要的缩进
- 遗漏了冒号
# 1忘记缩进
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician.title() + ",that was a great trick!")
# 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() +'.
')
# 3不必要的缩进
magicians = ['alice','david','carolina']
print(magicians[0])
# 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!')
# 5遗漏了冒号
for magician in magicians
print(magician.title() + ",that was a great trick!")
- Q:创建数字列表
- A:
- 使用函数range()”创建数字列表“
- 对列表进行统计运算 函数max() min() sum()
- 列表解析 将for循环和创建列表的代码合并为一行
# 下面是使用函数range()创建列表
print(list(range(1,5)))
# 注意range()函数是生成一些整数 这里借助list函数将这些整数排成一个列表
# 下面是对列表统计运算的例子
>>>number = [1,2,3,4,5,6]
>>>min(number)
1
>>>max(number)
6
>>>sum(number)
21
# 下面是解析列表的一行代码示例
squares = [value**2 for value in range(1,10)]
print(squares)
-------------------------------------------------------------
[1,4,9,16,25,36,49,64,81,100]
- Q:列表切片
- A:[0:0]
players = ['charles','martina','michael','florence','eli']
print(players[0:3]) # 前三
print(platyer[:4]) # 前四
print(platyer[2:]) # 第二位后面的
print(platyer[-3:]) # 后三个
print(platyer[:]) # 全部
Q:遍历切片
A:和遍历列表操做一样
- Q:复制列表
- A:使用切片复制列表
my_foods = ['pizza','falafel','carrot']
friends_food = my_foods[:]
print(friends_foods)
# 下面我们核实一下
my_foods.append('cannoli')
friends_food.append('ice cream')
# 再分别打印即可
# 下面是一种直接的错误思想
my_foods = ['pizza','falafel','carrot']
friends_food = my_foods
my_foods.append('cannoli')
friends_food.append('ice cream')
print(my_foods)
print(friends_food)
# 事实上这里让python将新变量friends_foods关联到包含在my_foods中的列表,两个变量都指向一个列表