Python编程 从入门到实践-4操作列表上

笔记出处(学习UP主视频记录) https://www.bilibili.com/video/av35698354?p=6

4.1 遍历整个列表

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

alice
david
carolina

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

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print (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!

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print (magician.title() + ", that was a great trick!")
    print ("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.

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

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

print ("Thank you, everyone. That was 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 was a great magic show!

 4.2.1 忘记缩进

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

print (magician)
^
IndentationError: expected an indented block

 4.2.2 忘记缩进额外的代码行

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

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

4.2.3 不必要的缩进

message = "Hello Python world!"
    print (message)

print (message)
^
IndentationError: unexpected indent

4.2.4 循环后不必要的缩进

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

    print ("Thank you, everyone. That was a great magic show!")

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

Thank you, everyone. That was a great magic show!
David, that was a great trick!
I can't wait to see your next trick, David.

Thank you, everyone. That was a great magic show!
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.

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

4.2.5 遗漏了冒号

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

for magician in magicians
^
SyntaxError: invalid syntax

4.3 创建数值列表

4.3.1 使用函数range()

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

1
2
3
4

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

1
2
3
4
5

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

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

[1, 2, 3, 4, 5]

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

[2, 4, 6, 8, 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)

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

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

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

0

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

9

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

45

4.3.4 列表解析

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

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

上一篇:iOS中block的定义与使用


下一篇:关于Python修改列表的值的问题