# 操作列表

#### 一、遍历整个列表

###### 使用for循环来打印魔术师名单中的所有名字:

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)


alice
david
carolina


1、避免缩进错误
2、避免出现不必要的缩进
3、不要忘记for后的冒号

#### 二、创建数值列表

• 使用函数range()
for value in range(1,6):
print(value)


1
2
3
4
5

• 使用range()创建数字列表

numbers=list(range(1,6))
print(numbers)


[1, 2, 3, 4, 5]


even_numbers = list(range(2,11,2))
print(even_numbers)


[2, 4, 6, 8, 10]


squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)


[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

• 对数字列表执行简单的统计计算
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits))
# 0
print(max(digits))
# 9
print(sum(digits))
# 45

• 列表解析
squares = [value**2 for value in range(1,11)] print(squares)


#### 三、使用列表的一部分

###### 切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
#['charles', 'martina', 'michael']
print(players[2:])
#['michael', 'florence', 'eli']
print(players[:2])
#['charles', 'martina']
print(players[-2:])
#['florence', 'eli']

###### 遍历切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
for player in players[:3]:
print(player)


charles
martina
michael

###### 复制列表

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 favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli']

My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'ice cream']


my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods #错误的示例
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("\nMy favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)


My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']

My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']


#### 四、元组

###### 定义元组

dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])


200
50


###### 遍历元组中的所有值

dimensions = (200, 50)
for dimension in dimensions:
print(dimension)


200
50

###### 修改元祖变量

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


#### 一部分练习题

# 4-3 数到20
for number in range(1,21):
print(number)
# 4-4 一百万
for number in list(range(1,100)):
print(number)
# 4-5 计算 1~1 000 000 的总和
numbers=list(range(1,1000001))
print(min(numbers))
print(max(numbers))
print(sum(numbers))
# 4-6 奇数
for number in range(1,21,2):
print(number)
# 4-7 3的倍数
for number in range(3,31,3):
print(number)
# 4-8 立方
squares=[]
for value in range(1,11):
squares.append(value**3);
print(squares)
# 4-9 立方解析
squares=[value**3 for value in range(1,11)]
print(squares)


©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客