在Python中訪問元組的值
要訪問元組中的值,請使用方括號進行切片,以及索引或索引以獲取該索引處可用的值。
示例
#!/usr/bin/python tup1 = ('physics', 'chemistry', 1997, 2000); tup2 = (1, 2, 3, 4, 5, 6, 7 ); print "tup1[0]: ", tup1[0]; print "tup2[1:5]: ", tup2[1:5];
輸出
當執行以上程式碼時,會產生以下結果 -
tup1[0]: physics tup2[1:5]: [2, 3, 4, 5]
廣告