Python编程:从入门到实践------第4章:操作列表

一、遍历整个列表——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、元组(不可修改,但可赋值)

元组用圆括号来标识而非方括号,其他与列表一样。

动手试一试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

猜你喜欢

转载自blog.csdn.net/weixin_44487378/article/details/104078494