• 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() + ".
    ")

    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() + ".
    ")
    
    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() + ".
    ")

    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() + ".
    ")
    
        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]

    Caesar卢尚宇

    2020年3月12日

  • 相关阅读:
    spring 之 AOP
    spring 之 动态代理
    为 NSDate 添加扩展类 判断时间
    iOS 日期相关总结
    iOS 请求出现 "Request failed: bad request (400)"
    NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) 解决办法
    poj-3302
    辽宁省赛——杨鲁斯卡尔专场-J
    辽宁省赛——杨鲁斯卡尔专场 -F
    zzuli训练赛_05_13-D
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/12468331.html
Copyright © 2020-2023  润新知