操作列表

1、遍历整个列表

1.1用for循环打印

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician)
#输出结果
alice
david
carolina  

1.2深入研究循环
上述代码先执行for magician in magicians:,然后执行print(magician)打印alice;然后magicians还有其他元素,继续执行for magician in magicians:,再执行 print(magician)打印david;直到打印完全部元素。

1.3在for循环中执行更多的操作
操作1

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
#输出结果
Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!

操作2

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")
#输出结果
Alice, that was a great trick!
I can't wait to see your next trick, Alice.

David, that was a great trick!
I can't wait to see your next trick, David.

Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.

1.4在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 wsa a great magic show!")
#输出结果
Alice, that was a great trick!
I can't wait to see your next trick, Alice.

David, that was a great trick!
I can't wait to see your next trick, David.

Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.

Thank you, everyone. That wsa a great magic show!

2、避免缩进错误

2.1忘记缩进
对于位于for语句后面且属于循环组成部分的代码行,一定要缩进!没缩进时,会报错。

2.2忘记缩进额外的代码行
注意逻辑错误,假设你的for循环后面要跟两行缩进代码,但是你只缩进第一行,第二行没有缩进,此时python不会报错,但输出并不会达到你的预期,所以一定要注意!!!

2.3不必要的缩进
如果你不小心缩进了无须缩进的代码行,python将会指出。为避免意外缩进错误,请只缩进需要缩进的代码。

2.4遗漏了冒号
for语句后面的冒号切记不要遗忘,否则将花费大量的时间去寻找错误。

3、创建数值列表

3.1使用函数range()
函数range()让你能够轻松生成一系列数。它让python从指定的第一个值开始数,并在到达你指定的第二个值时停止,所以不会输出第二个值。

for value in range(1, 5):
    print(value)
#输出结果
1
2
3
4

注意:
1.调用函数range()时,也可以只指定一个函数,这样它将从0开始。例如range(6)返回数0~5。
2.使用range()时,如果输出不符合预期,请尝试将指定的值加1或减1。

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

numbers = list(range(1, 6))
print(numbers)
#输出结果
[1, 2, 3, 4, 5]

3.2.2使用range()时还可以指定步长。
下面的代码打印1~10的偶数

even_numbers = list(range(2, 11, 2))
print(even_numbers)
#输出结果
[2, 4, 6, 8, 10]

3.2.3使用range()几乎能创建任何数集
下面代码打印整数(1~10)的平方,在Python中,用两个星号(**)表示乘方运算。

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

print(square)
#输出结果
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

立方:在Python中,2的立方用2**3来表示。

3.3对数字列表执行简单的统计计算
有几个专门用于处理数字列表的函数。

digits = list(range(10))
print(digits)
print(min(digits))
print(max(digits))
print(sum(digits))
#输出结果
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0
9
45

3.4列表解析

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

要使用这种语法,首先指定一个描述性的列表名,如squares。然后,指定一个左方括号,并定义一个表达式,用于生成要存储到列表中的值。在这个示例中,表达式为value**2,它计算平方值。接下来,用一个for循环,用于给表达式提供值,再加上右方括号。在这个示例中,for循环为for value in range(1,11),它将值1~10提供给表达式value**2。请注意,这里的for语句末尾没有冒号。

4、使用列表的一部分

4.1切片
要创建切片,可指定要使用的第一个元素和最后一个元素的索引。

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
print(players[1:4])

print(players[:4]) #从列表开头开始
print(players[2:]) #开始于索引2,终止于列表末尾
print(players[-3:])#开始于倒数第三个元素,终止于列表末尾
#输出结果
['charles', 'martina', 'michael']
['martina', 'michael', 'florence']
['charles', 'martina', 'michael', 'florence']
['michael', 'florence', 'eli']
['michael', 'florence', 'eli']

4.2遍历切片
遍历列表的部分元素,可以在for循环中使用切片。

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    

4.3复制列表
要复制整个列表,同时省略起始和终止索引。复制后的列表和原来的列表互不影响,为两个单独的列表。

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
#不能使用friend_foods = my_foods来复制
#否则只是将新变量关联于旧变量,两个变量同时指向一个列表
#既改变其中一个,另一个变量也会跟着改变
print(friend_foods)
#输出结果
['pizza', 'falafel', 'carrot cake']

5、元组

5.1定义元组
元组很像列表,但是值不可修改,python中元组使用圆括号来表示。

dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
#输出结果
200
50

注意,如果元组只包含一个元素,必须在这个元素后面加上逗号。

5.2遍历元组中所有值
和列表一样,用for循环来遍历。

dimensions = (200, 50)

for dimension in dimensions:
    print(dimension)
#输出结果
200
50

5.3修改元组变量
虽然不能修改元组的元素,但可以给存储元组的变量重新赋值。

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

dimensions = (400, 100)
for dimension in dimensions:
    print(dimension)
#输出结果
200
50
400
100

6、设置代码格式

最好遵循PEP 8 的Python改进提案。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值