zoukankan      html  css  js  c++  java
  • Python之路【第十篇】:索引与切片的不同

    在学习list、tuple、str等数据类型时,多次接触到索引与切片

    元素数量而言:

    索引:取一个元素

    切片:可以取多个元素

    元素类型而言:

    索引:不好描述,举个例子来说:lst = ['a','b','c'],lst[1]得到‘b’是个字符串

    切片:不好描述,举个例子来说:lst = ['a','b','c'],lst[1:2]得到[‘b’]是个列表

    lst = ['hello','world','I','love','python']
    
    #索引:
    a = lst[1]
    print(a)
    print(type(a))
    
    #切片:
    b = lst[1:2]
    print(b)
    print(type(b))
    
    c =lst[1:4]
    print(c)
    print(type(c))
    输出结果为:

    world
    <class 'str'>
    ['world']
    <class 'list'>
    ['world', 'I', 'love']
    <class 'list'>

    三样东西有助于缓解生命的疲劳:希望、睡眠和微笑。---康德
  • 相关阅读:
    程序的机器级表示(一)
    virtual memory(1)
    Python定义参数数量可变的method的问题
    存储器结构层次(四)
    CAShapeLayer
    cell
    远程服务器推送
    keyboad
    search搜索
    Cocoopod
  • 原文地址:https://www.cnblogs.com/ronghe/p/8309224.html
Copyright © 2011-2022 走看看