logo
  • 教程列表
  • SO官方文檔
  • 元組
    • 元組
    • 元組是不可變的
    • 包裝和拆包元組
    • 內建元組功能
    • 元組是元素明確的可洗和等同的
    • 索引元組
    • 反轉元素
  1. StackOverflow 文件
  2. Python Language 教程
  3. 元組
  4. 索引元組

索引元組

Created: November-22, 2018

x = (1, 2, 3)
x[0]  # 1
x[1]  # 2
x[2]  # 3
x[3]  # IndexError: tuple index out of range

使用負數進行索引將從最後一個元素開始為 -1:

x[-1] # 3
x[-2] # 2
x[-3] # 1
x[-4] # IndexError: tuple index out of range

索引一系列元素

print(x[:-1])   # (1, 2)
print(x[-1:])   # (3,)
print(x[1:3])   # (2, 3)
  • 反轉元素
  • 元組是元素明確的可洗和等同的

Copyright © 2018. All right reserved

tastones.com 备案号:鲁ICP备18045372号-1

  • 關於本站
  • 免責聲明