在学习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'>

 

相关文章:

  • 2021-07-22
  • 2021-11-21
  • 2022-12-23
  • 2022-12-23
  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-19
  • 2021-12-19
  • 2022-12-23
  • 2022-02-18
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
相关资源
相似解决方案