Python,元组中的值可以通过索引获得。元组是一种不可变的序列类型,因此,您不能更改元组中的元素,但是可以访问它们。。以下是获得元组中值的一些方法:
元组中的每个元素都有一个索引,索引是元素在元组中的序列号,从0开始。
tup。 =(。'apple','banana','cherry')。print。(。tup。[。0]。)。# 输出 'apple'print。(。tup。[。1。]。)。# 输出 'banana'print。(。tup。[。2。]。)。# 输出 'cherry'
tup。 =(。'apple','banana','cherry')。for。item。 in。tup。:。print。(。item。)。# 输出 'apple'# 输出 'banana'# 输出 'cherry'
tup。 =(。'apple','banana','cherry')。i。 =0while。i。
tup。 =(。'apple','banana','cherry')。values。 =[。value。 for。value。 in。tup。]。print。(。values。)。# 输出 ['apple', 'banana', 'cherry']。
tup。 =(。1。,2。,3。,4。,5。)。print。(。max。(。tup。)。)。# 输出 5。print。(。min。(。tup。)。)。# 输出 1。print。(。sum。(。tup。)。)。# 输出 15。
tup。 =(。'apple','banana','cherry','apple')。print。(。tup。.。count。(。'apple')。)。# 输出 2。
tup。 =(。'apple','banana','cherry')。print。(。tup。.。index。(。'banana')。)。# 输出 1。
分享让更多人看到