一、遍历整个列表——for循环
1.for循环的基本使用
输入如下代码,其中number的名称可随机制定
numbers=[1,2,3,4]
for number in numbers:
print(number)
则输出结果如下:
1
2
3
4
2.for循环的缩进
对for循环来说,是否有缩进是判断语句是否在循环内的标志。
numbers=[1,2,3,4]
for number in numbers:
print(str(number)+" is num")
print("They are all nums")
输出结果如下:
1 is num
2 is num
3 is num
4 is num
They are all nums
动手试一试4-1、4-2代码:
pizzas=["kfc pizza","Mcd pizza","pizza hut pizza"]
for pizza in pizzas:
print("I like "+pizza)
print("I really love pizza!\n")
pets=["cat","dog","fish"]
for pet in pets:
print("A "+pet+" would make a great pet")
print("Any of these animals would make a great pet!")
输出结果:
I like kfc pizza
I like Mcd pizza
I like pizza hut pizza
I really love pizza!
A cat would make a great pet
A dog would make a great pet
A fish would make a great pet
Any of these animals would make a great pet!
3.创建数字列表——range()
运行如下代码:
for value in range(1,5)
print(value)
输出结果(注意结果中不包含5)
1
2
3
4
也可对其进行一些运算
squares=[]
for value in range(1,11):
squares.append(value**2)
print(squares)
输出结果:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
动手试一试 4-3到4-9代码:
for value in range(1,21):
print(value)
print("\n")
numbers=[]
for value in range(1,1000001):
numbers.append(value)
print(str(sum(numbers))+"\n")
for value in range(1,21,2):
print(value)
print("\n")
lists=[value for value in range(3,31,3)]
print(lists)
print("\n")
lists2=[]
for nums in range(1,11):
lists2.append(nums**3)
print(lists2)
print("\n")
lists3=[nums**3 for nums in range(1,11)]
print(lists3)
输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
500000500000
1
3
5
7
9
11
13
15
17
19
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
4.使用列表的一部分
指定第一个元素的索引和最后一个元素的索引加1。
若使用nums[-3:],则输出最后3个元素。
nums=[1,2,3,4,5]
print(nums[2:4])
print(nums[:4])
print(nums[-3:])
输出:
[3, 4]
[1, 2, 3, 4]
[3, 4, 5]
可通过以下方法复制:
nums=[1,2,3,4,5]
nums2=nums[:]
print(nums)
print(nums2)
输出如下:
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
动手试一试 4-10 4-11 4-12
nums=[1,2,3,4,5]
print("The first three items in the list are:"+str(nums[0:3]))
print("Three items from the middle of the list are:"+str(nums[1:4]))
print("The last three items in the list are:"+str(nums[-3:]))
print("\n")
pizzas=["kfc pizza","Mcd pizza","pizza hut pizza"]
friend_pizzas=pizzas[:]
for pizza in friend_pizzas:
print(pizza)
pizzas.append("x pizza")
friend_pizzas.append("y pizza")
print("My favourite pizzas are:"+str(pizzas))
print("My friend's favourite pizzas are:"+str(friend_pizzas))
输出结果:
The first three items in the list are:[1, 2, 3]
Three items from the middle of the list are:[2, 3, 4]
The last three items in the list are:[3, 4, 5]
kfc pizza
Mcd pizza
pizza hut pizza
My favourite pizzas are:['kfc pizza', 'Mcd pizza', 'pizza hut pizza', 'x pizza']
My friend's favourite pizzas are:['kfc pizza', 'Mcd pizza', 'pizza hut pizza', 'y pizza']
5、元组(不可修改,但可赋值)
元组用圆括号来标识而非方括号,其他与列表一样。
5.1、元组和列表的区别
1.元组和列表都属于序列。
2.列表属于可变序列,它的元素可以随时修改或者删除,而元组属于不可变序列,其中的元素是不能修改的,除非整体重新赋值。
3.列表可以使用多种方法实现添加和修改列表元素,而元组没有办法,因为不能想元组中添加或修改元素,同样也不能删除元素。
4.列表可以使用切片方法访问和修改列表中的元素,元组也支持切片,但是它只支持通过切片访问元组中的元素,不支持修改。
5.元组比列表中的访问和处理速度更快,所以如果只需要对其中的元素进行访问,而不进行任何修改,建议使用元组。
6.列表不能作为字典类型中的键,而元组是可以的。
动手试一试4-13
foods=("apple","pear","melon","strawberry","lemon")
for food in foods:
print(food)
print("\n")
#foods[2]="orange"
foods=("apple","pear","melon","fish","chicken")
for food in foods:
print(food)
输出结果:
apple
pear
melon
strawberry
lemon
apple
pear
melon
fish
chicken