1.遍历整个列表
使用for循环
magicians=['alice','david','carolina']
for magician in magicians:
print(magician)
结果:
alice
david
carolina
for循环的更多操作
magicians=['alice','david','carolina']
for magician in magicians:
print(magician.title()+",that was a great tricke!")
print("I can't wait to see you next trick,"+magician.title()+".\n")
print("Thank you ,everyone! That was a great magic show !")
结果:
Alice,that was a great tricke!
I can't wait to see you next trick,Alice.
David,that was a great tricke!
I can't wait to see you next trick,David.
Carolina,that was a great tricke!
I can't wait to see you next trick,Carolina.
Thank you ,everyone! That was a great magic show !
2.避免缩进错误
python根据缩进来判断代码行与前一个代码行的关系。不要在循环结束前少缩进;结束后多缩进。也要注意不要忘记冒号。
3.创建数值列表
3.1使用函数range()
注意range(1,5)不会显示数字“5”
for value in range(1,5):
print(value)
结果:
1
2
3
4
3.2使用range()创建数字列表
要创建数字列表,可使用函数list()将range()的结果直接转换为列表。
numbers=list(range(1,6))
print(numbers)
结果:
[1, 2, 3, 4, 5]
range()函数还可以指定步长,如打印1~10内的偶数:
numbers=list(range(2,11,2))
print(numbers)
结果:
[2, 4, 6, 8, 10]
使用函数range()几乎能够创建所有需要的数字集。若创建一个列表,其中包含1~10的平方:
squares=[]
for value in range(1,11):
squares.append(value**2)
print(squares)
结果:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
3.3对数字列表执行简单的统计计算
digits=[11,24,3,7,16,45,8]
print(min(digits))
print(max(digits))
print(sum(digits))
结果:
3
45
114
3.4列表解析
前面介绍的生成列表的squares的方式包含三四行代码,而列表解析让你只需要编写一段代码就可以。
squares=[value**2 for value in range(1,11)]
print(squares)
结果:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
4.使用列表的一部分
4.1切片
要创建切片,可指定要使用的第一个元素的索引和最后一个元素的索引加1。
打印前三名队员:
players=['charles','martina','michael','forence','eli']
print(players[0:3])
结果:
['charles', 'martina', 'michael']
如果没有指定第一个索引,将从列表头开始:
players=['charles','martina','michael','forence','eli']
print(players[:4])
结果:
['charles', 'martina', 'michael', 'forence']
类似的,如果要取从第二个到表末尾的所有元素:
players=['charles','martina','michael','forence','eli']
print(players[1:])
结果:
['martina', 'michael', 'forence', 'eli']
如果用负数索引,可以输出倒数的队员,如要输出名单上最后三位队员:
players=['charles','martina','michael','forence','eli']
print(players[-3:])
结果:
['michael', 'forence', 'eli']
4.2遍历切片
players=['charles','martina','michael','forence','eli']
print("Here are first three players on my team:")
for player in players[:3]:
print(player.title())
结果:
Here are first three players on my team:
Charles
Martina
Michael
4.3复制列表
my_foods=['pizza','falafel','carrot cake']
friend_foods=my_foods[:]
print("My favourite foods are:")
print(my_foods)
print("\n my friend's favourite foods are:")
print(friend_foods)
结果:
My favourite foods are:
['pizza', 'falafel', 'carrot cake']
my friend's favourite foods are:
['pizza', 'falafel', 'carrot cake']
注意要使用切片,不然friend_foods=my_foods是行不通的
5.元组
5.1定义元组
python将不能修改的值称为不可变的,不可变的列表被称为元组。
元组看起来犹如列表,但使用圆括号而不是方括号来标识,定义元组后,就可以使用索引来访问其元素,就像访问列表一样。
dimensions=(200,50)
print(dimensions[:])
结果:
(200, 50)
5.2遍历元组中的所有值
dimensions=(200,50)
for dimension in dimensions:
print(dimension)
结果:
200
50
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)
结果:
Original dimensions:
200
50
Modified dimensions:
400
100