3.操作列表

3.1遍厉整个列表

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

3.1.1在for循环中执行更多操作

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
     print(f"{magician.title()},that was a great trick!")
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
     print(f"{magician.title()},that was a great trick!")
     print(f"I can't wait to see your next trick,{magician.title()}".\n)

3.1.2在for循环结束后执行一些操作

在for循环后面,没有缩进的代码都只执行一次,不会重复执行

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
     print(f"{magician.title()},that was a great trick!")
     print(f"I can't wait to see your next trick,{magician.title()}".\n)
print("Thank you,everyone.That was a great magic show!")

3.2避免缩进错误

3.2.1忘记缩进

IndentationError:expected an indented block

3.2.2忘记缩进额外的代码行(逻辑错误)

3.2.3不必要的缩进

IndentationError:unexpected indent

3.2.4循环后不必要的缩进(逻辑错误)

3.2.5遗漏了冒号

3.3创建数值列表

3.3.1使用函数range()

包头不包尾

for value in range(1,5):
     print(value)

#要打印1-5,需要使用range(1,6)
for value in range(1,6)
     print(value)

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

要创建数字列表,可使用函数list()将range()的结果直接转换为列表

numbers = list(range(1,6))
print(numbers)
#结果如下:
#[1,2,3,4,5]

#指定步长
#打印1-10的偶数
even_number = list(range(1,11,2))
print(even_number)
#结果如下:
#[2,4,6,8,10]

#打印前10个整数(1-10)的平方
squares = []
for value in range(1,11):
     square = value**2
     squares.append(square)

print(squares)
#结果如下:
#[1,4,9,16,25,36,49,64,81,100]

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

print(squares)

3.3.3对数字列表执行简单的统计计算

digits = [1,2,3,4,5,6,7,8,9,0]
min(digits)
max(digits)
sum(digits)

3.3.4列表解析

列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素。

squares = [value**2 for value in range(1,11)]
print(squares)
#结果如下:
#[1,4,9,16,25,36,49,64,81,100]

3.4使用列表的一部分

3.4.1切片

要创建切片,可指定要使用的第一个元素和最后一个元素的索引。与函数range()一样,Python在到达第二个索引之前的元素后停止。(包头不包尾)

players = ['charles','martina','michael','florence','eli']
print(players[0:3])
#结果如下:
#['charles','martina','michael']

players = ['charles','martina','michael','florence','eli']
print(players[1:4])
#结果如下:
#['martina','michael','florence']

#如果没有指定第一个索引,Python将自动从列表开头开始
players = ['charles','martina','michael','florence','eli']
print(players[:4])
#结果如下:
#['charles','martina','michael','florence']

players = ['charles','martina','michael','florence','eli']
print(players[2:])
#结果如下:
#['michael','florence','eli']

#打印最后三位队员
players = ['charles','martina','michael','florence','eli']
print(players[-3:])
#结果如下:
#['michael','florence','eli']

3.4.2遍历切片

players = ['charles','martina','michael','florence','eli']

print(“Here are the first three players on my team:”)
for  player in players[:3]
      print(player.title())
#结果如下:
#Here are the first three players on my team:
#Charles
#Martina
#Michael

3.4.3复制列表

要复制列表,可创建一个包含整个列表的切片,方法是同时省略其实索引和终止索引([:])

my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods[:]

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']

#My friend's favorite foods are:
#['pizza','falafel','carrot cake']
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("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','ice cream']

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

3.5元组

列表是可以修改的,元组是不可变的列表

3.5.1定义元组

使用圆括号来标识。

dimensions = (100,50print(dimensions[0])
print(dimensions[1])
#结果如下
#200
#50

严格的说,元组是由逗号标识的。如果你要定义只包含一个元素的元组,必须在这个元素后面加上逗号:

my_t = (3,)

3.5.2遍历元组中的所有值

dimensions = (100,50for dimension in dimensions
     print(dimensions)
#结果如下
#100
#50

3.5.3修改元组变量

dimensions = (100,50)
print("Original dimensions:")
for dimension in dimensions
     print(dimension)

dimensions =(400,100)
print("\nModified dimensions:")
for dimension in dimensions
     print(dimension)
#结果如下
#Original dimensions:
#100
#50

#Modified dimensions:
#400
#100

 

  

 

posted @ 2021-03-10 12:07  从小白到小白  阅读(81)  评论(0)    收藏  举报