zoukankan      html  css  js  c++  java
  • python基础(三)

    操作列表

      • 遍历整个列表

      • 避免缩进错误

      • 创建数值列表

      • 使用列表的一部分

      • 元组

      • 设置代码格式

    littles = ['q','w','e','m']
    for little in littles:
        print(little) #for循环

    q
    w

    e
    m

    #在for循环中执行更多的操作
    for little in littles:
        print(little.title() + ",hahahhaha!")

    Q,hahahhaha!

    W,hahahhaha!
    E,hahahhaha!
    M,hahahhaha!



    for little in littles:
        print(little.title() + ",hahahhaha!")
        print("I love my home!" + "
    ")#两条for循环都缩进了,都将针对每个字母执行一次
    Q,hahahhaha!
    I love my home!

    W,hahahhaha!
    I love my home!

    E,hahahhaha!
    I love my home!

    M,hahahhaha!
    I love my home!
    for  little in littles:
        print(little.title() + ",hahahhaha!")
    print("I love my home!")#最后执行一次
    
    
    Q,hahahhaha!
    W,hahahhaha!
    E,hahahhaha!
    M,hahahhaha!
    I love my home!
    
    
    for little in littles:
        print(little.title() + ",hahahhaha!")
        print("I love my home!" + "
    ")
    print("hihihi!")
    
    

    Q,hahahhaha!

    I love my home!

    W,hahahhaha!
    I love my home!

    E,hahahhaha!
    I love my home!

    M,hahahhaha!
    I love my home!

    hihihi!


    #创建数值列表

    #使用函数range()
    for value in range(1,5):
        print(value)

    1

    2
    3

    4

    
    
    number = list(range(1,5))
    print(number)
    [1, 2, 3, 4]
    even_numbers = list(range(2,11,2))
    print(even_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]
    #对数字列表进行简单的统计计算
    digits = range(0,10)
    min(digits)
    0
    max(digits)
    
    

    9

    
    
    sum(digits)

    45

    这个用起来比较方便!for循环直接放在里面

    #列表解析,简化代码
    squares = [value**2 for value in range(1,11)]
    print(squares)

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

    
    
    
    

    #使用列表的一部分

    切片,左闭右开

    
    
    haha = ['99','96','97','66']
    print(haha[0:3])
    ['99', '96', '97']
    print(haha[1:4])
    ['96', '97', '66']
    print(haha[:4])

    ['99', '96', '97', '66']

    print(haha[2:])

    ['97', '66']

    print(haha[-3:])#最后三个

    ['96', '97', '66']

    haha = ['99','96','97','66']
    print("Here are the scores:")
    for score in haha[:3]:
        print(score.title())

    Here are the scores:

    99
    96
    97
    #复制列表,可创建一个包含整个列表的切片
    haha = ['99','96','97','66']
    hihi = haha[:]
    print("my scores are:")
    print(haha)

    my scores are:

    ['99', '96', '97', '66']
    print("his scores are:")

    print(hihi)
    his scores are:
    ['99', '96', '97', '66']
    haha.append('100')
    hihi.append('99')
    print(haha)
    print(hihi)

    ['99', '96', '97', '66', '100']

    ['99', '96', '97', '66', '99']

    #元组

    列表非常适合于存储在程序运行期间可能变化的数据集,列表是可以修改的。[]

    元组,适用于创建一系列不可修改的元素。()
    dim = (200,50)
    print(dim[0])
    200
    print(dim[1])

    50

    dim[0] = 100
    print(dim[0])#python不能给元组的元素赋值

    ---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)
    <ipython-input-57-743659d5cfb2> in <module>
    ----> 1 dim[0] = 100
    2 print(dim[0])

    TypeError: 'tuple' object does not support item assignment
    #遍历元组中的所有值
    dims = (200,50)
    for dim in dims:
        print(dim)
    200
    50
    #修改元组变量,重新定义整个元组
    dims = (400, 100)
    print(dims)
    
    

    (400, 100)

    for dim in dims:
        print(dim)

    400

    100

     
  • 相关阅读:
    How to Analyze Java Thread Dumps
    Attention to encodings
    Inspect Memory Leak
    Java boolean operator &=, |= and ^=
    网站PC端和移动端,用户通过设备识别
    网页平滑过渡效果和自适用,三角图形,动效
    开机自动调用.bat文件且浏览器全屏展示
    react中使用屏保
    Axios的get和post请求写法
    React在componentWillMount中请求接口数据结束后再执行render
  • 原文地址:https://www.cnblogs.com/Cookie-Jing/p/13589543.html
Copyright © 2011-2022 走看看