当前位置: 首页 > 图灵资讯 > 行业资讯> python如何访问元组中的元素

python如何访问元组中的元素

发布时间:2025-01-08 14:54:41

python访问元组中的元素的方法:我们可以使用下标索引来访问元组中的值。具体使用方法如:【tup1 = ('physics', 'chemistry', 1997, 2000);print tup1[0]】。

可以使用下标索引来访问元组中的值。

(推荐教程: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]

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09